PDA

View Full Version : QMainWindow as a child of QMainWindow



Elder Orb
19th March 2008, 20:45
Is is possible at all ? I have to implement a very special layout, where only the part of main window must have ability to manage dock widgets (and it is not implementable in terms of dock areas). The problem is in crash when I trying to add dock widget to child QMainWindow. Any ideas?

jpn
19th March 2008, 20:50
Yes, it's possible. QMainWindow is not limited to one instance and it can be placed into layout like any other widget (even though it might not lead to the most intuitive user interface...)

Elder Orb
19th March 2008, 22:15
Yes, it's possible. QMainWindow is not limited to one instance and it can be placed into layout like any other widget (even though it might not lead to the most intuitive user interface...)

I've made one more sample project where I have two main window and one dock widget but for some reason I can only see the top-level mainwindow.

Here is what I do:



QMainWindowTest::QMainWindowTest(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QMainWindow* w = new QMainWindow(this);
w->addDockWidget(Qt::LeftDockWidgetArea, new QDockWidget(""));
}

jpn
20th March 2008, 05:44
You're not adding the "child main window" to any layout.

Elder Orb
20th March 2008, 18:12
You're not adding the "child main window" to any layout.

It must not be the reason. When I added for example QLineEdit the same way - I could see it. It seems second QMainWindow still doesn't have a parent. Anyway I prepared test sample, which uses layout for second QMainWindow - no luck. Here is a code:


#include <QtGui/QApplication>
#include <QHBoxLayout>
#include <QMainWindow>
#include <QDockWidget>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QHBoxLayout* l = new QHBoxLayout;
w.setLayout(l);

QMainWindow* m = new QMainWindow;
QDockWidget* d = new QDockWidget;
m->addDockWidget(Qt::RightDockWidgetArea, d);

l->addWidget(m);


w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


But if I call "m->show()" I can see second main window as a second top-level widget. Maybe I need to tweak some window flags to get second mainwindow to become a child.

jpn
20th March 2008, 18:31
You can't install a layout on QMainWindow. QMainWindow has its own special layout which contains the central widget, docks widgets, toolbars etc. Set one main window as others central widget or install the layout on a widget which you set as central widget.

totem
11th February 2011, 18:21
I faced the same problem, but seems solved thanks to this page (http://developer.qt.nokia.com/faq/answer/how_can_i_embed_e.g_a_qmainwindow_inside_a_qdialog ) :



#include <QApplication>
#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
#include <QToolBar>
#include <QAction>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

// Application QMainWindow
QMainWindow *mainWindow = new QMainWindow;
mainWindow->setWindowTitle("MainWindow") ;
mainWindow->setCentralWidget(new QTextEdit("Hello Main World!")) ;
QDockWidget *dock = new QDockWidget;
mainWindow->addDockWidget(Qt::RightDockWidgetArea, dock);
QToolBar *toolbar1 = new QToolBar ;
toolbar1->addAction(new QAction("FirstOne",0)) ;
mainWindow->addToolBar(toolbar1) ;
QToolBar *toolbar2 = new QToolBar ;
toolbar2->addAction(new QAction("SecondOne",0)) ;
mainWindow->addToolBar(toolbar2) ;

// another QMainWindow you want to embed
QMainWindow *subMainWindow = new QMainWindow(0); // no parent
subMainWindow->setWindowTitle("sub-mainwindow") ;
subMainWindow->setCentralWidget(new QTextEdit("Hello Submain World!")) ;
QToolBar *toolbar3 = new QToolBar ;
toolbar3->addAction(new QAction("ThirdOne",0)) ;
subMainWindow->addToolBar(toolbar3) ;

// embed :
dock->setWidget(subMainWindow) ;
subMainWindow->setParent(dock) ; // here

mainWindow->show();

a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));

return a.exec();
}

d_stranz
12th February 2011, 15:57
You aren't doing the same thing as the original poster. He was trying to add the new QMainWindow as a direct child of the top-level QMainWindow. You are adding it as the main widget of a dock window.

In your case, you are following all of the QMainWindow "rules" even though the effect is different. It isn't really clear what the OP intended his GUI to look like. Maybe he wanted another main window that floats or docks like yours will.

I am guessing that what he wants is what Microsoft calls an "MDI" or "multiple document interface" app, where you can have several child windows within a main window frame. The menu, toolbars, and other main window components control whichever child window has focus.

But this is a 3-year-old post, so I am guessing that he either solved his problem or gave up.

totem
12th February 2011, 21:31
Maybe you're right yes :) I did not get his point in first posts

I was actually referring to his answer in post #5


But if I call "m->show()" I can see second main window as a second top-level widget. Maybe I need to tweak some window flags to get second mainwindow to become a child.

I met the same issue, and solved it the way I precised. I only unburried this 3-years old post to help people looking for an answer because it was a problem for me, even 3 years later