PDA

View Full Version : Read multiple QlistWidget items to strings



Cyrebo
21st March 2013, 21:53
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:


//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.

d_stranz
22nd March 2013, 23:24
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
QString allNamesInOneString;
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}


To do the second:



//Get user inputs
QStringList allNamesAsAList;
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().

Cyrebo
28th March 2013, 10:11
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
QString allNamesInOneString;
int names = ui->listWidget->count();
for(int i = 0; i < names; i++)
{
allNamesInOneString += ui->listWidget->item(i)->text();
}


To do the second:



//Get user inputs
QStringList allNamesAsAList;
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...



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;
}


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?

Cyrebo
29th March 2013, 01:32
Problem solved haha so simple, thanks for helping anyways!