PDA

View Full Version : how to get lineEdit content which is created dynamically?



aurora
19th October 2011, 07:14
In my program i'm creating lineEdit widget dynamically whenever a user enters "ADD" button.
But i'm not able to access the text of that dynamically created lineEdit.
my code is as follows...


on_pushButton_ADD_clicked()
{
QLineEdit *lineEdit = new QLineEdit;
QLabel *label = new QLabel;
ui->gridLayout->addWidget( label,LayoutCount,0 );
ui->gridLayout->addWidget( lineEdit,LayoutCount,1 );
.................
}


for accessing the dynamically created lineEdit i'm trying as follows..

KeyList.append(ui->gridLayout->lineEdit->text());

But i'm getting error....
Please help me.!!!!!!

FelixB
19th October 2011, 07:42
you get an error because "gridlayout" has no member "lineEdit". See the documentation of QGridLayout, it has a method "widgetAt(int x, int y)" - or something like that. It returns a QWidget which must be casted to QLineEdit.

Pseudocode:



for(unsigned int y=0; y<MAX; y++)
if(QLineEdit* edit = qobject_cast<QLineEdit*>(ui->gridlayout->widgetAt(layoutCount,y)))
// do something with the lineEdit


hth,
Felix

aurora
19th October 2011, 09:37
you get an error because "gridlayout" has no member "lineEdit". See the documentation of QGridLayout, it has a method "widgetAt(int x, int y)" - or something like that. It returns a QWidget which must be casted to QLineEdit.

Pseudocode:



for(unsigned int y=0; y<MAX; y++)
if(QLineEdit* edit = qobject_cast<QLineEdit*>(ui->gridlayout->widgetAt(layoutCount,y)))
// do something with the lineEdit


hth,
Felix
SORRY....that does not work....there is a method called " ItemAtPosition(int x, int y)", it returns a layout item...but i'm not able to get the lineEdit....which is at that position....so getting error...

FelixB
19th October 2011, 10:14
OMG, have a look at the docs of QLayoutItem.

QLayoutItem has a member "QWidget* widget()". What do you think could this method do?

nish
19th October 2011, 10:40
rather than playing with the layout i suggest you to keep track of your lineEdits in a QList or a QStack. As soon as you add a new lineEdit, insert the pointer into the list and then you can access the lineEdits anywhere.

aurora
19th October 2011, 11:19
rather than playing with the layout i suggest you to keep track of your lineEdits in a QList or a QStack. As soon as you add a new lineEdit, insert the pointer into the list and then you can access the lineEdits anywhere.

i tried to do like this.....

QList<QWidget *> lineEditList;
QLineEdit *lineEdit = new QLineEdit;

lineEditList.append(lineEdit);
//doing remaining operation....



wnd while accessing list ...

QString temp=lineEditList.at(i)->text();


but this does not works....i'm getting error..:(

FelixB
19th October 2011, 11:57
but this does not works....i'm getting error..:(

what error do you get? somethings wrong with my crystal ball...

aurora
19th October 2011, 12:09
what error do you get? somethings wrong with my crystal ball...


ERROR:'class QWidget' has no member named 'text'!

FelixB
19th October 2011, 12:46
of course not. have a second look at my example in the second post. maybe you'll find something that might solve your problem...

nish
19th October 2011, 12:50
if you are storing only LineEdits then why not use QList<QLineEdit*> ?? And read about dynamic_cast or qobject_cast.

aurora
24th October 2011, 04:56
ya...thank u...QList<QLineEdit *> lineEditList
worked for me....
But now i'm also using combobox with that.....
so i declared like this,

QList<QComboBox *> TagList;
but getting error saying that "'ComboBox' was not declared in this scope"!!!!

nish
24th October 2011, 07:06
then store only QWidgets, and do a qobject_cast and see if the object is lineEdit or combobox

FelixB
24th October 2011, 07:56
but getting error saying that "'ComboBox' was not declared in this scope"!!!!

I assume you mean "'QComboBox'...".

do you include the header of QComboBox?