<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Using standard tooltips might be an issue in some (not so special) cases, as QToolTip events are strictly related to the widget events, so you'll need to install an eventFilter on every single widget you'll want the behavior you ask about.<div>A simple implementation would be something like this:</div><div><br></div><div><font face="monospace, monospace"> def __init__(self, *args, **kwargs):</font></div><div><font face="monospace, monospace"> [...]</font></div><div><font face="monospace, monospace"> self.toolTipWidget = QtWidgets.QLabel()</font></div><div><font face="monospace, monospace"> [...]</font></div><div><font face="monospace, monospace"> self.someWidget = SomeWidget()<br></font></div><div><font face="monospace, monospace"> self.someWidget.setToolTip('I am a tooltip!')</font></div><div><font face="monospace, monospace"> self.someWidget.installEventFilter(self)</font></div><div><font face="monospace, monospace"> [...]<br></font><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace"> def eventFilter(self, source, event):</font></div><div><font face="monospace, monospace"> if event.type() == QtCore.QEvent.ToolTip:</font></div><div><font face="monospace, monospace"> self.toolTipWidget.setText(source.toolTip())<br></font></div><div><font face="monospace, monospace"> return QtWidgets.QWidget.eventFilter(self, source, event)</font></div></div><div><br></div><div>On the other hand, I'd suggest to use the StatusTip instead of the ToolTip: it's something I've successfully used for something similar to your needs: in this way you can keep the StatusTip for "simple" tooltip text and the ToolTips for further customized messages, as they also allow rich text content.</div></div><div>Here's a small example:</div><div><br></div><div><div><font face="monospace, monospace"> def __init__(self, *args, **kwargs):</font></div><div><font face="monospace, monospace"> [...]</font></div><div><font face="monospace, monospace"> self.toolTipWidget = QtWidgets.QLabel()</font></div><div><font face="monospace, monospace"> [...]</font></div><div><span style="font-family:monospace,monospace"> self.someWidget = SomeWidget()</span><br></div><div><font face="monospace, monospace"> self.someWidget.setStatusTip('I am a statustip!')</font></div><div><span style="font-family:monospace,monospace"> [...]</span><br></div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace"> def event(self, event):</font></div><div><font face="monospace, monospace"> if event.type() == QtCore.QEvent.StatusTip and event.tip():</font></div><div><font face="monospace, monospace"> self.toolTipWidget.setText(event.tip())</font></div><div><font face="monospace, monospace"> return QtWidgets.QWidget.event(self, event)</font></div><div><br></div></div><div>Note that here I used the <font face="monospace, monospace">setStatusTip()</font> method instead of the <font face="monospace, monospace">setToolTip()</font> one.</div><div>Obviously, if you're using Designer you'll set the StatusTip property from there, instead of the ToolTip property.</div><div><br></div><div>How does it work?</div><div>Usually the StatusTip is only used on a QMainWindow with a QStatusBar installed, but, interestingly enough, the <font face="monospace, monospace">event()</font> method of <b>any</b> widget can be used to catch <i>any</i> StatusTip event called from both that widget <i>and</i> its children.<br><br>In both cases you'd better think about a way to "clear" the tool/status tip, as leaving it there might be confusing.<br>If you don't have too many widgets, the eventFilter way might be a good solution, as you can also catch the QEvent.Leave event type to hide the "tooltip" by clearing the label text, otherwise it's probably better using a QTimer on the parent widget or the label, and set it as singleShot (don't use the static method, as it could hide a new statustip activated in the meantime), then connect it to something like <font face="monospace, monospace">lambda: self.toolTipWidget.setText('')</font><font face="arial, helvetica, sans-serif"> whenever you catch the StatusTip event.</font></div><div><br></div><div>If for some reason you'll need to stick with ToolTips, there's a solution anyway.</div><div>If all widgets already have their tooltips, and the layout is static and permanent once the main parent widget is being instantiated, just use the children() iterator and check for both isWidgetType() and toolTip() contents: if those condition match, install the eventFilter; if the layout is dynamic instead, use the childEvent method on the parent and check for QEvent.childAdded and QEvent.childRemoved events, then use installEventFilter or removeEventFilter respectively.</div><div><br></div><div>Regards,</div><div>Maurizio</div><div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail-m_-6716252547537194329gmail_attr">Il giorno lun 21 gen 2019 alle ore 23:22 Tong Zhang <<a href="mailto:warriorlance@gmail.com" target="_blank">warriorlance@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hello,</div><div><br></div><div>Can I show the tooltip of some widget onto a QLabel? e.g. Tooltip will show when I move the mouse onto the pushbutton, how about show the tooltip on another widget, say label?<br></div><div><br></div><div>Thanks,</div><div>Tong<br></div></div>
_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br>
<a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_-6716252547537194329gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>