单击该按钮后,我希望使qtooltip消息持久。我计划稍后用qtimer来隐藏它,但问题是,一旦我移动鼠标光标离开按钮rect,消息就消失了,我想让它停留在那里,直到稍后我调用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_()发布于 2017-06-09 20:46:07
一个可能的解决方案是使用QLabel作为QToolTip,我们通过启用Qt.ToolTip标志来做到这一点。就你而言:
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_()https://stackoverflow.com/questions/44463700
复制相似问题