下面的代码(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;
}
提前谢谢。
如果有人遇到这种情况:QMdiArea::tileSubWindows((就可以了。
mdiArea->tileSubWindows();