I tried to use setToolTip of QGroupBox, but the tooltip shows in all places in the group box. What I want is to only show the tooltip in the title label.
Is is even possible? If not, why the QGroupBox is designed that way?
CodePudding user response:
It is possible to control the tool-tip behaviour, but there's no built-in method to do that, so you just need to add a little custom event-handling yourself. Here's a basic demo that implements that using an event-filter:
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.button = QtWidgets.QPushButton('Test')
self.button.setToolTip('Button ToolTip')
self.group = QtWidgets.QGroupBox('Title')
self.group.installEventFilter(self)
self.group.setToolTip('Groupbox ToolTip')
self.group.setCheckable(True)
hbox = QtWidgets.QHBoxLayout(self.group)
hbox.addWidget(self.button)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.group)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.ToolTip and
isinstance(source, QtWidgets.QGroupBox)):
options = QtWidgets.QStyleOptionGroupBox()
source.initStyleOption(options)
control = source.style().hitTestComplexControl(
QtWidgets.QStyle.CC_GroupBox, options, event.pos())
if (control != QtWidgets.QStyle.SC_GroupBoxLabel and
control != QtWidgets.QStyle.SC_GroupBoxCheckBox):
QtWidgets.QToolTip.hideText()
return True
return super().eventFilter(source, event)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
app.exec_()
An alternative solution would be to create a subclass and override the event
method directly:
class GroupBox(QtWidgets.QGroupBox):
def event(self, event):
if event.type() == QtCore.QEvent.ToolTip:
options = QtWidgets.QStyleOptionGroupBox()
self.initStyleOption(options)
control = self.style().hitTestComplexControl(
QtWidgets.QStyle.CC_GroupBox, options, event.pos())
if (control != QtWidgets.QStyle.SC_GroupBoxLabel and
control != QtWidgets.QStyle.SC_GroupBoxCheckBox):
QtWidgets.QToolTip.hideText()
return True
return super().event(event)
CodePudding user response:
QWidget::setToolTip
, by defaults, set the tooltip for the whole widget. QGroupBox
does not provide a special tooltip behavior, so the tooltip is visible for the whole group box, including its children (except if those provide their own tooltips). However, you are ably to customise this behaviour yourself, by reimplementing QWidget::event
:
If you want to control a tooltip's behavior, you can intercept the event() function and catch the QEvent::ToolTip event (e.g., if you want to customize the area for which the tooltip should be shown).
Two approaches are possible:
- Capture the
QEvent::ToolTip
event on theQGroupBox
itself. See the comment of musicamante on how to determine the title region. - Capture the
QEvent::ToolTip
on the children of theQGroupBox
and suppress the the event.
A full blown custom tooltip example is given in the Qt documentation itself.