QDockWidget initial width


How do I set the initial width of a QDockWidget?

I have implemented the sizeHint function but what next?

10/22/2015 5:44:48 PM

Accepted Answer

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.

10/22/2015 5:02:14 PM

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");
  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);

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow