Results 1 to 7 of 7

Thread: get initial row index of QListView after sorting?

  1. #1
    Join Date
    Mar 2011
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question get initial row index of QListView after sorting?

    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?

  2. #2
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: get initial row index of QListView after sorting?

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: get initial row index of QListView after sorting?

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Mar 2011
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: get initial row index of QListView after sorting?

    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).

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: get initial row index of QListView after sorting?

    Why not just sort the QList as well?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2011
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: get initial row index of QListView after sorting?

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get initial row index of QListView after sorting?

    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.

    Qt Code:
    1. QList<QListWidgetItem*> items;
    2. QList<...> list;
    3. for(int i=0; i<list.count();++i){
    4. fillItemFromList(item, list.at(i));
    5. items << item;
    6. listWidget->addItem(item);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 2nd March 2011, 09:03
  2. Replies: 1
    Last Post: 12th January 2011, 06:52
  3. Sorting in QListView class
    By Yayati.Ekbote in forum Qt Programming
    Replies: 5
    Last Post: 14th April 2010, 12:26
  4. QListView sorting
    By fear in forum Qt Programming
    Replies: 14
    Last Post: 21st August 2007, 13:08
  5. How can I disable the QListView Sorting?
    By darpan in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2006, 10:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.