Search code examples
qtqt4qdockwidget

Tooltips for QDockWidget close button & float button?


Is there a way to set a tool tip text for the close button & float button in a QDockWidget ?

enter image description here


Solution

  • As ixSci mentioned, using setTitleBarWidget() is a potential way of solving this problem. Having said that I was looking for a much simpler solution, ideally using QSS (Qt Style Sheets). So after digging into the source code of QDockWidget I found an alternative way which suits my requirement better.

    I wanted to find the place these float and close buttons are created. That is inside QDockWidgetPrivate::init() method in QDockWidget.cpp.

    As for an example, the float button is created like this:

    QAbstractButton *button = new QDockWidgetTitleButton(q);
    button->setObjectName(QLatin1String("qt_dockwidget_floatbutton"));
    QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_toggleTopLevel()));
    layout->setWidgetForRole(QDockWidgetLayout::FloatButton, button);
    

    Now all I need is to use the flexibility of Qt Style Sheets, for that I need only the Object Name, in this case it's "qt_dockwidget_floatbutton"

    So all you need to do, to set tooltips for Close and Float buttons of a QDockWidget, is to add following two lines of styles in your application style sheet

    QAbstractButton#qt_dockwidget_closebutton{qproperty-toolTip: "Close";}
    QAbstractButton#qt_dockwidget_floatbutton{qproperty-toolTip: "Restore";}