如何使QToolTip消息持久化? [英] How to make QToolTip message persistent?

查看:144
本文介绍了如何使QToolTip消息持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮后使qtooltip消息持久化.我计划稍后使用qtimer自己隐藏它,但是问题是,一旦我将鼠标光标从按钮rect移开,消息就会消失,我想使其停留在那儿,直到以后我调用hideText()

I want to make a qtooltip message persistent after I clicked the button. I plan to use qtimer to hide it by myself later, but the problem is as soon as I move mouse cursor away from the button rect, the message disappears, I want to make it stay there, until later I call hideText()

from PyQt4 import QtGui, QtCore
from functools import partial

class MyDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        layout = QtGui.QVBoxLayout()
        btn = QtGui.QPushButton('Push Me')
        layout.addWidget(btn)
        self.setLayout(layout)

        btn.clicked.connect(partial(self.showFloatingMessage,'This is a long message'))

    def showFloatingMessage(self, message='', delay=500):

        desktop = QtGui.QApplication.desktop()
        screen_num = desktop.screenNumber(QtGui.QCursor.pos())
        screen_rect = desktop.screenGeometry(screen_num)
        QtGui.QToolTip.showText(screen_rect.center(), message, None, screen_rect)


app = QtGui.QApplication([])

dialog = MyDialog()
dialog.show()

app.exec_()

推荐答案

一个可行的解决方案是将QLabel用作QToolTip,我们通过启用Qt.ToolTip标志来实现.就您而言:

A possible solution is to use a QLabel as QToolTip, we do this by enabling the Qt.ToolTip flag. In your case:

from PyQt4 import QtGui, QtCore


class MyDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        layout = QtGui.QVBoxLayout()
        btn = QtGui.QPushButton('Push Me')
        layout.addWidget(btn)
        self.setLayout(layout)

        btn.clicked.connect(lambda: self.showFloatingMessage('This is a long message', 5000))

    def showFloatingMessage(self, message='', delay=500):
        desktop = QtGui.QApplication.desktop()
        screen_num = desktop.screenNumber(QtGui.QCursor.pos())
        screen_rect = desktop.screenGeometry(screen_num)

        lb = QtGui.QLabel(self)
        lb.setWindowFlags(QtCore.Qt.ToolTip)
        lb.setText(message)
        lb.move(screen_rect.center())
        lb.show()
        QtCore.QTimer.singleShot(delay, lb.hide)


app = QtGui.QApplication([])

dialog = MyDialog()
dialog.show()

app.exec_()

这篇关于如何使QToolTip消息持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