c++qtqmdiarea

Horizontally tiling QMdiArea sub winows that contains QScrollArea


The following piece of code (mainwindow.cpp) is a simplified version of my problem. I tried to tile QMdiArea sub windows horizontally, but somehow the sub windows' are not fully shown - the horizontal scroll bars at the bottom of those sub windows are not shown properly. It seems to me that the problem is the height setting code - If I enlarge the main window (without putting the tiling code in resize event), the sub windows show up OK. So I guess the solution would be figuring out the proper height. But, how?

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

// CODE added to the generated =========================================
#include <QMdiArea>
#include <QDebug>
#include <QGridLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QLabel>
#include <QString>
#include <QMdiSubWindow>
// CODE END=========================================

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // CODE added to the generated =========================================
    QMdiArea *mdiArea = new QMdiArea;
    this->setCentralWidget(mdiArea);

    QLabel *lable1 = new     QLabel(QString("01234567890123456789012345678901234567890123456789012345678901234567890123456789    01234567890123456789012345678901234567890123456789012345678901234567890123456789"));
    QScrollArea *scrollArea1 = new QScrollArea;
    scrollArea1->setWidget(lable1);
    QGridLayout *grid1 = new QGridLayout;
    grid1->addWidget(scrollArea1);
    QWidget *widget1 = new QWidget;
    widget1->setLayout(grid1);
    mdiArea->addSubWindow(widget1, Qt::FramelessWindowHint);


    QLabel *lable2 = new     QLabel(QString("01234567890123456789012345678901234567890123456789012345678901234567890123456789    01234567890123456789012345678901234567890123456789012345678901234567890123456789"));
    QScrollArea *scrollArea2 = new QScrollArea;
    scrollArea2->setWidget(lable2);
    QGridLayout *grid2 = new QGridLayout;
    grid2->addWidget(scrollArea2);
    QWidget *widget2 = new QWidget;
    widget2->setLayout(grid2);
    mdiArea->addSubWindow(widget2, Qt::FramelessWindowHint);

    // horizontally tile the above widget1&2
    int subWidth = this->width() / mdiArea->subWindowList().count();
    int x = 0;
    for(auto sub : mdiArea->subWindowList()) {
        sub->resize(subWidth, height()); // what kind of height could let the whole sub windows including horizontal scrollbars show?
        sub->move(x, 0);
        x += subWidth;
    }
    // CODE END=========================================
}

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

Thanks in advance.


Solution

  • In case someone runs into this: QMdiArea::tileSubWindows() does the trick.

    mdiArea->tileSubWindows();