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++
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With