Mat
Mat

Reputation: 4501

Qt: how to make mainWindow automatically resize when centralwidget is resized?

I would like to have my CentralWidget a certain size. What do I need to do to make my mainWindow resize along it's central widget? here the code that doesn't work:

int main (int argc, char **argv) {
    QApplication app(argc, argv);

    QGLFormat glFormat;
    glFormat.setVersion(4,2);
    glFormat.setProfile( QGLFormat::CompatibilityProfile);

    QGLWidget* render_qglwidget = new MyWidget(glFormat);

    QGLContext* glContext = (QGLContext *) render_qglwidget->context();
    glContext->makeCurrent();


    QMainWindow* mainWindow = new MyMainWindow();
    render_qglwidget->resize(720, 486);
    mainWindow->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
    mainWindow->setCentralWidget(render_qglwidget);

    render_qglwidget->resize(720, 486);

    mainWindow->show();

    return app.exec();
}

the window that opens will be very small.

i can set the size of the mainwindow using

 mainWindow->resize(720, 486);

and the centralwidget will also change it's size. but the central widget will be slightly squashed because the toolbar of the mainWindow also lies within those 486 pixels.

How to let the mainWindow resize automatically?

Upvotes: 8

Views: 19555

Answers (4)

JanKanis
JanKanis

Reputation: 6672

I used the following code to solve a similar problem. I wanted to explicitly resize my main window so that the central widget has a given size.

def centralWidgetResize(self, x, y):
    # If the window is not visible, it doesn't keep its layout up to date, so force it.
    if not self.isVisible():
      self.layout().update()
    # Does nothing if the layout is already up to date (and the window is visible).
    self.layout().activate()
    size = self.size()
    childsize = self.centralWidget().size()
    dx = size.width() - childsize.width()
    dy = size.height() - childsize.height()
    self.resize(x + dx, y + dy)

It's Python but the C++ code should be a straightforward translation. It also works with weird toolbar placements or multiple toolbars. Note that the actual updating of the size only happens when the window is shown, if it is needed immediately and the window is hidden do another layout().update(); layout().activate().

Upvotes: 1

alexisdm
alexisdm

Reputation: 29886

You can reimplement QMainWindow::event() to resize the window automatically whenever the size of the central widget changes:

bool MyMainWindow::event(QEvent *ev) {
    if(ev->type() == QEvent::LayoutRequest) {
        setFixedSize(sizeHint());
    }
    return result = QMainWindow::event(ev);
}

You also have to use setFixedSize() for the central widget instead of just resize(). The latter is almost useless when the widget is placed inside a layout (which is a QMainWindowLayout here).

Upvotes: 3

Chris Browet
Chris Browet

Reputation: 4266

It is not possible to set the size of the QMainWindow based up its central widget. It is always the other way around.

As you said, use QMainWindow::resize(), adding the toolbar and statusbar height to your central widget to get the final result.

Be also sure to resize in a "delayed init" (i.e. a QTimer with a timeout of 0), so that the height of the toolbar and statusbar are accurate.

If you want your main window to be unresizable by the user, use QMainWindow::setWindowFlags(Qt::FramelessWindowHint) to disable resize by mouse dragging.

Upvotes: 0

NFRCR
NFRCR

Reputation: 5570

Set the size of the central widget. Then get the sizehint for the main window and set it to it's size.

(sizeHint(), setFixedSize()).

Upvotes: 0

Related Questions