Results 1 to 5 of 5

Thread: how to access widgets values?

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to access widgets values?

    by using folowing coding , i can able to create label,lineedit,pushbuttons.

    how to access lineedit's typed value.. when the user clicks a pushbutton(EX show)?
    i dont know how to get the values of WIDGETS.?

    pls kindly help me

    These are the files im using:

    DynamicControls.h
    Qt Code:
    1. //DynamicControls.h
    2. class DynamicControls : public QDialog
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. DynamicControls(QWidget *parent = 0);
    8. QGridLayout *layout;
    9. QLabel *label;
    10. QLineEdit *lineEdit;
    11. QLineEdit *pushbutton;
    12.  
    13. QString tableName;
    14. void createLabel(QString Wstr,QString WTip ,int x,int y);
    15. void createLineEdit(QString Wstr,QString WTip ,int x,int y);
    16. void createLayout();
    17. //void getInfoLineEdit(QString LineEditName);
    18. private slots:
    19. void updateTable();
    20. private:
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 


    DynamicControls.cpp
    Qt Code:
    1. //DynamicControls.cpp
    2. #include <QtGui>
    3. #include <QSqlQuery>
    4. #include <QVariant>
    5.  
    6. #include "dynamiccontrols.h"
    7.  
    8. DynamicControls::DynamicControls(QWidget *parent)
    9. : QDialog(parent)
    10. {
    11. layout = new QGridLayout;
    12. }
    13.  
    14. void DynamicControls::createLabel(QString Wstr,QString WTip,int x,int y)
    15. {
    16. label=new QLabel(Wstr);
    17. label->setToolTip(WTip);
    18. label->setObjectName(WTip);
    19. layout->addWidget(label,x,y);
    20. }
    21.  
    22. void DynamicControls::createLineEdit(QString Wstr,QString WTip,int x,int y)
    23. {
    24. lineEdit=new QLineEdit(Wstr);
    25. lineEdit->setToolTip(WTip);
    26. lineEdit->setObjectName(WTip);
    27. layout->addWidget(lineEdit,x,y);
    28. }
    29.  
    30. void DynamicControls::createPushButton(QString Wstr,QString WTip,int x,int y)
    31. {
    32. pushbutton= new QPushButton(Wstr);
    33. pushbutton->setToolTip(WTip);
    34. pushbutton->setObjectName(WTip);
    35. layout->addWidget(pushbutton,x,y);
    36. }
    37. void DynamicControls::createLayout()
    38. {
    39. setLayout(layout);
    40. }
    To copy to clipboard, switch view to plain text mode 



    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QVariant>
    3. #include <QLineEdit>
    4. #include "dynamiccontrols.h"
    5. #include "Connection.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. DynamicControls *w = new DynamicControls;
    11. w->createLabel("Id","IDLabel",0,0);
    12. w->createLineEdit("","IDLineEdit",1,);
    13. w->createLabel("Name","NameLabel",0,0);
    14. w->createLineEdit("","NameLineEdit",1,);
    15. w->createPushButton("Show","ShowButton",2,0);
    16. w->createLayout();
    17. w->show();
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    in the above program, i want to get the values of lineedits namelinedit,idlineedit when the user click show button

    Thnks

    Thnks
    Bala
    Last edited by BalaQT; 21st August 2009 at 17:05.

  2. #2
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: how to access widgets values?

    Use the method text() for QLineEdit.
    Also why are you doing the initializations in main.cpp and not in the class constructor??

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to access widgets values?

    With lineedit.text() I'm getting only the , latest lineedit's value.
    but im using two lineedits.
    how can i access both?

    im using in main because,
    i want to create dynamic line edits frm database
    the folowing code will be replaced..
    where q is the sqlquery with attributes for creating LINE EDIT.

    OLD CODE:
    w->createLineEdit("","NameLineEdit",1,1);

    NEW CODE:
    w->createLineEdit(q.value(1).toString(),q.value(1).t oString(),1,1);

    HOW CAN I ACCESS ALL THE LINEEDITS VALUE?

    bala

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to access widgets values?

    Quote Originally Posted by BalaQT View Post
    HOW CAN I ACCESS ALL THE LINEEDITS VALUE?
    Don't shout; it's rude.

    Read something about C++ programming and how to store values and keep references.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to access widgets values?

    Quote Originally Posted by BalaQT View Post
    HOW CAN I ACCESS ALL THE LINEEDITS VALUE?
    In class DynamicControls: give every line edit an id, and create a getter function ala return getLineDcit(id)->text();.

Similar Threads

  1. Mac OS X Top-Level Transparent Widgets
    By tinsuke in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 16:01
  2. Upper limit on number of widgets?
    By jdiewald in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2008, 23:00
  3. Access to widgets from a Dialog to another one
    By jean in forum Qt Programming
    Replies: 7
    Last Post: 7th August 2008, 09:42
  4. Replies: 2
    Last Post: 16th May 2008, 14:39
  5. Access widgets
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 17th November 2006, 14:37

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.