How to resize QMainWindow after deleting all DockWidgets?

I am trying to make an application consisting of QMainWindow , the central widget of which is QToolBar (this may not be normal for me, but toolbars work well for my purpose). Docks are only allowed below. I added a QDockWidget to it, and QAction in the QToolBar turned QDockWidget on and off using removeDockWidget() and restoreDockWidget() .

The default size of QMainWindow is 800 to 24, QToolBar s maximumHeight also set to 24. Immediately after calling removeDockWidget() QMainWindow s geometry returns to (0,0,800,24) using setGeometry() .

I want to achieve resizing QMainWindow s to 24 when uninstalling DockWidget . It seems that setGeometry() works, since the width and position change are correspondingly, but funny enough, the height does not shift. And this is my problem :)

What do you think?

Here is a screenshot illustrating the problem.

NB: if I create the same script using QWidget, not QMainWindow, and using the show() or hide() child widget, I can resize the parent without adjustments with adjustSize() : this is the problem here above the QMainWindow specification.

+6
source share
2 answers

Parameters

a) You can overload the sizeHint () virtual function. Let it return the size you want for its main window.

b) In the main window constructor, you can call setMinimumSize () and setMaximumSize () one after the other, as with the desired size of the main window. If you save both values, you will get a fixed size.

c) Take a look at layout () -> setResizeMode (Fixed).

+2
source

It looks like you misunderstood the meaning of the QMainWindow.sizeHint() method.

According to the QWidget.sizeHint() documentation (from which QMainWindow inherits):

This property contains the recommended size for the widget.

If this property is not valid, size is not recommended. By default, the implementation of sizeHint () returns an invalid size if there is no layout for this widget, and otherwise returns the preferred layout size.

To get the actual size of your window, you should instead use the QMainWindow.geometry() method, which gives all the information about the size and position of the widget:

 win_geo = self.geometry() win_top = win_geo.top() win_bottom = win_geo.bottom() win_left = win_geo.left() win_right = win_geo.right() win_width = win_geo.width() win_height = win_geo.height() 
+1
source

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


All Articles