Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++

like image 947
Petar Petrov Avatar asked Dec 14 '22 16:12

Petar Petrov


2 Answers

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();
    }
}
like image 196
Petar Petrov Avatar answered Dec 26 '22 00:12

Petar Petrov


I'm afraid you can't do it that way, as QDockWidget look and feel is essentially hard coded in the QStyle your application is using, as stated in the documnetation (here in the "Appearance" section). Basically, QDockWidget is a borderless window, and the title bar and its structure(title, buttons, etc) is just painted using the style.

To overcome this, you may use a QProxyStyle to paint the minimize and maximize buttons, but these would not be "real" buttons but just their pixmaps. Hence, you would still need to perform some tinkering to handle the clicks to those virtual buttons (e.g. catching the click event on the title bar and determining if it happened inside one of these buttons).

Another possible solution is to subclass QDockWidget and implement all the painting and click event handling there. Beaware that if you want to support multiple platforms you may need to use QStyle::drawControl() to paint the extra buttons, instead of painting everything by yourself (e.g. drawing a pixmap).

I hope this helps you. Good luck with your project.

like image 42
Sergio Monteleone Avatar answered Dec 25 '22 23:12

Sergio Monteleone