PDA

View Full Version : Dynamic FOrms



BalaQT
14th August 2009, 05:47
hi,
im new to QT programming,
i want to create DYANMIC form based on DATABASE
for ex:
sqlite table contains how many forms to create and size of the form
QT should connect sqlite table and it should create the forms

pls guide me

Bala

wysota
14th August 2009, 07:39
So what's the question?

BalaQT
14th August 2009, 10:01
wysota> Thnks for replying me
my question is :
i) how to create forms from QT by reading from SQLITE table

following is the code im using to show one window.


#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QTextBox>
#include <QProcess>
#include "Connection.h"
#include <QtCore/QVariant>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *win1 = new QWidget;
QGridLayout *layout = new QGridLayout;
QPushButton *ClickButton=new QPushButton("Click");
QPushButton *CancelButton=new QPushButton("Cancel");
QObject::connect(ClickButton,SIGNAL(clicked()),&app,SLOT(quit()));
if(!createConnection())
return 1;
QSqlQuery q;
q.exec("SELECT * FROM ConfigTable");
q.next();
win1->setWindowTitle("Window1");
win1->setFixedHeight(q.value(2).toInt());
win1->setFixedWidth(q.value(3).toInt());
layout->addWidget(ClickButton,0,0);
layout->addWidget(CancelButton,0,2);
win1->setLayout(layout);
win1->show();
return app.exec();
}



How to create more than one windows, if the ConfigTable contain 2 or more records?

Thnks
Bala

wysota
14th August 2009, 10:41
I don't see a problem... just use a for() loop and in each iteration create a widget, fill it with child widgets and show it. Alternatively you can use QUiLoader if you can afford storing ui files in the database.

BalaQT
14th August 2009, 10:58
Sir, thnks for the reply,
i got the logic. but dont know how to do it in QT coding. pls provide me source code with for loop to create child widgets

Thnks
Bala

wysota
14th August 2009, 11:16
How can I provide code if I don't know what the database holds?

BalaQT
14th August 2009, 11:24
Table Details
Database : SQLITE
Table Name : ConfigTable
Fields :
FormId Numeric
FormName Text
FormHeight Numeric
FormWidth Numeric

ConfigTable location : E:\Bala\Ramana\DynamicForm\DynaBase

connection.h


#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//db.setDatabaseName(":memory:");
//db.setDatabaseName("e:/BALA/hirola");
db.setDatabaseName("e:/BALA/Ramana/DynamicForm/DynaBase");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}


return true;
}
#endif


DynamicTable.Cpp


#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QProcess>
#include "Connection.h"
#include <QtCore/QVariant>


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *win1 = new QWidget;
QGridLayout *layout = new QGridLayout;
QPushButton *ClickButton=new QPushButton("Click");
QPushButton *CancelButton=new QPushButton("Cancel");
QObject::connect(ClickButton,SIGNAL(clicked()),&app,SLOT(quit()));
if(!createConnection())
return 1;
QSqlQuery q;
q.exec("SELECT * FROM ConfigTable");
QString salary;
q.next();
win1->setWindowTitle("Window1");
win1->setFixedHeight(q.value(2).toInt()); //FormHeight
win1->setFixedWidth(q.value(3).toInt()); //FormWidth
layout->addWidget(ClickButton,0,0);
layout->addWidget(CancelButton,0,2);
win1->setLayout(layout);
win1->show();
return app.exec();
}


This is working fine for ONE ROW, ONE WINDOW.
how to create n windows, if n rows are in configtable.

Thnks
Bala

wysota
14th August 2009, 11:33
while(q.next()){
QWidget *w = new QWidget;
w->setWindowTitle(...);
w->setWFixedHeight(q.value(2).toInt());
...
w->show();
}

BalaQT
14th August 2009, 11:57
Thnks wysota
it solved my problem.
im new to QT. I understand SIMPLE OR BASIC means not easy untill u familiar with it.
i love QT.but im a beginner. so pls consider me,even if i ask any dumb questions.

i)what is the command for position the forms (LEFT AND TOP)?

Thnks
Bala

numbat
14th August 2009, 12:16
w->move(x, y);

BalaQT
21st August 2009, 16:49
thanks wysota

