PDA

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



davidm71
24th April 2017, 02:22
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:



//mybox.h
#ifndef MYBOX_H
#define MYBOX_H

#include <QWidget>

class mybox : public QWidget
{
public:
mybox(QWidget *parent = 0);


};

#endif // MYBOX_H

---------------------------------------------------------
//mybox.cpp

#include "mybox.h"

mybox::mybox(QWidget *parent):QWidget(parent)
{

}



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



class Ui_MainWindow
{
public:
QWidget *centralWidget;
myBox *myBoxWidget;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;

void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QStringLiteral("MainWindow"));
MainWindow->resize(400, 300);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
myBoxWidget = new myBox(centralWidget);
myBoxWidget->setObjectName(QStringLiteral("myBoxWidget"));
myBoxWidget->setGeometry(QRect(110, 40, 181, 141));
MainWindow->setCentralWidget(centralWidget);


Red exclamation marks all over myBoxWidget..


What am I doing wrong??

Thanks

high_flyer
24th April 2017, 15:17
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.

davidm71
24th April 2017, 18:31
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.

high_flyer
24th April 2017, 21:28
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!)

davidm71
24th April 2017, 22:47
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.

d_stranz
26th April 2017, 17:12
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.

davidm71
26th April 2017, 18:46
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/68168-setWindowstate()-not-working-after-moving-window-when-maximized?p=298736#post298736

Thanks