Results 1 to 7 of 7

Thread: When sort the lists?

  1. #1
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question When sort the lists?

    Hi all, In my app I use a QTabWidget with 6 tab widgets. Each of them consists in a QListWidget. I fill these lists with the thumbnailed images from a list of files (assigning a list using the image format/type). I want all the QListWidgets to be sorted in ascending order using the name of the image file. Also, I want to set in the text of each tab the name of the format/type of the images that its list contains and the number of images in the list in parenthesis. My question is: when do you think that could be better/cleaner to sort the lists and set the text of the tabs?
    A) After inserting each image as QListWidgetItem in the correct QListWidget on the loop that reads each file from the list of files to insert in the lists.
    B) Once all the images in the list of files have been inserted in the lists.
    In the first case, every time that a new image is inserted in a QListWidget, this list is sorted (which could be inefficient) and fixed the text of the tab. But, differing from the second option, it's not necesary to loop through all the tabs at the end to do it. What do you think that's better?
    Last edited by SkripT; 14th April 2006 at 00:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When sort the lists?

    Sort that list of files or insert items in the Right Place(tm).

  3. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: When sort the lists?

    Quote Originally Posted by jacek
    Sort that list of files or insert items in the Right Place(tm).
    Sorry I mean sort the QListWidgets with QListWidget::sortItems in ascending order

  4. #4
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: When sort the lists?

    Any suggestion, please?

  5. #5
    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: When sort the lists?

    I think Jacek's answer still stands If you insert them in a proper order, you won't have to sort them.

  6. #6
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: When sort the lists?

    Quote Originally Posted by wysota
    I think Jacek's answer still stands If you insert them in a proper order, you won't have to sort them.
    Yes you both are right. But suppose the case that the list is not empty and you insert new items (at the end of the list). In this case what's the best way?

  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: When sort the lists?

    A) After inserting each image as QListWidgetItem in the correct QListWidget on the loop that reads each file from the list of files to insert in the lists.
    This is exactly the same as inserting the item in the proper place. The cost of such operation depends on the way you insert those items. In the worst case you have to make (x-1) comparisons for each item and you are inserting n items, so it gives O(n^2).

    B) Once all the images in the list of files have been inserted in the lists.
    This is a single sort operation with O(nlgn) cost.

    B is faster, A is better if you update the list during the fill, because you always have a sorted list.

  8. The following user says thank you to wysota for this useful post:

    SkripT (14th April 2006)

Similar Threads

  1. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  2. QTreeWidget (Row Check, Custom Sort)
    By AaronMK in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2008, 15:55
  3. How to sort the treeWidget item according to size and date
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 10:32
  4. QTreeWidget->clear() causing crash after sort
    By Chuk in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2007, 09:59
  5. How to sort a QComboBox in Qt4
    By guenthk in forum Qt Programming
    Replies: 7
    Last Post: 25th September 2006, 18:35

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.