Qt: how to make mainWindow resize automatically when resizing a centralized image?

I would like to have my CentralWidget of a certain size. What do I need to do to change my main variable along the center widget? here's 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 main window using

  mainWindow->resize(720, 486); 

and the central widget will also resize it. but the central widget will be slightly compressed, because the mainWindow toolbar also lies within these 486 pixels.

How to enable automatic resizing of mainWindow?

+6
source share
4 answers

You can override QMainWindow::event() to automatically resize the window when resizing the central widget:

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

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

+1
source

Set the size of the center widget. Then get the sizehint value for the main window and set its size.

(sizeHint (), setFixedSize ()).

0
source

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

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

Remember to also resize in the "delayed init" (i.e. a QTimer with a timeout of 0), so the height of the toolbar and status bar is accurate.

If you want your main window to be inaccessible to the user, use QMainWindow::setWindowFlags(Qt::FramelessWindowHint) to disable resizing by drag and drop.

0
source

I used the following code to solve a similar problem. I wanted to explicitly resize the main window so that the center widget had 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) 

This is Python, but C ++ code should be a simple translation. It also works with weird toolbar placements or with multiple toolbars. Please note that the actual size update occurs only when the window is displayed, if necessary immediately, and the window is hidden, makes another layout().update(); layout().activate() layout().update(); layout().activate() .

0
source

Source: https://habr.com/ru/post/911045/


All Articles