Results 1 to 3 of 3

Thread: The single Inheritance Approach

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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?
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "dialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. Dialog dialog;
    9. dialog.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #include "ui_widget1.h"
    2.  
    3.  
    4. class Dialog : public QDialog
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Dialog(QWidget *parent = 0);
    10.  
    11. private:
    12. Ui::Dialog ui;
    13. };
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent)
    4. : QDialog(parent)
    5. {
    6. ui.setupUi(this);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    ui_widget1.h
    Qt Code:
    1. #ifndef UI_WIDGET1_H
    2. #define UI_WIDGET1_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QPushButton>
    9. #include <QtGui/QWidget>
    10.  
    11. class Ui_Widget1
    12. {
    13. public:
    14. QPushButton *btn1;
    15. QPushButton *btn1_2;
    16.  
    17. void setupUi(QWidget *Widget1)
    18. {
    19. Widget1->setObjectName(QString::fromUtf8("Widget1"));
    20. Widget1->resize(QSize(181, 101).expandedTo(Widget1->minimumSizeHint()));
    21. btn1 = new QPushButton(Widget1);
    22. btn1->setObjectName(QString::fromUtf8("btn1"));
    23. btn1->setGeometry(QRect(50, 40, 75, 24));
    24. btn1_2 = new QPushButton(Widget1);
    25. btn1_2->setObjectName(QString::fromUtf8("btn1_2"));
    26. btn1_2->setGeometry(QRect(100, 70, 75, 24));
    27. retranslateUi(Widget1);
    28.  
    29. QMetaObject::connectSlotsByName(Widget1);
    30. } // setupUi
    31.  
    32. void retranslateUi(QWidget *Widget1)
    33. {
    34. Widget1->setWindowTitle(QApplication::translate("Widget1", "Ich bin ein Widget", 0, QApplication::UnicodeUTF8));
    35. btn1->setText(QApplication::translate("Widget1", "Show Message", 0, QApplication::UnicodeUTF8));
    36. btn1_2->setText(QApplication::translate("Widget1", "Show Message", 0, QApplication::UnicodeUTF8));
    37. Q_UNUSED(Widget1);
    38. } // retranslateUi
    39.  
    40. };
    41.  
    42. namespace Ui {
    43. class Widget1: public Ui_Widget1 {};
    44. } // namespace Ui
    45.  
    46. #endif // UI_WIDGET1_H
    To copy to clipboard, switch view to plain text mode 

    ERROR:
    Qt Code:
    1. dialog.h:5: error: invalid use of undefined type `struct QDialog'
    2. D:/apps/Qt/4.1.3/include/QtGui/../../src/gui/kernel/qwindowdefs.h:38: error: for
    3. ward declaration of `struct QDialog'
    4. dialog.h:12: error: using-declaration for non-member at class scope
    5. dialog.h:12: error: expected `;' before "ui"
    6. In file included from dialog.cpp:1:
    7. dialog.h:13:3: warning: no newline at end of file
    8. dialog.cpp: In constructor `Dialog::Dialog(QWidget*)':
    9. dialog.cpp:4: error: type `struct QDialog' is not a direct base of `Dialog'
    10. dialog.cpp:6: error: `ui' undeclared (first use this function)
    11. dialog.cpp:6: error: (Each undeclared identifier is reported only once for each
    12. function it appears in.)
    13. dialog.cpp:8:2: warning: no newline at end of file
    14. mingw32-make[1]: *** [release\dialog.o] Error 1
    15. mingw32-make[1]: Leaving directory `W:/Data/qt4/einstieg/widget'
    16. mingw32-make: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    Thank you
    Think DigitalGasoline

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.

    Qt Code:
    1. // dialog.h
    2. #include "ui_widget1.h"
    3. // you must include this because you designed a widget in the designer
    4. #include <QDialog>
    5.  
    6. class Dialog : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Dialog(QWidget *parent = 0);
    12.  
    13. private:
    14. Ui::Widget1 ui; // corrected the name, check the object name property in the designer
    15. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The single Inheritance Approach

    Hi JPN!!
    Thanks it works. I will design a dialog for a dialog..
    Think DigitalGasoline

Similar Threads

  1. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.