24/07/2019 37

How To Use MDI Area in QT C++

Hello everyone, In this article we will talk about how can we use MDI forms in QT GUI with C++ and we will make an example.

Firstly I am using QT 5.11.3 version and we will make our example in this version.Now I am ready to start the article.

First I want to say what MDI does ?

A Multiple Document Interface (MDI) program is to display multiple subwindows in parent window. This is not like single document interface (SDI) applications that can only remove one document at a time. Any window can be MDI window parent. And parent window can show subwindows what we want.

Now lets get started to use mdi screen on QT C++ GUI program.

In our project's pro file make sure widgets library is added.


QT += widgets

After this we need to insert an MDI Area object under Containers to our window:


QT GUI MDI AREA PANEL

After insertation the mdi area now we need to put our sub forms inside this mdi area. Now lets do this.

First we need to include our forms classes in header


#include "form1.h"
#include "form2.h"
#include "form3.h"

Then declare them as private or public :


private:
    Ui::MainWindow *ui;

    form1         *frm1;
    form2         *frm2;
    form3         *frm3;

And I want to use Signal/Slot with them. I want to emit a function from subwindow and update somethings in MainWindow. So we need to declare our slot functions header:


private slots:

    void fnc_slotfnc1();
    void fnc_slotfnc2();
    void fnc_slotfnc3();


We need to declare signals in all windows:

Put this signal declaration inside form1 header:


signals:
    void signalForm1();

Put this signal declaration inside form2 header:


signals:
    void signalForm2();

Put this signal declaration inside form3 header:


signals:
    void signalForm3();

Our SIGNALS are ready to use now.

Now we need to work in mainwindows.cpp file. In here first we are giong to initiate the subwindows. Then set the window settings and sizes. After this we can do whatever we want about subwindow. After configuring the window and adding inside the mdi area, we need to connect subwindow's SIGNAL and mainwindow's SLOT.



//first initiate the subwindows object
frm1 = new form1(this);
//add the subwindow into mdi area
ui->mdiAllWindows->addSubWindow(frm1);
//configure the window frame settings
frm1->parentWidget()->setWindowFlags( Qt::FramelessWindowHint ); 
//and sizes
frm1->parentWidget()->setGeometry(10,10,470,60);
//and then our signal/slot connection 
connect(frm1, SIGNAL(signalForm1()), this, SLOT(fnc_slotfnc1()));

frm2 = new form2(this);
ui->mdiAllWindows->addSubWindow(frm2);
frm2->parentWidget()->setWindowFlags( Qt::FramelessWindowHint ); 
frm2->parentWidget()->setGeometry(10,80,470,60);
connect(frm2, SIGNAL(signalForm2()), this, SLOT(fnc_slotfnc2()));

frm3 = new form3(this);
ui->mdiAllWindows->addSubWindow(frm3);
frm3->parentWidget()->setWindowFlags( Qt::FramelessWindowHint );
frm3->parentWidget()->setGeometry(10,150,470,60);
connect(frm3, SIGNAL(signalForm3()), this, SLOT(fnc_slotfnc3()) );

Here we have created our subwindows and inserted into mdi area. After this we have connected the slots to their signals.

Put these slots in to mainwindow.cpp file. These are slots that we will use to update data from subwindows:,


void MainWindow::fnc_slotfnc3(){

}

void MainWindow::fnc_slotfnc2(){

}

void MainWindow::fnc_slotfnc1(){

}

Now we are ready to use mdi area in our program.

Keep following my articles.

Have a nice coding.

Burak Hamdi TUFAN


Tags

Share this Post



Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

    There is no comment. Be the owner of first comment...