Search code examples
c++qtqt-creatorqt5mdichild

Custom UI for child window in Qt


I've been doing some test with Qt 5 using C++ and I saw a lot of potential! But I came to a big wall which I haven't been able to pass yet.

I want to make a widnow that contains other windows (MDI), and I follow some of the tutorials online but the window is created by code, and I don't know how to "link" it to an UI file that I already design.

I follow the MDI tutorial that comes with Qt Creator, it works fine, but as I said before it doesn't work with custom UIs. And I found this other one that is exactly what I want, I follow it and it doesn't work I also downloaded the source code from the example, run it and still doesn't work. It opens a window with nothing in it.

If some one has an example, a good tutorial or a book that comes with the right info, I'll appreciate it.

Thanks a lot for taking the time to read this.


Solution

  • You should place a QMdiArea widget on your window. It provides an area in which MDI windows are displayed. It can be done via designer.

    All your subwindows should inherit QMdiSubWindow which provides a subwindow class for QMdiArea :

    class MyWindow : public QMdiSubWindow
    {
        Q_OBJECT
    
    public:
        explicit MyWindow(QWidget *parent = 0);
        ~MyWindow();
    };
    

    After creating a custom subwindow you can add it to the MDI area like:

    MyWindow * subWindow = new MyWindow(ui->mdiArea);
    MyWindow->show();
    

    You can also add subWindows in designer by right clicking on the MDI area and choosing :

    "Add subWindow"

    This will add a subwindow which is viewable in designer. You can also add arbitrary widgets to the added subwindows in designer this way.