Results 1 to 9 of 9

Thread: QMainWindow as a child of QMainWindow

  1. #1
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QMainWindow as a child of QMainWindow

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow as a child of QMainWindow

    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...)
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow as a child of QMainWindow

    Quote Originally Posted by jpn View Post
    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:

    Qt Code:
    1. QMainWindowTest::QMainWindowTest(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QMainWindow* w = new QMainWindow(this);
    7. w->addDockWidget(Qt::LeftDockWidgetArea, new QDockWidget(""));
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow as a child of QMainWindow

    You're not adding the "child main window" to any layout.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow as a child of QMainWindow

    Quote Originally Posted by jpn View Post
    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:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QHBoxLayout>
    3. #include <QMainWindow>
    4. #include <QDockWidget>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. w.setLayout(l);
    10.  
    11. m->addDockWidget(Qt::RightDockWidgetArea, d);
    12.  
    13. l->addWidget(m);
    14.  
    15.  
    16. w.show();
    17. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 


    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow as a child of QMainWindow

    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.
    J-P Nurmi

  7. #7
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow as a child of QMainWindow

    I faced the same problem, but seems solved thanks to this page :

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QDockWidget>
    4. #include <QTextEdit>
    5. #include <QToolBar>
    6. #include <QAction>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11.  
    12. // Application QMainWindow
    13. QMainWindow *mainWindow = new QMainWindow;
    14. mainWindow->setWindowTitle("MainWindow") ;
    15. mainWindow->setCentralWidget(new QTextEdit("Hello Main World!")) ;
    16. QDockWidget *dock = new QDockWidget;
    17. mainWindow->addDockWidget(Qt::RightDockWidgetArea, dock);
    18. QToolBar *toolbar1 = new QToolBar ;
    19. toolbar1->addAction(new QAction("FirstOne",0)) ;
    20. mainWindow->addToolBar(toolbar1) ;
    21. QToolBar *toolbar2 = new QToolBar ;
    22. toolbar2->addAction(new QAction("SecondOne",0)) ;
    23. mainWindow->addToolBar(toolbar2) ;
    24.  
    25. // another QMainWindow you want to embed
    26. QMainWindow *subMainWindow = new QMainWindow(0); // no parent
    27. subMainWindow->setWindowTitle("sub-mainwindow") ;
    28. subMainWindow->setCentralWidget(new QTextEdit("Hello Submain World!")) ;
    29. QToolBar *toolbar3 = new QToolBar ;
    30. toolbar3->addAction(new QAction("ThirdOne",0)) ;
    31. subMainWindow->addToolBar(toolbar3) ;
    32.  
    33. // embed :
    34. dock->setWidget(subMainWindow) ;
    35. subMainWindow->setParent(dock) ; // here
    36.  
    37. mainWindow->show();
    38.  
    39. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    40.  
    41. return a.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow as a child of QMainWindow

    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.

  9. #9
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow as a child of QMainWindow

    Maybe you're right yes I did not get his point in first posts

    I was actually referring to his answer in post #5

    Quote Originally Posted by Elder Orb
    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

Similar Threads

  1. QMainWindow child of a QDialog
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2008, 07:16
  2. Replies: 14
    Last Post: 19th March 2007, 08:48
  3. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.