PDA

View Full Version : QMainWindow setCentralWidget from ui widget, Qt4



alan
3rd May 2008, 00:35
Sorry for a question with an obvious (?) answer (seems not to be asked so far).

I would like to create a program based on QMainWindow and have created a quite complex QWidget in Qt-Designer (Qt4) which I would now like to set as the CentralWidget in the QMainWindow.

I'm using the ui.setup approach, but this gives me always the error, that a 'Ui::Xy&' is not a 'QWidget*': & != * .

Can somebody point to a simple example where a *.ui file is used to create the CentralWidget of a QMainWIndow, please?

The Qt4 example about QMainWindow does not do it for me...

Or is the correct approach to create this main widget as a QMainWindow instead of a QWidget?
(But what would then be the CentralWidget?)

(I use cmake rather than qmake, but this should not make any difference, I assume.)
The widget to be used as the CentralWidget compiles OK, so far I managed to see it outside of the QMainWindow. :rolleyes:

Help is much appreciated.

Helge

P.S.: Using Qt 4.4.0-rc1-5 on debian sid amd64, if that matters.

aamer4yu
3rd May 2008, 08:30
It shudnt be as complex as it sounds,,,,

what all u need is myWindow->setCentralWidget(myWidget);
thats it.

I'm using the ui.setup approach, but this gives me always the error, that a 'Ui::Xy&' is not a 'QWidget*': & != * .

how are u setting the widget, can we see ??

alan
3rd May 2008, 15:37
Thanks, aamer4yu, but this is what is not working.

Let's assume I have the ui file: myproj.ui which generates ui_myproj.h (and is based on the QWidget class, called Cw).

In myproj.h I have:

#include ui_myproj.h
private:
Ui::Cw uicw;
QWidget wCw;

In myproj.cpp, in the constructor, I do (myproj is a QMainWindow):

uicw.setupUi( &wCw);
setCentralWidget(uicw );

Then I get:

error: no matching function for call to 'Myproj::setCentralWidget(Ui::Cw&)'
note: candidates are: void QMainWindow::setCentralWidget(QWidget*)

It wants a pointer, not a reference ? I assume I need a pointer to uicw or wCw (the widget), but I don't know how to get it.

***** Update:
Good that we talked about it... the following code works, not sure whether it is 'good', but it works:

In myproj.h I have:

#include ui_myproj.h
private:
Ui::Cw uicw;
QWidget wCw;

In myproj.cpp, in the constructor, I do (myproj is a QMainWindow):

uicw.setupUi( &wCw);
QWidget * wCw2 = &wCw; // creating the pointer
setCentralWidget( wCw2 );

This finally sets the ui QWidget as central widget.

aamer4yu
3rd May 2008, 19:48
You could very well use -

setCentralWidget(&uicw );

couldnt you ?? &uicw is the address of the object and thats what setCentralWidget(QWidget*) needs :)

alan
3rd May 2008, 23:29
aamer4yu,

I just tried this and it produced the same error as before:

error: no matching function for call to 'Myproj::setCentralWidget(Ui::Cw*)'
note: candidates are: void QMainWindow::setCentralWidget(QWidget*)

But thanks for trying. I thought too, that creating this extra pointer was a bit strange (but would have stick with it).
Well, I'm not a good coder, so may be I made a mistake elsewhere...

But wait, (huch!),
>setCentralWidget( &wCw );
does the trick.
However, it does need the line:
>uicw.setupUi( &wCw);
before to work
Yeah, much nicer!

Thanks again !

For reference, in case somebody has the same problem: 2 lines in the constructor of myproj.cpp are enough:
>uicw.setupUi( &wCw);
>setCentralWidget( &wCw );

jpn
13th May 2008, 13:00
Do NOT allocate child widgets on the stack. QMainWindow takes ownership of the central widget and deletes it later when the main window is deleted. You will get a crash because of the double free attempt.