tags:

views:

1473

answers:

2

How do I set the initial width of a QDockWidget?

I have implemented the sizeHint function but what next?

+1  A: 

If you want it to have the same width as the same last time the program was running, you should look into settings. The Qt 4.4 documentation has an example on how to use settings here.

This is how I have done it:

void Applicotion::readSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  libraryDock->setFloating(settings.value("docked").toBool());
  libraryDock->resize(settings.value("size", QSize(1, 1)).toSize());
  libraryDock->move(settings.value("pos", QPoint(200, 200)).toPoint());
  addDockWidget((Qt::DockWidgetArea)settings.value("dockarea", Qt::RightDockWidgetArea).toInt(), libraryDock);
  settings.endGroup();
}

void Applicotion::writeSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  settings.setValue("dockarea", dockWidgetArea(libraryDock));
  settings.setValue("docked", libraryDock->isFloating());
  settings.setValue("size", libraryDock->size());
  settings.setValue("pos", libraryDock->pos());
  settings.endGroup();

}

Marius
If you are using a QMainWindow to hold the docked windows it is much easier to use saveState and restoreState wich saves the state of all dock widgets and toolbars
David Dibben
+2  A: 

The documentation for QDockWidget says:

A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on wether it is docked; a docked QDockWidget has no frame and a smaller title bar.

So the size hint is taken from whatever you put in the dock widget. Have you tried setting the size of the QDockWidget's child?

But, I agree with Marius, the best thing to do is probably to use QSettings to save and restore the widths of all the dock windows when the application starts. Have a look at QMainWindow::saveState Apart from getting the data from saveState rather than from individual functions my save function looks very similar to the one given by Marius.

David Dibben