Qt World Summit: Submit your Presentation

Resizing and moving a QDockWidget when it is undocked

  • Hi folks, I have some QDockWidget that are sometimes docked in a big space but containing only small widgets.
    When undocking it I would like the QDockWidget to be resized to it's smallest size.
    I got this working by connecting the topLevelChanged signal. @connect(ui->statusDock, SIGNAL(topLevelChanged(bool)), this, SLOT(slotDockResized(bool)));

    void MainWindow::slotDockResized(bool b)
    if( !b ) return;
    QDockWidget* senderDock = dynamic_cast<QDockWidget*>(QObject::sender());
    senderDock->move(QCursor::pos().x()-0.5f*senderDock->width(), QCursor::pos().y()-10);
    }@ But unfortunately when the QDockWidget is dragged to be undocked and if the widget changes from something really large to something much smaller, there are chance that the QDockWidget won't be under the mouse cursor anymore (because during the resizing the upper left is the reference point).

    That's why I added the move() call. This works if the QDockWidget is undocked with a double click on its titlebar.
    But it won't work if it is undock with drag and drop. I guess that in that case the QDockWidget has already entered a mouseMove event and my own move() call is ignored.

    Any clue to bypass this?

  • Is it possible to move a Widget that entered a drag event without reimplementing anything?

  • My very first BUMP here :p

  • Try if this works.. You can use negative values too in QWidget's geometry property.

    So instead of adjustSize().. you could use get the size using the senderDock->sizeHint() or senderDock->minimumSizeHint() and then do a senderDock->setGeometry(<The New Rect>) with a QRect of that size and centered on the current mouse cursor position relative to the widgets origin.

  • Thanks for your help. However I'm not sure I understood everything.
    As I said, the adjustSize() call works: the widget is correctly resized.
    The problem is with the move() call. Do you suggest me to replace it with a setGeometry()?

Log in to reply