PDA

View Full Version : get initial row index of QListView after sorting?



lisarden
28th March 2011, 10:55
I use QListView to show some data. To add rows I use addItem() and when use activated() event a row number is for the order in which items were added. If sorting is enabled then row order changes and I can't get the initial row index of a particular row upon selection.

How can I get the initial row index existing before sorting after the sorting is performed?

BalaQT
28th March 2011, 11:36
when use activated() event a row number is for the order in which items were added. If sorting is enabled then row order changes and I can't get the initial row index of a particular row upon selection.

you can use int currentRow ()
hope it helps,

bala

high_flyer
28th March 2011, 11:42
Can please explain what do you mean?
You have QListWidget.
You add an Item "our item", which is now in index X.
You add few other items, while sorting is enabled, so now "our item" has index Y.
Do you want to know its original index X, or do you want to know which item has index X now?
If its the former, can you explain why?
Because I can't think of a case where you would need to know the previous index of an item.

lisarden
28th March 2011, 12:52
Thank you guys!

I explain: I parse XML file and add necessary items from it to QList. Then, I scan through QList to add items from it to QlistWidget. If sorting is enabled then QList.at(0) != QListWidget.item(0) in general. So I need to know which item from QList corresponds to a current QLIstWidget item (after sorting).

high_flyer
28th March 2011, 13:00
Why not just sort the QList as well?

lisarden
28th March 2011, 13:11
do you mean firtst sort QList and then just add the content to QListWidget without sorting? I don't find any of sort() functions included to QList class. Should I do it myself? I find QListWidget sorting capability good enough for my application except for the initial problem I posted.

wysota
28th March 2011, 13:13
What do you need the QList for? Can't you build the QListWidget right away? Either way you can have another list of items you add to the list. Then the order of items in QListWidget doesn't matter as you have your own unsorted list.


QList<QListWidgetItem*> items;
QList<...> list;
for(int i=0; i<list.count();++i){
QListWidgetItem *item = new QListWidgetItem;
fillItemFromList(item, list.at(i));
items << item;
listWidget->addItem(item);
}