PDA

View Full Version : Toolbars, floating, and sizing



spraff
26th January 2009, 11:19
Hi. At the moment I have this in a QMainWindow subclass constructor:


thing = new QToolBar(name,this);
addToolBar (Qt::LeftToolBarArea, thing)
// populate thing...

Which works fine.

If I resize the main window, the centralWidget is smaller than that size becuase
the toolbar takes up room. Ideally the resize method would set the centralWidget's size and grow the QMainWindow to fit the toolbars. (If I call resize on the centralWidget, the QMainWindow doesn't seem to respond.)

Another acceptable solution would be to create the QToolBar to be initially floating, but I can't see how. Omitting the addToolBar call simply means that it has no layout and appears squashed int the QMainWindow's space, and there aren't any QToolBar methods I can see that accomplish this.

Can you see how to solve either of these niggles?

Thanks.

wysota
26th January 2009, 12:20
If I resize the main window, the centralWidget is smaller than that size becuase
the toolbar takes up room. Ideally the resize method would set the centralWidget's size and grow the QMainWindow to fit the toolbars. (If I call resize on the centralWidget, the QMainWindow doesn't seem to respond.)

Basically you have two choices.
1. Let QMainWindow adjust its size according to its contents using resize(sizeHint()) and adjust layouts so that you are satisfied with the size hint
2. Use resize(sizeHint()+QSize(addWidth, addHeight)) where addWidth and addHeight are the values which you want to add to the size hint (which you have to calculate manually).

There is also a third, a bit wacky solution - go even further and ask Qt to calculate the offset for you.

QSize margins = size()-centralWidget->size(); // this is the size of "stuff" around the central widget
QSize myDestSize = ...; // set to whatever you want the central widget to be
resize(myDestSize+margins);


Another acceptable solution would be to create the QToolBar to be initially floating, but I can't see how.
Probably like this:

addToolBar(Qt::NoToolBarArea, thing);