PDA

View Full Version : QToolbar expanding sizePolicy not working properly



sfcheng77
15th February 2011, 17:04
I have created a main window with two toolbars on the same row. Each toolbar has three buttons on it. However, I would like to have the first toolbar take up all the available space while let the 2nd one stay at minimum. I am trying to do this via sizing policy. I basically took the SDI example from QtDemo and modified the code in void MainWindow::createToolBars() as shown below:

void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
fileToolBar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Minimum);

editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
fileToolBar->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimum);
}

However, it has no effect at all. QT always let the last toolbar on the same row takes up all the available space.

Any idea what I did wrong? Thanks for any tips.

d_stranz
15th February 2011, 21:17
QMainWindow probably overrides the size policy and resizes the toolbars itself. What happens if you set a maximum size for the last toolbar? Does that get ignored too?

sfcheng77
22nd February 2011, 23:50
It doesn't get ignored. The last toolbar stays at the specified maximum size but the remaining space stays unused after the last toolbar.
The only workaround I've found so far is implement the sizeHint() of the first toolbar and give it a very large width. But that beats the purpose of the sizingPolicy to the toolbars. In QT3, we used to have setHorizontalStretchable() method. Now that method is gone and the sizing Policy doesn't work. I think this is a bug of QT4.




QMainWindow probably overrides the size policy and resizes the toolbars itself. What happens if you set a maximum size for the last toolbar? Does that get ignored too?