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?