Qt Mdi Area get size not including scroll area
-
I have some issue with Qt MdiArea where I created a sub-window which fit inside MdiArea, and then I check the size of MdiArea got correct result. But if I move the window to the end of MdiArea which will make scroll bar appear, and check the MdiArea size again, but getting the same size as window without scrollbar. I think the scroll area not considering while getting size, but only the viewport size returning.
How I can get the total size including scrollable area of the MdiArea.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation); // Set Mdi Area as the central widget setCentralWidget(ui->mdiArea); QWidget *widget = new QWidget(ui->mdiArea); QMdiSubWindow *w1 = ui->mdiArea->addSubWindow(widget); w1->resize(320, 240); // And show the widget widget->show(); } void MainWindow::wheelEvent(QWheelEvent *event) { qDebug()<<"mdiArea size: "<<ui->mdiArea->size(); }
Correct Result without scroll bar.
Wrong Result with scroll bar.
-
ui->mdiArea->size()
is the size of the entire widget, including all its children and scrollbars are just children.
What you probably want is the size of the scroll area viewport - the widget that hosts the actual contents of the area, soui->mdiArea->viewport()->size()
. -
@Chris-Kawa said in Qt Mdi Area get size not including scroll area:
ui->mdiArea->viewport()->size()
I tried that too, but got the same result.
qDebug()<<"mdiArea size: "<<ui->mdiArea->size(); qDebug()<<"mdiArea -> viewport size: "<<ui->mdiArea->viewport()->size();
Print the same result when scroll bar there and not there.
mdiArea size: QSize(982, 662)
mdiArea -> viewport size: QSize(982, 645)And I got it solved by,
QScrollBar *h_bar = ui->mdiArea->horizontalScrollBar(); QScrollBar *v_bar = ui->mdiArea->verticalScrollBar(); int total_width = h_bar->maximum()-h_bar->minimum()+ui->mdiArea->width(); int total_height = v_bar->maximum()-v_bar->minimum()+ui->mdiArea->height();
Seems working for me.
-
Oh, I misunderstood what you want. I thought you wanted the viewport size but you want the actual viewport contents size. QScrollArea has a
widget()
method that lets you get the content widget and its size, but unfortunately QMdiArea doesn't seem to have similar option, so your solution might be ok.