Important: Please read the Qt Code of Conduct -


  • I have an application whose centralWidget is a QMdiArea and I am trying to add a new subwindow to the QMdiArea whenever someone clicks a button. The sample code I used to implement it is shown below. I see that the addSubWindow call adds new subwindows when I call it from the constructor of the MainWindow class but fails to show a new subwindow when I call it from the openNewTab slot. I am sure(using print statements) that addSubWindow method is called from openNewTab slot. I read the docs multiple times and it looks like this should work, but it doesn't. I am definitely doing missing something. Any pointers?

    I am using latest Qt on a Macbook pro.

    class MyMdiArea : public QMdiArea {

    void addSubWindow() {
    widget = new QWidget();

    class MainWindow : public QMainWindow
    myMdiArea = new MyMdiArea(this);
    myMdiArea->addSubWindow(); //works
    myMdiArea->addSubWindow(); //works

       QToolBar *toolBar = addToolBar(tr("Navigation"));                                                
       QAction* newTabAction = new QAction(tr("&New Tab"), this);                                       
       newTabAction->setStatusTip(tr("Open a new Tab in this workspace"));                              
       connect(newTabAction, SIGNAL(triggered()), SLOT(openNewTab()));                                  

    void OpenNewTab() {
    myMdiArea->addSubWindow(); // doesn't work

  • Hello,
    By default subWindow are hidden. Don't know why !
    So the modification is easy :
    @ //slot
    void OpenNewTab() {
    QMdiSubWindow * pSubWindow= myMdiArea->addSubWindow();

  • All widgets that you create need to be shown at some point. For your main window, you usually do that in your main() function. All child widgets automatically also get shown at that point. But for widgets you create later on in the life of the program, you need to do that explicitly.

  • Thanks, I think that it's for performance purpose.

  • Thanks laumaya.

Log in to reply