Results 1 to 13 of 13

Thread: how to get lineEdit content which is created dynamically?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to get lineEdit content which is created dynamically?

    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...
    Qt Code:
    1. on_pushButton_ADD_clicked()
    2. {
    3. QLineEdit *lineEdit = new QLineEdit;
    4. QLabel *label = new QLabel;
    5. ui->gridLayout->addWidget( label,LayoutCount,0 );
    6. ui->gridLayout->addWidget( lineEdit,LayoutCount,1 );
    7. .................
    8. }
    To copy to clipboard, switch view to plain text mode 

    for accessing the dynamically created lineEdit i'm trying as follows..
    Qt Code:
    1. KeyList.append(ui->gridLayout->lineEdit->text());
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    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:

    Qt Code:
    1. for(unsigned int y=0; y<MAX; y++)
    2. if(QLineEdit* edit = qobject_cast<QLineEdit*>(ui->gridlayout->widgetAt(layoutCount,y)))
    3. // do something with the lineEdit
    To copy to clipboard, switch view to plain text mode 

    hth,
    Felix

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    Quote Originally Posted by FelixB View Post
    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:

    Qt Code:
    1. for(unsigned int y=0; y<MAX; y++)
    2. if(QLineEdit* edit = qobject_cast<QLineEdit*>(ui->gridlayout->widgetAt(layoutCount,y)))
    3. // do something with the lineEdit
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    OMG, have a look at the docs of QLayoutItem.

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

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to get lineEdit content which is created dynamically?

    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.

  6. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    Quote Originally Posted by nish View Post
    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.....
    Qt Code:
    1. QList<QWidget *> lineEditList;
    2. QLineEdit *lineEdit = new QLineEdit;
    3.  
    4. lineEditList.append(lineEdit);
    5. //doing remaining operation....
    To copy to clipboard, switch view to plain text mode 


    wnd while accessing list ...
    Qt Code:
    1. QString temp=lineEditList.at(i)->text();
    To copy to clipboard, switch view to plain text mode 


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

  7. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    Quote Originally Posted by aurora View Post
    but this does not works....i'm getting error..
    what error do you get? somethings wrong with my crystal ball...

  8. #8
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    Quote Originally Posted by FelixB View Post
    what error do you get? somethings wrong with my crystal ball...

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

  9. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    of course not. have a second look at my example in the second post. maybe you'll find something that might solve your problem...

  10. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to get lineEdit content which is created dynamically?

    if you are storing only LineEdits then why not use QList<QLineEdit*> ?? And read about dynamic_cast or qobject_cast.

  11. #11
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    ya...thank u...QList<QLineEdit *> lineEditList
    worked for me....
    But now i'm also using combobox with that.....
    so i declared like this,
    Qt Code:
    1. QList<QComboBox *> TagList;
    To copy to clipboard, switch view to plain text mode 
    but getting error saying that "'ComboBox' was not declared in this scope"!!!!

  12. #12
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to get lineEdit content which is created dynamically?

    then store only QWidgets, and do a qobject_cast and see if the object is lineEdit or combobox

  13. #13
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get lineEdit content which is created dynamically?

    Quote Originally Posted by aurora View Post
    but getting error saying that "'ComboBox' was not declared in this scope"!!!!
    I assume you mean "'QComboBox'...".

    do you include the header of QComboBox?

  14. The following user says thank you to FelixB for this useful post:

    aurora (24th October 2011)

Similar Threads

  1. Two dynamically created object interaction issue
    By kornicameister in forum Qt Quick
    Replies: 2
    Last Post: 9th September 2011, 11:35
  2. Replies: 3
    Last Post: 11th August 2011, 17:16
  3. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09:40
  4. QMainWindow ignores dynamically created, floating QDockWidget
    By stefanadelbert in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2010, 01:06
  5. read and write content of lineedit widget in Other Form
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 15:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.