Results 1 to 6 of 6

Thread: Implementing QListWidget sortItems

  1. #1
    Join Date
    Jun 2015
    Posts
    3
    Qt products
    Qt5

    Default Implementing QListWidget sortItems

    Hi, I'm wondering how to go about implementing QListWidget::sortItems() based on different data in my custom QListWidgetItem.
    For example, to be able to call sortItems(Type::ID) or sortItems(Type::Name) with both ascending and decending option but I'm having trouble figuring out how to go about doing that.

    On a related note, I can't seem to find a method for getting all the QListWidgetItems in QListWidget, how would one go about getting a list of the items? Just create a basic for loop and call QListWidget::Item?

  2. #2
    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: Implementing QListWidget sortItems

    Quote Originally Posted by Superwig View Post
    Hi, I'm wondering how to go about implementing QListWidget::sortItems() based on different data in my custom QListWidgetItem.
    For example, to be able to call sortItems(Type::ID) or sortItems(Type::Name) with both ascending and decending option but I'm having trouble figuring out how to go about doing that.
    What is the problem you are having?

    On a related note, I can't seem to find a method for getting all the QListWidgetItems in QListWidget, how would one go about getting a list of the items? Just create a basic for loop and call QListWidget::Item?
    QListWidget::item() returns a given item, QListWidget::count() returns the number of items.
    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.


  3. #3
    Join Date
    Jun 2015
    Posts
    3
    Qt products
    Qt5

    Default Re: Implementing QListWidget sortItems

    How to reimplement sortItems

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Implementing QListWidget sortItems

    When you call sortItems(), the list view will run a sorting algorthm on its items, using the items' operator<() for comparison.
    So you decide which item "is less than" another by reiplementing that operator.

    Cheers,
    _

  5. #5
    Join Date
    Jun 2015
    Posts
    3
    Qt products
    Qt5

    Default Re: Implementing QListWidget sortItems

    Quote Originally Posted by anda_skoa View Post
    When you call sortItems(), the list view will run a sorting algorthm on its items, using the items' operator<() for comparison.
    So you decide which item "is less than" another by reiplementing that operator.

    Cheers,
    _
    So how would you go about implementing a sort function on the items which takes a parameter?

  6. #6
    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: Implementing QListWidget sortItems

    You would check the current settings of the list widget owning the item and act upon that.
    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: 2
    Last Post: 1st April 2011, 09:32
  2. sortItems by double value in QTableWidget
    By wconstan in forum Newbie
    Replies: 4
    Last Post: 27th December 2009, 18:20
  3. Replies: 1
    Last Post: 18th November 2009, 07:38
  4. Implementing IME using QT
    By pmohod in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2007, 12:22
  5. Replies: 2
    Last Post: 25th March 2006, 06:54

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.