
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:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}
//Get user inputs
QString allNamesInOneString;
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}
To copy to clipboard, switch view to plain text mode
To do the second:
//Get user inputs
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesAsAList << ui->listWidget->item(i)->text();
}
//Get user inputs
QStringList allNamesAsAList;
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesAsAList << ui->listWidget->item(i)->text();
}
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...
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;
}
static int LayoutCount;
QLineEdit *lineEdit = new QLineEdit;
ui->gridLayout->addWidget( lineEdit,LayoutCount,0 );
int iCount = ui->gridLayout->count(); //Total no of LineEdit added on gridLayout dynamically
QString str;
for(int i = 0; i < iCount; i++)
{
QLayoutItem* pLine = ui->gridLayout->itemAt(i);
QLineEdit* pLineEdit = dynamic_cast<QLineEdit*>(pLine->widget()); //<<<<<<<<<<<<<<<<<<<
if(pLineEdit != 0) //<<<<<<<<<<<<<<<<<<<
{
str = pLineEdit->text();
ui->listWidget->addItem(str);
}
qDebug() << str;
}
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?
Bookmarks