rakesh sinha
rakesh sinha

Reputation: 119

QMdiArea is returning the wrong height and width

I am trying to arrange my subWindows in the QMdiArea vertically. I saw lot of examples online and they all were doing the same thing as I am doing here.

I have two textEdits which needs to be tiled vertically both covering half of the screen. So in the constructor of the MainWindow I add the two textEdits as subWindow to the qMdiArea and then find the height and width of the qMdiArea divide the height by 2 and resize the subWindow. Please see the code below.

My mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->showMaximized();
    qMdiArea = new QMdiArea();
    qTextEdit1 = new QTextEdit();
    qTextEdit2 = new QTextEdit();
    setCentralWidget(qMdiArea);

    qMdiArea->adjustSize();
    qMdiArea->addSubWindow(qTextEdit1);
    qMdiArea->addSubWindow(qTextEdit2);

    QPoint position(0, 0);
    foreach (QMdiSubWindow *window, qMdiArea->subWindowList())
    {
        QRect rect(0, 0, qMdiArea->width(), qMdiArea->height() / qMdiArea->subWindowList().count());
        window->setGeometry(rect);
        window->move(position);
        position.setY(position.y() + window->height());
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

My window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMdiArea>
#include <QTextEdit>
#include <QPoint>
#include <QMdiSubWindow>
#include <QRect>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMdiArea *qMdiArea;
    QTextEdit *qTextEdit1;
    QTextEdit *qTextEdit2;
};

#endif // MAINWINDOW_H

and my Main File :

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

But its not happening as expected. The window just occupy a part of the screen though they are tiled vertically. My screen resolution is 1920x1200

enter image description here

Upvotes: 3

Views: 622

Answers (1)

Max Go
Max Go

Reputation: 2102

The height() and width() of mdiArea are invalid at that stage, because the widget hasn't been exposed/shown yet. Calling show() only schedules a widget for display, the act of sizing it and showing it on screen happens later when the control has returned to the event loop.

As a solution, you can override the resizeEvent handler. Once you do, your project will work again:

Definition in mainwindow.h:

virtual void resizeEvent(QResizeEvent *ev) override;

Implementation in mainwindow.cpp:

void MainWindow::resizeEvent(QResizeEvent *ev)
{
    Q_UNUSED(ev)

    QPoint position(0, 0);
    foreach (QMdiSubWindow *window, qMdiArea->subWindowList())
    {
        QRect rect(0, 0, qMdiArea->contentsRect().width(), qMdiArea->contentsRect().height() / qMdiArea->subWindowList().count());
        window->setGeometry(rect);
        window->move(position);
        position.setY(position.y() + window->height());
    }
}

Also it seems that you don't really need to have this->showMaximized(); call inside MainWindow's constructor. You can call it from main.cpp, for example.

Upvotes: 2

Related Questions