6

I’m trying to make an application consisting of a QMainWindow, the central widget of which is a QToolBar (it may not be usual, but for my purpose the toolbar’s well suited). Docks are allowed below only. I added a QDockWidget to it, and a QAction on the QToolBar toggles the QDockWidget on and off with removeDockWidget() and restoreDockWidget().

The default size of the QMainWindow is 800 by 24, QToolBar’s maximumHeight is set to 24 too. Right after the removeDockWidget() is called, QMainWindow’s geometry is set back to (0,0,800,24) with setGeometry().

What I want to achieve is to resize the QMainWindow’s height to 24 when the DockWidget’s removed. The setGeometry() seems to work since width and position change accordingly, but funnily enough, the height doesn’t budge. And that’s my problem really :)

What’s the matter you think?

Here is a screen-cast illustrating the issue at hand.

NB: if i create the same scenario using a QWidget rather than QMainWindow, and using a show() or hide() on the child widget, then I can resize the parent with adjustSize() without problem: it seems the problem here above is QMainWindow specific.

neydroydrec
  • 6,973
  • 9
  • 57
  • 89

2 Answers2

2

Options

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

b) In the main window's constructor you can call setMinimumSize() and setMaximumSize() one after another, both with the the desired main window size. If you keep both same you get a fixed size.

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

Ankur Gupta
  • 2,284
  • 4
  • 27
  • 40
  • Thanks. I'm not sure I follow your explanation. I've tried to change the geometry of the QmainWindow with updateGeometry() but it doesn't take effect really. I put the code here above with the output to show you what I mean. – neydroydrec Aug 16 '11 at 19:07
  • Check my answer and see if it makes sense :) – neydroydrec Aug 16 '11 at 22:40
  • @Benjamin ... Did you try overloading sizeHint or calling setMinimumSize() and setMaximumSize() ? if yes then did you get the desired size you were looking out for ? – Ankur Gupta Aug 17 '11 at 03:11
  • Let me try this again: i think i messed up somewhere to get to my previous conclusion. – neydroydrec Aug 17 '11 at 10:00
  • OK I tried this: `self.setMaximumSize(self.centralWidget().geometry().height(), self.centralWidget().geometry().width()) self.setGeometry(self.centralWidget().geometry())` thinking that should tell the QMainWindow to set its size below the maximum, in this case the size of the CentralWidget. I also tried providing explicit integers eg. `setMaximumSize(45,800)`, but although it affects the width, the height of the QMainWindow doesn't go below 75px. – neydroydrec Aug 17 '11 at 10:10
  • Update: I've tried a similar code based on QWidget rather than QMainWindow and the `adjustSize()` works like a charm, removing the space formerly occupied by the hidden dock. So this must be a problem specific to the QMainWindow. – neydroydrec Aug 17 '11 at 20:07
1

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

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

This property holds the recommended size for the widget.

If the value of this property is an invalid size, no size is recommended. The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

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

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()
mdeous
  • 17,513
  • 7
  • 56
  • 60
  • You are correct indeed on the mistake I committed regarding `sizeHint()`. But as I explained just now in my answer: the geometry only applies to the client area, and when the dock is removed, the frame area is not affected. Hence setting a new geometry does not suffice, even `adjustSize()` cannot change the frame size. If you have any Qt solution to changing the frame size, please do let me know :) – neydroydrec Aug 16 '11 at 22:39