its working fine, but i have more doubts in this

how can i access the created window's widgets?
for ex:
a form displayed with 2 line edits in it.
createLineedit is the function im using to create a lineedit.

createLineedit() will be called 2 times to create 2 lineedits;

hw can i access the values in it, when i press one pushbutton

the follwing code called frm main.cpp
createLineedit("id")
createLineedit("name");

How to take the values TYPED in id,name line edit ,when the user press Savepushbutton?

pls guide me

BalaQT
22nd August 2009, 11:19
any experts pls reply me.

My need is i want to create dynamic widgets and dynamic forms..
Im successful in creating dynamic forms and dynamic widgets, but How can i access the widget's value's?

since all widgets have same name, i can able to take the latest value only
how can i access all the values?

pls guide me

Thanks
Bala

numbat
22nd August 2009, 11:55
If you have saved the widgets in a list you can just access that list:


for (int i = 0; i < list.size(); i++)
qDebug() << list[i].text();

If you haven't saved a list, you can get one from your top-level widget. This will get all line edits which you can then iterate as above.


QList<QLineEdit *> myEdits = parentWidget.findChildren<QLineEdit *>();

BalaQT
22nd August 2009, 13:09
Thnks nimbut,
Its working.
one more doubt in connecting SIGNALS AND SLOT

For better understanding, i will explain my need.
im creating forms and lineedit,pushbutton,label DYNAMICALLY frm a DB.


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


This function will be called in CreateDynamicForm shown below:


void DynamicControls::CreateDynamicForm()
{
QSqlQuery q1,q2;
q1.exec("select * from FormMaster where FormVisible=1 order by FormId");
while(q1.next())
{
tableName=q1.value(7).toString();
q2.exec("select * from WidgetMaster where FormId=" + q1.value(0).toString() + " and WVisible=1 order by RowNo,ColumnNo");
while(q2.next())
{
if(q2.value(2).toString()=="lineedit")
{
createLineEdit(q2.value(3).toString(),q2.value(1). toString(),q2.value(6).toInt(),q2.value(7).toInt() );

else if(q2.value(2).toString()=="PushButton")
{
createPushButton(q2.value(3).toString(),q2.value(1 ).toString(),q2.value(6).toInt(),q2.value(7).toInt ());
}
}
}



The CreateDynamicForm will be called in CONSTRUCTOR
i want to know hw to connect signals
ex:
if the created buttons name is SAVE , then my own slot UPDATEtable() has to be called..
how can i do it.

since its dynamic , im getting some err in the following connect
(frankly i dont know how to connect a dynamically created button into CONNECT())
QObject::connect(pushbutton, SIGNAL(clicked()),this,SLOT(updateTable1(IndxValue 1)));

since all the pushbuttons names are same, connect not working properly.

how to create a connect for dynamically created pushbuttons...

Bala

wysota
25th August 2009, 09:17
Come on, all the time you are facing strictly C++ issues. We don't teach C++ nor basics of programming here - get a decent book on C++ to learn about pointers.

As for your last problem - you are passing a value to the slot in the connect statement which is forbidden. See the FAQ section of this site.

BalaQT
26th August 2009, 02:18
hi,
Thnks wysota, i will surely study on it. but pls help me here.

a)I've managed to create dynamic buttons.
Now I am having a problem with being able to tell which button was pressed. The button objects are created at runtime and they're added to QList. How do I find out which button in the array was clicked?

ex: 5 buttons are created frm DB. (all are having same name pushbutton)
1)new , 2) save 3)del 4)view 5)exit

i want to perform particular actions according to button click.
how can i know which button was clicked?

Any suggestions?

Thnks
Bala

nish
26th August 2009, 02:58
what is the need of sending pms if you posted your topic here and getting replies?

there is a function sender() which you can call in a slot to check which object called that slot.

QList<QPushButton *> btns;
void createbuttons()
{
btns<<new QPushButton("abc",this)<<new QPushButton("abc",this)<<new QPushButton("abc",this);
connect all these buttons to a slot myslot();
}

void myslot()
{
QObject* o = sender();
if(o==btns[0]) etc...
}

wysota
26th August 2009, 08:19
how can i know which button was clicked?

I'd suggest using QSignalMapper.