PDA

View Full Version : getting QString out of QListWidget



tommy
3rd August 2009, 10:17
Hello,

I have a QListWidget list that I first fill with QStrings, then sort the list, and then I want to get the QStrings back out of the list. I don't know how to do that. Can you help? Below is my code with comments.



//"number" and "features" are int values
//"dataTable" is a table of integers

//declare QListWidget list
QListWidget sList = new QListWidget(this);

//fill QListWidget list with entries
for(int k=0; k<number+1; k++)
{
QString entry;
for(int i=0; i<features+1; i++)
{
entry.append(dataTable[i][k]);
entry.append(",");
}
sList->insertItem(k, entry);
}

//sort list entries in ascending order
sList->sortItems(Qt::AscendingOrder);

//get the list entries back in the QString format
//I DON'T KNOW HOW??
//QString entry2 = sList.itemAt(1); for example won't work


Thanks!

yogeshgokul
3rd August 2009, 10:19
Set this property to true first.


void setSortingEnabled ( bool enable )

numbat
3rd August 2009, 10:37
Try:


QString str = sList.item(1)->text();

tommy
3rd August 2009, 12:54
Thanks!
However when I try to compile



QString entry2 = sList.item(1)->text();


I get:
error: 'item' has not been declared

But item is a function of QListWidgetItem, so why/where would I declare 'item'?
Is this happening because I don't build my QListWidget right?
Thanks!

yogeshgokul
3rd August 2009, 12:57
Is your existing code is working after setting the property ?

spirit
3rd August 2009, 13:07
you should use operator ->


QString entry2 = sList->item(1)->text();

since you created sList in the heap.

tommy
3rd August 2009, 13:09
Yes, everything else is working properly. I just cannot get the QString back from the QList Widget.

tommy
3rd August 2009, 13:10
Hi Spirit,

Of course, you are correct. Everything is fixed now. Thanks!