PDA

View Full Version : Transition from QMainWindow to QWidget



Sabre Runner
12th October 2010, 13:32
I have a strange problem. I've built an application in a QMainWindow class. But now I need to rebuild it into something that will share the main window with others. So I've built a new QMainWindow class that will house everything. window.h file:

#ifndef WINDOW_H
#define WINDOW_H

#include <QMainWindow>
#include "widget.h"

namespace Ui {
class window;
}

class window : public QMainWindow
{
Q_OBJECT

public:
explicit window(QWidget *parent = 0);
~window();

private:
Ui::window *ui;
};

#endif // WINDOW_HAnd then I've tried to rewrite the old main windows as a QWidget and have the window show it. widget.h file:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class widget;
}

class widget : public QWidget
{
Q_OBJECT

public:
explicit widget(QWidget *parent = 0);
~widget();

private:
Ui::widget *ui;
};

#endif // WIDGET_HThis the code I use minus the internal methods. And the compilation error is on first line of the widget constructor:

widget::widget(QWidget *parent) : QWidget(parent), ui(new Ui::widget)Which says "invalid use of incomplete type 'struct Ui::widget'".

Now, I've tried looking it up and I've found that this kind of error usually occurs through recursive definitions and integrations where by the time the compiler is doing the line in error, it needs definitions that won't become available until later on or even inside the class.
Following the instructions on building an example, I did manage to write pretty much the same thing from scratch with, as far as I can see, the same code as the one I pasted and it works perfectly.

So, my question is, did anyone already try doing what I did and can tell me what I'm doing wrong or any suggestions on how I can do it without writing the whole thing from scratch? Because right now it seems like I have to and I'd like to avoid it.

Proper notice: I am a newbie to Qt and am still using a lot of automatics like the Designer, the automatic slot connection and the likes...

tbscope
12th October 2010, 14:42
Does your widget have a .ui file too?
Then, at least, you need to include ui_widget.h (or whatever it is called) in your widget cpp file.

By the way, it is also a good idea if you're learning Qt and don't have any experience with C++ to have a good C++ beginners book around.

Sabre Runner
12th October 2010, 14:50
the ui header file is also included in the code file.
I am a kind of novice at C++. I've written terminal applications for various uses a couple of years now. This is my first foray into a GUI and actual use application.
But yes, that is good advice. I'll bring my book with me next time.

Other than that, you have no idea why this might happen?

tbscope
12th October 2010, 14:58
Other than that, you have no idea why this might happen?
Yes, I do know why you get that error.
The error means it can't find the definition of Ui::widget. This is defined in a file called ui_widget.h. It gets automatically generated when you build your program. You don't have to worry about the compiler not finding it on time, this is all taken care of by qmake.

You either didn't include the correct file, it didn't get generated or you didn't include that file at all.
With the information you have given already, it is not possible to tell.

Can you post the following files please?
your .pro file
and the .cpp file of the widget.

Sabre Runner
12th October 2010, 15:15
Actually, now that you mention it. I think I know where the problem is. It's probably the modifications I forgot to make inside the UI file when I changed it.

Be right back. :)

Sabre Runner
12th October 2010, 16:05
Yes, I forgot the UI file in the transition from a QMainWindow class to a QWidget class.