The single Inheritance Approach
Hi everybody :)
I have created a widget with the designer. Now i am trying to subclass it like the doc, i get following error by compiling. Can somebody see the problem? :confused:
main.cpp
Code:
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
Dialog dialog;
dialog.show();
return app.exec();
}
dialog.h
Code:
#include "ui_widget1.h"
{
Q_OBJECT
public:
private:
Ui::Dialog ui;
};
dialog.cpp
Code:
#include "dialog.h"
{
ui.setupUi(this);
}
ui_widget1.h
Code:
#ifndef UI_WIDGET1_H
#define UI_WIDGET1_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>
class Ui_Widget1
{
public:
{
Widget1
->setObjectName
(QString::fromUtf8("Widget1"));
Widget1
->resize
(QSize(181,
101).
expandedTo(Widget1
->minimumSizeHint
()));
btn1
->setObjectName
(QString::fromUtf8("btn1"));
btn1
->setGeometry
(QRect(50,
40,
75,
24));
btn1_2
->setObjectName
(QString::fromUtf8("btn1_2"));
btn1_2
->setGeometry
(QRect(100,
70,
75,
24));
retranslateUi(Widget1);
} // setupUi
void retranslateUi
(QWidget *Widget1
) {
Q_UNUSED(Widget1);
} // retranslateUi
};
namespace Ui {
class Widget1: public Ui_Widget1 {};
} // namespace Ui
#endif // UI_WIDGET1_H
ERROR:
Code:
dialog.
h:5: error
: invalid use of undefined type `
struct QDialog'D:/apps/Qt/4.1.3/include/QtGui/../../src/gui/kernel/qwindowdefs.h:38: error: for
ward declaration of `struct QDialog'
dialog.h:12: error: using-declaration for non-member at class scope
dialog.h:12: error: expected `;' before "ui"
In file included from dialog.cpp:1:
dialog.h:13:3: warning: no newline at end of file
dialog.cpp: In constructor `Dialog::Dialog(QWidget*)':
dialog.
cpp:4: error
: type `
struct QDialog' is not a direct base of `Dialog'dialog.cpp:6: error: `ui' undeclared (first use this function)
dialog.cpp:6: error: (Each undeclared identifier is reported only once for each
function it appears in.)
dialog.cpp:8:2: warning: no newline at end of file
mingw32-make[1]: *** [release\dialog.o] Error 1
mingw32-make[1]: Leaving directory `W:/Data/qt4/einstieg/widget'
mingw32-make: *** [release] Error 2
Thank you :)
Re: The single Inheritance Approach
You designed a widget in the designer, but are using the Ui for a dialog. It works this way, because QDialog inherits QWidget, but more appropriate way is to design a dialog for a dialog, a widget for a widget and a mainwindow for a mainwindow.
Code:
// dialog.h
#include "ui_widget1.h"
// you must include this because you designed a widget in the designer
#include <QDialog>
{
Q_OBJECT
public:
private:
Ui::Widget1 ui; // corrected the name, check the object name property in the designer
};
Re: The single Inheritance Approach
Hi JPN!!
Thanks it works. I will design a dialog for a dialog.. :D