PDA

View Full Version : Multiple form object creation



kpmsivachand
2nd February 2009, 06:44
Hello guys!

In my project i need to invoke mutiple ui files. I tried QWizard, but it leads to fail. So i used QUiLoader. Consider my example, ( I am using the QT-Designer 4.4 for creating ui )

testform.h


class form1: public QWidget
{
....
....
....
};

class form2: public QWidget
{
....
....
....
};

testform.cpp


form1::form1(QWidget *parent): QWidget(parent)
{
QUiLoader loader;

QFile file(":/form1.ui");
file.open(QFile::ReadOnly);
QWidget *formWidget = loader.load(&file, this);
file.close();

ui_inputSpinBox1 = qFindChild<QSpinBox*>(this, "inputSpinBox1");

QMetaObject::connectSlotsByName(this);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(formWidget);
setLayout(layout);
}

testform::testform(QWidget *parent): QWidget(parent)
{
QMessageBox::information(this,tr("test"),tr("test"),QMessageBox::Ok);
QUiLoader loader;

QFile file(":/testform.ui");
file.open(QFile::ReadOnly);
QWidget *formWidget = loader.load(&file, this);
file.close();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(formWidget);
setLayout(layout);
}

void form1::on_inputSpinBox1_valueChanged(int value)
{
this->hide(); // form1 hide method called successfully
form2 form2_object;
form2.show(); // This object created and as soon as closed
}

main.cpp


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
form1 form1_object;
form1.show();
return app.exec();
}

My question is how to create a object for form2 from form1 method. Or else how to invoke
the form2 object from form1 method. Please help me guys.... Sorry for
such a lengthy thread.... Any hint can be Appreciated.

Advance thanks!

wysota
2nd February 2009, 16:30
Include "form2.h" from "form1.cpp" and use "new form2(...)" to create the widget. It's just plain C++ you know...

kpmsivachand
3rd February 2009, 02:09
Thanks for your reply... Let me try out... :)