PDA

View Full Version : how to add and delete the text from dynamically created line edits



Cyrebo
28th March 2013, 00:08
Hi,

My question refers to this thread created in 2011....
http://www.qtforum.org/article/36821/how-to-get-lineedit-content-which-is-created-dynamically.html

I would like to modify it so the text can also be deleted with a delete button. I want to add all the text that gets inputted to a database but I want to allow the user to also delete a line input they wish to change.

Here's what I have so far, it adds them to the list widget but with a lot of spacing for some reason...


void userDialog::on_pushButton_3_clicked()
{
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 = (QLineEdit*)pLine->widget();
str = pLineEdit->text();
ui->listWidget->addItem(str);

qDebug() << str;
}

}


Plz help...

giblit
28th March 2013, 02:48
just add this to your onbuttonclicked function
lineEdit->clear();
or
ui->lineEdit->clear();
if you are using the designer ui form

Santosh Reddy
28th March 2013, 09:29
All items in a QLayout may not be QLineEdits (not event QWidgets), there are some layout space management items (which are QWidgets/QLineEdits). You need to take text() only from valid QLineEdit items, also I am wondring why does you program does not crash !

Do something like this


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

Cyrebo
28th March 2013, 09:46
[QUOTE=Santosh Reddy;241542]All items in a QLayout may not be QLineEdits (not event QWidgets), there are some layout space management items (which are QWidgets/QLineEdits). You need to take text() only from valid QLineEdit items, also I am wondring why does you program does not crash !

Do something like this


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;
}
[/QUxtOTE]

Thanks but this does not solve my problem. The qlistwidget still adds extra spacing inbetween each text. This is a problem because if i want to read all the text in, I would be reading some spacing. How do i dynamically delete the text that was added? Right now the program is storing the text in a variable called str but I want to delete it from the qlistwidget and later read the text values in from there.

Santosh Reddy
28th March 2013, 09:57
How do i dynamically delete the text that was added?
From where you want to delete text?


Right now the program is storing the text in a variable called str but I want to delete it from the qlistwidget and later read the text values in from there.
It is not clear yet, do want to clear the QListWidget ?, and later read text from where?

Cyrebo
28th March 2013, 10:16
I mean I have an add button function which allows for dynamically adding text to the list widget but I want to able to delete the text from the widget. The program saves the text entered at the point of entry by the user to a var called str. I want to alter this so that the text is saved in list widget then when the user clicks another button(Ok), each text is saved individually. I need this to save the text to a database table.

anda_skoa
28th March 2013, 21:14
If you dynamically create widget that you would want to interact with later on, the best way is always to keep their pointers in an appropriately typed container.

E.g. QList<QLineEdit*>, etc

Cheers,
_

Santosh Reddy
29th March 2013, 08:02
Please answer my earlier questions, which will help me understand what you want

Cyrebo
30th March 2013, 17:44
If you dynamically create widget that you would want to interact with later on, the best way is always to keep their pointers in an appropriately typed container.

E.g. QList<QLineEdit*>, etc
_

Thanks for suggestion but how do you do this calling the pointer from outside where you declared it?

E.g. I have a declared it to show on runtime but I also want to take in the user input from it on the click of a button which inside another public/private slot. How do I call the pointer from there to get the value from the pointer object such as qlinedit?

Added after 6 minutes:


Please answer my earlier questions, which will help me understand what you want

My reply to anda skoa is pretty much what I want to accomplish as far as reading and writing goes.

anda_skoa
2nd April 2013, 10:02
Thanks for suggestion but how do you do this calling the pointer from outside where you declared it?

E.g. I have a declared it to show on runtime but I also want to take in the user input from it on the click of a button which inside another public/private slot. How do I call the pointer from there to get the value from the pointer object such as qlinedit?


I am not entirely sure what you mean.
First you would have a QList<QLineEdit*> member in your class. When you create line edits dynamically, you add them to the list and can then iterate over that list or use index based access anywhere in your instance.

Cheers,
_