PDA

View Full Version : how to access widgets values?



BalaQT
21st August 2009, 18:00
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


//DynamicControls.h
class DynamicControls : public QDialog
{
Q_OBJECT

public:
DynamicControls(QWidget *parent = 0);
QGridLayout *layout;
QLabel *label;
QLineEdit *lineEdit;
QLineEdit *pushbutton;

QString tableName;
void createLabel(QString Wstr,QString WTip ,int x,int y);
void createLineEdit(QString Wstr,QString WTip ,int x,int y);
void createLayout();
//void getInfoLineEdit(QString LineEditName);
private slots:
void updateTable();
private:

};



DynamicControls.cpp



//DynamicControls.cpp
#include <QtGui>
#include <QSqlQuery>
#include <QVariant>

#include "dynamiccontrols.h"

DynamicControls::DynamicControls(QWidget *parent)
: QDialog(parent)
{
layout = new QGridLayout;
}

void DynamicControls::createLabel(QString Wstr,QString WTip,int x,int y)
{
label=new QLabel(Wstr);
label->setToolTip(WTip);
label->setObjectName(WTip);
layout->addWidget(label,x,y);
}

void DynamicControls::createLineEdit(QString Wstr,QString WTip,int x,int y)
{
lineEdit=new QLineEdit(Wstr);
lineEdit->setToolTip(WTip);
lineEdit->setObjectName(WTip);
layout->addWidget(lineEdit,x,y);
}

void DynamicControls::createPushButton(QString Wstr,QString WTip,int x,int y)
{
pushbutton= new QPushButton(Wstr);
pushbutton->setToolTip(WTip);
pushbutton->setObjectName(WTip);
layout->addWidget(pushbutton,x,y);
}
void DynamicControls::createLayout()
{
setLayout(layout);
}






main.cpp



#include <QtGui/QApplication>
#include <QVariant>
#include <QLineEdit>
#include "dynamiccontrols.h"
#include "Connection.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DynamicControls *w = new DynamicControls;
w->createLabel("Id","IDLabel",0,0);
w->createLineEdit("","IDLineEdit",1,);
w->createLabel("Name","NameLabel",0,0);
w->createLineEdit("","NameLineEdit",1,);
w->createPushButton("Show","ShowButton",2,0);
w->createLayout();
w->show();
return a.exec();
}



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

Thnks

Thnks
Bala

Archimedes
21st August 2009, 18:30
Use the method text() for QLineEdit.
Also why are you doing the initializations in main.cpp and not in the class constructor??

BalaQT
22nd August 2009, 12:16
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).to String(),1,1);

HOW CAN I ACCESS ALL THE LINEEDITS VALUE?

bala

franz
22nd August 2009, 13:03
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.

Lykurg
22nd August 2009, 13:06
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();.