0

以下代码(mainwindow.cpp)是我的问题的简化版本。我试图水平平铺 QMdiArea 子窗口,但不知何故子窗口没有完全显示 - 这些子窗口底部的水平滚动条没有正确显示。在我看来,问题在于高度设置代码 - 如果我放大主窗口(没有将平铺代码放在调整大小事件中),子窗口显示正常。所以我想解决方案是找出合适的高度。但是,怎么做?

#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;
}

提前致谢。

4

1 回答 1

0

如果有人遇到这种情况: QMdiArea::tileSubWindows() 可以解决问题。

mdiArea->tileSubWindows();
于 2020-08-15T18:14:42.740 回答