Results 1 to 4 of 4

Thread: Read multiple QlistWidget items to strings

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Read multiple QlistWidget items to strings

    Hi, i'm working on a program where i ask a user to input certain line edits that are added as a QlistWidget item when the user clicks add. I want to able to get the QlistWidget items(line edits) and save them as a string. I see no direct way of doing this in the qlistwidget library. So far I'm passing the items to a string but not able to get them each individually.

    Here's the snippet of my program where I try to do this:

    Qt Code:
    1. //Get user inputs
    2. int names = ui->listWidget->count();
    3. for(int i = 0; i <= names; i++){
    4. QString list = ui->listWidget->item(i)->text();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone have an illustration or example doing something similar or the same thing?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read multiple QlistWidget items to strings

    What do you want - all of the items concatenated into a single string, or each item as a separate string in a list of strings?

    To do the first:

    Qt Code:
    1. //Get user inputs
    2. QString allNamesInOneString;
    3. int names = ui->listWidget->count();
    4. for(int i = 0; i < names; i++)
    5. {
    6. allNamesInOneString += ui->listWidget->item(i)->text();
    7. }
    To copy to clipboard, switch view to plain text mode 

    To do the second:

    Qt Code:
    1. //Get user inputs
    2. QStringList allNamesAsAList;
    3. int names = ui->listWidget->count();
    4. for(int i = 0; i < names; i++)
    5. {
    6. allNamesAsAList << ui->listWidget->item(i)->text();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Note that your original code attempts to retrieve one too many items from the list widget. List widget item indexes go from 0 to count() - 1, not count().

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

    Cyrebo (29th March 2013)

  4. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Read multiple QlistWidget items to strings

    Quote Originally Posted by d_stranz View Post
    What do you want - all of the items concatenated into a single string, or each item as a separate string in a list of strings?

    To do the first:

    Qt Code:
    1. //Get user inputs
    2. QString allNamesInOneString;
    3. int names = ui->listWidget->count();
    4. for(int i = 0; i < names; i++)
    5. {
    6. allNamesInOneString += ui->listWidget->item(i)->text();
    7. }
    To copy to clipboard, switch view to plain text mode 

    To do the second:

    Qt Code:
    1. //Get user inputs
    2. QStringList allNamesAsAList;
    3. int names = ui->listWidget->count();
    4. for(int i = 0; i < names; i++)
    5. {
    6. allNamesAsAList << ui->listWidget->item(i)->text();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Note that your original code attempts to retrieve one too many items from the list widget. List widget item indexes go from 0 to count() - 1, not count().
    Thanks for the reply, however I need the text to be read in dynamically at run time. I want to save all the text the user enters to a database dynamically and not as a list but as a seperate string for each item. I have something like this which takes the input from a grid layout and saves it each time a user clicks add but is not really ideal if you want to delete an item...

    Qt Code:
    1. static int LayoutCount;
    2.  
    3. QLineEdit *lineEdit = new QLineEdit;
    4.  
    5. ui->gridLayout->addWidget( lineEdit,LayoutCount,0 );
    6.  
    7. int iCount = ui->gridLayout->count(); //Total no of LineEdit added on gridLayout dynamically
    8. QString str;
    9. for(int i = 0; i < iCount; i++)
    10. {
    11. QLayoutItem* pLine = ui->gridLayout->itemAt(i);
    12. QLineEdit* pLineEdit = dynamic_cast<QLineEdit*>(pLine->widget()); //<<<<<<<<<<<<<<<<<<<
    13.  
    14. if(pLineEdit != 0) //<<<<<<<<<<<<<<<<<<<
    15. {
    16. str = pLineEdit->text();
    17. ui->listWidget->addItem(str);
    18. }
    19. qDebug() << str;
    20. }
    To copy to clipboard, switch view to plain text mode 

    This works for only saving the item, could you please show me how to improve this to also allow for the deletion of an item?

  5. #4
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Read multiple QlistWidget items to strings

    Problem solved haha so simple, thanks for helping anyways!

Similar Threads

  1. QListWidget's strings get truncated in dialog
    By alxobr in forum Qt Programming
    Replies: 2
    Last Post: 7th June 2011, 12:43
  2. doubles and strings as items of a QTableWidget
    By fatecasino in forum Newbie
    Replies: 1
    Last Post: 29th January 2011, 18:38
  3. Replies: 2
    Last Post: 22nd November 2010, 21:49
  4. Replies: 1
    Last Post: 7th May 2010, 15:01
  5. Read files with strings and doubles
    By thebra in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 20:41

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.