Search code examples
c++qtqt5qdockwidget

Maximize and minimize buttons in an undocked QDockWidget


I´ve been trying to add the buttons to an undocked QDockWidget window as I normally do for a QDialog, but with no success as follows:

QDockWidget* dw = new QDockWidget(QString("Stream %1").arg(i + 1), this); 
dw->setWindowFlags((dw->windowFlags() | Qt::WindowMaximizeButtonHint |
    Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint));

When I undock them they still only have the [X] close button.

What am I missing?

Development environment info: Windows 10 x64, Visual Studio 2015, Qt 5.7.1, C++


Solution

  • I figured out how to do it. You have to connect to the QDockWidget toplevelChanged(bool) signal.

    connect(ui.dockWidget, SIGNAL(topLevelChanged(bool)), this, SLOT(dockWidget_topLevelChanged(bool)));
    

    Then you need to check if its floating and set the window hints.

    void MyClass::dockWidget_topLevelChanged(bool)
    {
        QDockWidget* dw = static_cast<QDockWidget*>(QObject::sender());
        if (dw->isFloating())
        {
            dw->setWindowFlags(Qt::CustomizeWindowHint |
                Qt::Window | Qt::WindowMinimizeButtonHint |
                Qt::WindowMaximizeButtonHint |
                Qt::WindowCloseButtonHint);
            dw->show();
        }
    }