QDockWidget and saveState()
-
I can see that it is saving the state of the dock by looking at the byte array and I see the name I have set of the QDockWidet etc... It only seems to be able to set the size of the dock when it's floating otherwise it always goes to the minimum width while docked. It does save whether it's floating and where it's docked, but something just isn't letting it set the size.
@
//constructorQMainWindow* viewMain; // child control for dock, I have a toolbar for the docked window
QToolBar* viewToolbar;
QDockWidget* viewDock;
QViewport* view; // QGLWidget subclassedthis->setCentralWidget(new QTabWidget(this)); // have the main window central widget set to a tabwidget
viewDock = new QDockWidget(this);
viewDock->setObjectName("ViewportDock");viewMain = new QMainWindow(viewDock);
viewMain->setObjectName("ViewportMainWindow");
viewMain->setWindowFlags(Qt::Widget);view = new QViewport(viewMain);
view->setObjectName("Viewport");viewMain->setCentralWidget(view);
viewToolbar = new QToolBar(viewMain);
viewToolbar->setObjectName("ViewportToolbar");viewMain->addToolBar(viewToolbar);
viewDock->setWidget(viewMain);
this->addDockWidget(Qt::RightDockWidgetArea, viewDock);
// I have a QSetting class, this is just to show when I am calling these
this->restoreState();
this->restoreGeometry()
@ -
Maybe it's a bug or maybe it's the intended behaviour. If you think it's a bug please file a bug and attach a small example that reproduces it. Even better, please get involved on "qt-project.org":http://qt-project.org and contribute a patch that fixes the problem :-)
-
Well I think it's the intended behaviour, in that it's not possible to change the size of the QDockWidget when it's docked, but I would think save/restore state would allow for it.
I find that whenever I undock than dock it always reverts back to the size that I had last set so I am thinking there is a Size variable for each docking spot, perhaps if there were a function to allow us to modify these.
@
QMainWindow::setDockSize( Qt::RightDockWidgetArea, int width (or) height );
@Right now it seems the only way to do this is to set the minimum size of the dockwidget to the size you want, than reset it back to default after it's loaded. This seems to be the only way to set the size of a dock area for a QMainWindow.
-
Well I narrowed down the problem to Visual Studio 2010. For some reason loadState() doesn't want to work there, only for QDockWidgets' size. I have the same code for Visual Studio 2008 and it works fine.
EDIT:
I narrowed it down more, the problem isn't IDE specific. The problem is setting a StyleSheet in the main() function for the QMainWindow and than calling restoreState() in the constructor of the QMainWindow. :\ What are the odds.
-
Yeah stylesheets sometimes have some unexpected side effects. Looks like a bug though. Please "report it":http://bugreports.qt.nokia.com
-
I am still having this problem. Turning off stylesheets didn't work for me. Below is the code fix that let it work for me:
@
void MainWindow::showEvent(QShowEvent*)
{
QSettings s;
this->restoreGeometry(s.value("MainWindow/geometry").toByteArray());
restoreState(s.value("MainWindow/windowState").toByteArray());
int arraySize = s.beginReadArray("MainWindow/docks");
QList<QDockWidget*> docks = this->findChildren<QDockWidget*>();
for(int i = 0; i< arraySize && i < docks.count(); i++)
{
s.setArrayIndex(i);
docks.at(i)->setFixedSize(s.value("size").toSize());
docks.at(i)->update();
docks.at(i)->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
docks.at(i)->setMinimumSize(1,1);
}
s.endArray();
}void MainWindow::closeEvent(QCloseEvent *e)
{
QSettings s;
s.setValue("MainWindow/geometry", this->saveGeometry());
s.setValue("MainWindow/windowState", saveState());s.beginWriteArray("MainWindow/docks"); QList<QDockWidget*> docks = this->findChildren<QDockWidget*>(); for(int i = 0; i < docks.size(); i++) { s.setArrayIndex(i); s.setValue("size",docks.at(i)->size()); } s.endArray();
}
@ -
I am still having this problem. Turning off stylesheets didn't work for me. Below is the code fix that let it work for me:
@
void MainWindow::showEvent(QShowEvent*)
{
QSettings s;
this->restoreGeometry(s.value("MainWindow/geometry").toByteArray());
restoreState(s.value("MainWindow/windowState").toByteArray());
int arraySize = s.beginReadArray("MainWindow/docks");
QList<QDockWidget*> docks = this->findChildren<QDockWidget*>();
for(int i = 0; i< arraySize && i < docks.count(); i++)
{
s.setArrayIndex(i);
docks.at(i)->setFixedSize(s.value("size").toSize());
docks.at(i)->update();
docks.at(i)->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
docks.at(i)->setMinimumSize(1,1);
}
s.endArray();
}void MainWindow::closeEvent(QCloseEvent *e)
{
QSettings s;
s.setValue("MainWindow/geometry", this->saveGeometry());
s.setValue("MainWindow/windowState", saveState());s.beginWriteArray("MainWindow/docks"); QList<QDockWidget*> docks = this->findChildren<QDockWidget*>(); for(int i = 0; i < docks.size(); i++) { s.setArrayIndex(i); s.setValue("size",docks.at(i)->size()); } s.endArray();
}
@