QDialog: how to use question mark (?) button?

前端 未结 3 645
逝去的感伤
逝去的感伤 2021-01-12 01:39

By default, QDialog windows have a question mark pushbutton in the upper-right corner. When I press it, the mouse cursor is changed to the \'Forbidden\' cursor,

相关标签:
3条回答
  • 2021-01-12 01:46

    The other answers were a bit misleading for me, focusing only on catching the question mark event, but not explaining the normal usage.

    When this button is clicked and WhatsThisMode is triggered, the elements of the dialog are supposed to give info about themselves. And if mouse hovers over an element that supports this info then the pointer will become a pointing arrow with a question mark (on Windows at least), with a tooltip message displayed on mouse click.

    Here's how to achieve it in PySide:

    someWidget.setWhatsThis("Help on widget")
    

    QWhatsThis documentation for PySide and Qt5 is also available.

    0 讨论(0)
  • 2021-01-12 01:52

    It is not a button documented by Qt. You can detect this by catching events and checking event type:

    http://qt-project.org/doc/qt-5/qevent.html#Type-enum

    There are different types as QEvent::EnterWhatsThisMode QEvent::WhatsThisClicked and so on. I achieved something similar to what are you looking for using event filter in mainwindow.

    if(event->type() == QEvent::EnterWhatsThisMode)
        qDebug() << "click";
    

    I saw "click" when I clicked on ? button.

    0 讨论(0)
  • 2021-01-12 02:00

    Based on Chernobyl's answer, this is how I did it in Python (PySide):

    def event(self, event): 
        if event.type() == QtCore.QEvent.EnterWhatsThisMode:
            print "click"
            return True
        return QtGui.QDialog.event(self, event)
    

    That is, you reimplement event when app enters 'WhatsThisMode'. Otherwise, pass along control back to the base class.

    It almost works. The only wrinkle is that the mouse cursor is still turned into the 'Forbidden' shape. Based on another post, I got rid of that by adding:

    QtGui.QWhatsThis.leaveWhatsThisMode()
    

    As the line right before the print command in the previous.

    0 讨论(0)
提交回复
热议问题