Originally Posted by
stampede
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
ui.setupUi(&d);
d.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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:
{
if (Dialog->objectName().isEmpty())
Dialog
->setObjectName
(QString::fromUtf8("Dialog"));
Dialog->resize(400, 300);
buttonBox
->setObjectName
(QString::fromUtf8("buttonBox"));
buttonBox
->setGeometry
(QRect(30,
240,
341,
32));
buttonBox->setOrientation(Qt::Horizontal);
retranslateUi(Dialog);
QObject::connect(buttonBox,
SIGNAL(accepted
()), Dialog,
SLOT(accept
()));
QObject::connect(buttonBox,
SIGNAL(rejected
()), Dialog,
SLOT(reject
()));
} // setupUi
void retranslateUi
(QDialog *Dialog
) {
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_TEST_H
#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|QDialogButtonBox::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
To copy to clipboard, switch view to plain text mode
and I add it to my MainWindow.h
#include "ui_test.h"
#include "ui_test.h"
To copy to clipboard, switch view to plain text mode
As you can see in ui_test.h file, I cannot use the code like below:
Ui::TestDialog ui;
Ui::TestDialog ui;
To copy to clipboard, switch view to plain text mode
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'
'Ui_Dialog' is not a member of 'Ui'
To copy to clipboard, switch view to plain text mode
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.
Bookmarks