Results 1 to 7 of 7

Thread: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help..

  1. #1
    Join Date
    Apr 2017
    Posts
    21
    Qt products
    Qt5
    Platforms
    Windows

    Default Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help..

    Hi,

    Trying to learn how to subclass a widget within a MainWindow. Inside this Widget I layed out nested under centralwidget I have a label to identify the widget box I placed. I named the widget myBoxWidget and promoted it to a class called 'myBox'. So if at this stage you try to compile the app you get an error saying mybox.h not found. So I create mybox.h and mybox.cpp with this code:

    Qt Code:
    1. //mybox.h
    2. #ifndef MYBOX_H
    3. #define MYBOX_H
    4.  
    5. #include <QWidget>
    6.  
    7. class mybox : public QWidget
    8. {
    9. public:
    10. mybox(QWidget *parent = 0);
    11.  
    12.  
    13. };
    14.  
    15. #endif // MYBOX_H
    16.  
    17. ---------------------------------------------------------
    18. //mybox.cpp
    19.  
    20. #include "mybox.h"
    21.  
    22. mybox::mybox(QWidget *parent):QWidget(parent)
    23. {
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    Then I try to compile this and get multiple errors like:
    missing ';' before '*'
    and alot more errors pointing to ui_mainwindow.h

    Qt Code:
    1. class Ui_MainWindow
    2. {
    3. public:
    4. QWidget *centralWidget;
    5. myBox *myBoxWidget;
    6. QMenuBar *menuBar;
    7. QToolBar *mainToolBar;
    8. QStatusBar *statusBar;
    9.  
    10. void setupUi(QMainWindow *MainWindow)
    11. {
    12. if (MainWindow->objectName().isEmpty())
    13. MainWindow->setObjectName(QStringLiteral("MainWindow"));
    14. MainWindow->resize(400, 300);
    15. centralWidget = new QWidget(MainWindow);
    16. centralWidget->setObjectName(QStringLiteral("centralWidget"));
    17. myBoxWidget = new myBox(centralWidget);
    18. myBoxWidget->setObjectName(QStringLiteral("myBoxWidget"));
    19. myBoxWidget->setGeometry(QRect(110, 40, 181, 141));
    20. MainWindow->setCentralWidget(centralWidget);
    To copy to clipboard, switch view to plain text mode 

    Red exclamation marks all over myBoxWidget..


    What am I doing wrong??

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    What am I doing wrong??
    Probably a lot :-)

    The first obvious thing is that you forgot the Q_OBJECT macro in your mybox.

    Then things to look at while you are at it:
    Your Ui_MainWindow needs to know where your myBox header is - make sure it is so.
    If this checks right and you still get errors, post them here.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2017
    Posts
    21
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    Hi,

    I actually tried Q_OBJECT macro but that did not work and was the first thing I tried. Also QT_Community IDE automatically placed the name of the mybox.h as an include in the ui_mybox.h file. Noticed that as long as I do not promote the widget to a custom class name I can compile the project with out issue. The problem is once I promote the file that happens. Also I am pretty sure my work environment is stable as I am able to compile other demo projects and run them.

    Thanks

    PS: Will read up on those links you sent me. Thank you.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    The Q_OBJECT macro is needed for the signals and slots which at the moment you don't seem to use, to this for now should not be a problem, but you should put it in there because you will need it later.
    Do you understand what "promoting" in designer actually means?
    You inability to build is almost certainly a configuration issue, but to tell for sure one needs to see the errors you get.

    (The links are in my signature, they were not meant specifically for you, but I do recommend them!)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2017
    Posts
    21
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    I guess I don't write clean code. I am feeling really stupid right now as all it was was the wrong case of the letter 'B' in Box not being captilized or uppercase...

    Shame on me..


    Thanks.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    I am feeling really stupid right now as all it was was the wrong case of the letter 'B' in Box not being captilized or uppercase...
    Nah, we've all been there and done that. Multiple times. Most of us forget how to count from zero from time to time, too.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Apr 2017
    Posts
    21
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie Error trying to subclass a QWidget from QTCreator's UI not Working.. Help.

    Sometimes I forget to think... Anyhow I have another mind bender of a problem if anyone can take a look:
    http://www.qtcentre.org/threads/6816...736#post298736

    Thanks

Similar Threads

  1. how to subclass a qwidget in ui file
    By newcfd in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2015, 20:14
  2. Help Newbie Subclass QStringList
    By avanta27 in forum Newbie
    Replies: 1
    Last Post: 8th September 2012, 04:53
  3. When is the geometry of a QWidget subclass set?
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2011, 21:14
  4. Replies: 3
    Last Post: 3rd November 2010, 17:30
  5. QtCreator 2.0 crashes application working in QtCreator 1.3
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2010, 12:58

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.