PDA

View Full Version : [SOLVED] How to use Ui forms as a simple Dialog?



tofighi
15th July 2011, 20:48
In the Qt Creator I have two options to add a form:
New->Qt-> Qt Designer Form Class
New->Qt-> Qt Designer Form

I want to use Qt Designer to design an "About this software" Dialog and run it from MainWindow menu. I know how to do this via Qt Designer Form Class, but I think it doesn't need to add about.ui, about.h and about.cpp to my application for doing this.
I believe it should be possible to do such an easy job only by using

New->Qt-> Qt Designer Form

but I don't know how use ui form itself as a Dialog. Please give me some directions.

squidge
15th July 2011, 21:09
The best way would be to create the files about.cpp and about.h to manage to form and then when you want to show the dialog, create an instance of the about class and request it to show the dialog.

If you are using Qt Designer, then the header file will contain everything necessary to build the dialog and configure all appropriate signals. A bit of glue code in the CPP will use that.

Santosh Reddy
15th July 2011, 21:12
You cannot use a ui file by itself, you need a QWidget on to which you load the ui file.


class Widget : public QWidget
{
Widget(QWidget * parent = 0);
private:
Ui::form * ui;
}

Widget::Widget(QWidget * parent) : QWidget(parent), ui(new Ui::form)
{
ui->setupUi(this); //this loads the ui on this widget
}

Widget::~Widget()
{
delete ui;
}

stampede
15th July 2011, 21:27
No, you don't need to write custom class, .ui form is enough, but writing class is more reusable. If this is simple window that displays some text, you can show the dialog this way:


#include "ui_AboutDialog.h"
//...
void Main::about(){
Ui::AboutDialog ui; // depends on what name you give to the dialog in Designer
QDialog d;
ui.setupUi(&d);
d.exec();
}

tofighi
15th July 2011, 22:07
No, you don't need to write custom class, .ui form is enough, but writing class is more reusable. If this is simple window that displays some text, you can show the dialog this way:


#include "ui_AboutDialog.h"
//...
void Main::about(){
Ui::AboutDialog ui; // depends on what name you give to the dialog in Designer
QDialog d;
ui.setupUi(&d);
d.exec();
}


I tried to do it in this way. I've created test.ui using this path:

New->Qt-> Qt Designer Form > Dialog with buttons buttom
and name it test.ui instead of dialog.ui

It adds automatically ui_test.h in the build directory :



#ifndef UI_TEST_H
#define UI_TEST_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>

QT_BEGIN_NAMESPACE

class Ui_Dialog
{
public:
QDialogButtonBox *buttonBox;

void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(400, 300);
buttonBox = new QDialogButtonBox(Dialog);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setGeometry(QRect(30, 240, 341, 32));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialo gButtonBox::Ok);

retranslateUi(Dialog);
QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));

QMetaObject::connectSlotsByName(Dialog);
} // setupUi

void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEST_H


and I add it to my MainWindow.h


#include "ui_test.h"

As you can see in ui_test.h file, I cannot use the code like below:


Ui::TestDialog ui;

because there is not such class. The class is named automatically Ui_Dialog, and I don't know why it gives it this name. I also tried to compile it with this name but it generates an error:

'Ui_Dialog' is not a member of 'Ui'

There is another side effect. If I add another form test2.ui in this way, it generates the same ui_test2.h, with the same class name ( Ui_Dialog).
I'm really confused by the way Qt 4.7 acts in this part.

stampede
15th July 2011, 22:11
Because this name is automatically generated, its based on the type (class) of object you create in designer. To change the name of the generated class, use objectName field in PropertyEditor (click somewhere on main widget form to edit its properties).
Btw. why do you think the class should be named TestDialog ? Class name is always as objectName property of "main widget" in the form (the one that holds other widgets, in your case you should use Ui:: Dialog).

tofighi
15th July 2011, 22:49
Because this name is automatically generated, its based on the type (class) of object you create in designer. To change the name of the generated class, use objectName field in PropertyEditor (click somewhere on main widget form to edit its properties).
Btw. why do you think the class should be named TestDialog ? Class name is always as objectName property of "main widget" in the form (the one that holds other widgets, in your case you should use Ui:: Dialog).

Ok, I got this part. But it still generates an error. My code is:


void MainWindow::openAbout()
{
Ui::Ui_Test uiTestDlg;
QDialog d;
uiTestDlg.setupUi(&d);
d.exec();
}
and I also included ui_test.h in the MainWindow.h
But it generates this error:


'Ui_Test' is not a member of 'Ui'

When I'm typing Ui:: it shows me Ui_Test is a member of Ui in the context menu, but whe I compile it, it says me it is not its member?!!:confused::confused::confused:

stampede
15th July 2011, 22:58
Not Ui:: Ui_Test - Ui:: Test or Ui_Test, check the generated header for the class names if you are not sure. Do not rely blindly on the auto completer, its not perfect.

tofighi
15th July 2011, 23:10
Not Ui:: Ui_Test - Ui:: Test or Ui_Test, check the generated header for the class names if you are not sure. Do not rely blindly on the auto completer, its not perfect.

Your help is so great and straightforward. Thank you very much. :)