Problem: If I pull my app’s toolbar off of the window frame and float it, I can’t dock it again. My window hierarchy is fairly complex, so I’m not sure which part I have to poke with a stick to get it to behave.
My Qt app uses a stackWidget as its central widget. In one of the stack’s contained widgets, I want a toolbar and a graphics view. When I float that toolbar, the graphics view expands to consume the entire screen space and refuses to shrink to allow the toolbar to dock again.
The relevant code is below. I’ve stuck it in a project of its own to see what’s going on, so minus a few extra definitions in the header this should compile:
QVBoxLayout* layout0 = new QVBoxLayout(ui->centralWidget);
stack = new QStackedWidget();
layout0->addWidget(stack);
QWidget* screen1 = new QWidget();
QVBoxLayout* layout1 = new QVBoxLayout(screen1);
QToolButton* tool = new QToolButton();
tool->setIcon(QIcon(":/images/sample.bmp"));
tool->setIconSize(QSize(400,400));
QObject::connect(tool,SIGNAL(clicked()),this,SLOT(onPage2()));
layout1->addWidget(tool);
stack->addWidget(screen1);
QWidget* screen2 = new QWidget();
QVBoxLayout* layout2 = new QVBoxLayout(screen2);
QGraphicsView* gview = new QGraphicsView();
layout2->addWidget(gview);
stack->addWidget(screen2);
QToolBar* toolbar = new QToolBar();
toolbar->addAction("home",this,SLOT(onPage1()));
this->addToolBar(Qt::TopToolBarArea,toolbar);
For a stack widget, Qt requires that each widget within the stack has enough room to add the toolbar before it’ll let you dock it. Strangely, a layout might fit on the screen before you undock the toolbar or expand the window, but Qt might be unhappy about that size.
First find your trouble stack. Next, increase the window size, modify the widget layout, and/or alter the size constraints of the sub-widgets within the stack to allow the toolbar to dock.