Take the 2-minute tour ×

I'd like to place QMainWindow instance inside another QWidget (for example centralWidget of another QMainWindow).

I'm wondering why it doesn't work ? QMainWindow inherits directly from QWidget. Placeing QWidget inside another QWidget works fine.

I often place QMainWindow instances in QTabBar without any problems.

ps You may ask why do I need to use QMainWindow ? I want to place 2 widgets inside 1 form using vertical layout. I want both widgets to have seperate Toolbars directly over them.

Maybe there is some other way to place toolbars inside plain QWidgets using QtCreator ?


First example (works fine)

I create new class/form based on QWidget. (QtCreator creates 3 files *.cpp, *.h and *.ui based on standard templates).

Class declaration looks like this

class NotesEditor : public QWidget

    explicit NotesEditor(QWidget *parent = 0);

    Ui::NotesEditor *ui;

When I try to use this widget and place it inside another widget it works fine. I used "promote to ..." feature of qtcreator - no problems here.

Second example (doesn't work)

I create new class/form based on QMainWindow. (QtCreator creates 3 files *.cpp, *.h and *.ui based on standard templates).

Class declaration looks like this:

class Notes : public QMainWindow

    explicit Notes(QWidget *parent = 0);

    Ui::Notes *ui;

And now when I try to place this widget in another widget its not visible. Same as before I used "promote to ..." feature of qtcreator.

Both widgets (first based on QWidget, second based on QMainWindow) have the same default structure based on standard qtcreator code templates. I didn't change much here - just added some buttons in the form designer.

In the second example I tried to use setEnabled(true) and setVisible(true) on the class instance. The first one gives no results. The second one opens this widget in seperate window.

I think that the big question is what probibits QMainWindow to be nested inside another QWidget. As I wrote before QMainWindow instances can be placed inside QTabWidgets without any problems.

share|improve this question
Can you elaborate what happens exactly. When you say "it does not work" what happens actually ? Does it give an error or simply nothing shows up ? –  O.C. Jun 15 '11 at 6:11
I provided further description. There no erros, nothing shows up in the second example. –  user666491 Jun 15 '11 at 13:47

3 Answers 3

centralwidget = new QMainWindow(this);

This should help.

share|improve this answer

Having the same problem, I found the solution here.

QMainWindow sets its window type to Qt::Window so that it will be in an independent window even if it has a parent (you can confirm this by calling show() on your QMainWindow, and you will see it in its own window). Try adding the line


after you construct the QMainWindow.

share|improve this answer

QMainWindow provides predefined stuff like toolbars and status bars and menu bars in a platform agnostic way (it "does the right thing", without manual intervention). If all you need is a bunch of buttons in a layout, use QWidget.

You need to make sure each QMainWindow has centralQWidget`, other than that, you should be fine.

share|improve this answer
I know what QMainWindow is. This post doesn't answer my question at all. –  user666491 Jun 14 '11 at 15:15
Well, if your question is "I'm wondering why it doesn't work ?", I tried to answer as precise as the question itself is. There's only one thing that can prevent you from doing what you are trying to do: no central widget. You can create your own layout with toolbars inside each tab of the QTabBar. Specify what you're after, and at least provide some code and/or error messages. I can't read your mind. –  rubenvb Jun 14 '11 at 15:50
I provided further description of the problem. I thought that my question was straightforward. There are no errors, its very simple. Anyway thanks for help :) –  user666491 Jun 15 '11 at 13:46

Your Answer

