[Qt-interest] Is it a stupid way for sorting?

Mark Brand mabrand at mabrand.nl
Thu Apr 22 08:54:49 CEST 2010


>
> I want to sort QStringList by specific item count so I tried to use 
> QMap for it but I failed but I used QTableWidget instead but without 
> viewing it in the GUI, *is it correct way for sorting? it's slow (and 
> stupid :-[ )?*
>
> QStringListsortPatterns(QStringList patterns)
> {
>      QStringList result;
>      QTableWidget *table =new  QTableWidget(patterns.count(), 2);
>      for(int  x=0; x<patterns.count(); x++)
>      {
>          int  count = patterns[x].count("?");
>          table->setItem(x, 0,new  QTableWidgetItem(QString("%1").arg(count, 2,'0')));
>          table->setItem(x, 1,new  QTableWidgetItem(patterns[x]));
>      }
>      table->sortItems(0);
>      for(int  x=0; x<patterns.count(); x++)
>          result<<table->item(x, 1)->text();
>      return  result;
> }
>


Although other respondents have already suggested better solutions, you 
should also understand that you have created a memory leak here. The 
QTableWidget neither has a parent nor is explicitly deleted.

regards,

Mark




More information about the Qt-interest-old mailing list