PDA

View Full Version : The single Inheritance Approach



raphaelf
1st June 2006, 08:44
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


#include <QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Dialog dialog;
dialog.show();
return app.exec();
}


dialog.h


#include "ui_widget1.h"


class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);

private:
Ui::Dialog ui;
};


dialog.cpp


#include "dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

}


ui_widget1.h


#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:
QPushButton *btn1;
QPushButton *btn1_2;

void setupUi(QWidget *Widget1)
{
Widget1->setObjectName(QString::fromUtf8("Widget1"));
Widget1->resize(QSize(181, 101).expandedTo(Widget1->minimumSizeHint()));
btn1 = new QPushButton(Widget1);
btn1->setObjectName(QString::fromUtf8("btn1"));
btn1->setGeometry(QRect(50, 40, 75, 24));
btn1_2 = new QPushButton(Widget1);
btn1_2->setObjectName(QString::fromUtf8("btn1_2"));
btn1_2->setGeometry(QRect(100, 70, 75, 24));
retranslateUi(Widget1);

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

void retranslateUi(QWidget *Widget1)
{
Widget1->setWindowTitle(QApplication::translate("Widget1", "Ich bin ein Widget", 0, QApplication::UnicodeUTF8));
btn1->setText(QApplication::translate("Widget1", "Show Message", 0, QApplication::UnicodeUTF8));
btn1_2->setText(QApplication::translate("Widget1", "Show Message", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Widget1);
} // retranslateUi

};

namespace Ui {
class Widget1: public Ui_Widget1 {};
} // namespace Ui

#endif // UI_WIDGET1_H



ERROR:


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 :)

jpn
1st June 2006, 09:13
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.



// dialog.h
#include "ui_widget1.h"
// you must include this because you designed a widget in the designer
#include <QDialog>

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);

private:
Ui::Widget1 ui; // corrected the name, check the object name property in the designer
};

raphaelf
2nd June 2006, 08:51
Hi JPN!!
Thanks it works. I will design a dialog for a dialog.. :D