Results 1 to 8 of 8

Thread: getting QString out of QListWidget

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default getting QString out of QListWidget

    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.

    Qt Code:
    1. //"number" and "features" are int values
    2. //"dataTable" is a table of integers
    3.  
    4. //declare QListWidget list
    5. QListWidget sList = new QListWidget(this);
    6.  
    7. //fill QListWidget list with entries
    8. for(int k=0; k<number+1; k++)
    9. {
    10. QString entry;
    11. for(int i=0; i<features+1; i++)
    12. {
    13. entry.append(dataTable[i][k]);
    14. entry.append(",");
    15. }
    16. sList->insertItem(k, entry);
    17. }
    18.  
    19. //sort list entries in ascending order
    20. sList->sortItems(Qt::AscendingOrder);
    21.  
    22. //get the list entries back in the QString format
    23. //I DON'T KNOW HOW??
    24. //QString entry2 = sList.itemAt(1); for example won't work
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting QString out of QListWidget

    Set this property to true first.
    Qt Code:
    1. void setSortingEnabled ( bool enable )
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yogeshgokul for this useful post:

    tommy (3rd August 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: getting QString out of QListWidget

    Try:
    Qt Code:
    1. QString str = sList.item(1)->text();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to numbat for this useful post:

    tommy (3rd August 2009)

  6. #4
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: getting QString out of QListWidget

    Thanks!
    However when I try to compile

    Qt Code:
    1. QString entry2 = sList.item(1)->text();
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by tommy; 3rd August 2009 at 13:02.

  7. #5
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting QString out of QListWidget

    Is your existing code is working after setting the property ?

  8. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting QString out of QListWidget

    you should use operator ->
    Qt Code:
    1. QString entry2 = sList->item(1)->text();
    To copy to clipboard, switch view to plain text mode 
    since you created sList in the heap.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    tommy (3rd August 2009)

  10. #7
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: getting QString out of QListWidget

    Yes, everything else is working properly. I just cannot get the QString back from the QList Widget.

  11. #8
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: getting QString out of QListWidget

    Hi Spirit,

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

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. Replies: 13
    Last Post: 15th December 2006, 11:52

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
  •  
Qt is a trademark of The Qt Company.