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:
Code:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i <= names; i++){
QString list
= ui
->listWidget
->item
(i
)->text
();
}
Does anyone have an illustration or example doing something similar or the same thing?
Thanks in advance.
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:
Code:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}
To do the second:
Code:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesAsAList << ui->listWidget->item(i)->text();
}
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().
Re: Read multiple QlistWidget items to strings
Quote:
Originally Posted by
d_stranz
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:
Code:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}
To do the second:
Code:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesAsAList << ui->listWidget->item(i)->text();
}
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...
Code:
static int LayoutCount;
ui->gridLayout->addWidget( lineEdit,LayoutCount,0 );
int iCount = ui->gridLayout->count(); //Total no of LineEdit added on gridLayout dynamically
for(int i = 0; i < iCount; i++)
{
QLineEdit* pLineEdit
= dynamic_cast<QLineEdit
*>
(pLine
->widget
());
//<<<<<<<<<<<<<<<<<<<
if(pLineEdit != 0) //<<<<<<<<<<<<<<<<<<<
{
str = pLineEdit->text();
ui->listWidget->addItem(str);
}
qDebug() << str;
}
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?
Re: Read multiple QlistWidget items to strings
Problem solved haha so simple, thanks for helping anyways!