工具提示停止 mouseMoveEvent
Posted
技术标签:
【中文标题】工具提示停止 mouseMoveEvent【英文标题】:Tooltip stops mouseMoveEvent 【发布时间】:2012-05-17 03:49:41 【问题描述】:我刚刚注意到在工具提示上移动鼠标光标似乎不会在 PySide 中返回 mouseMoveEvent。
在我的情况下,它会导致问题,因为当鼠标移到主图标区域时,我会在主图标顶部动态显示一个小图标,并在鼠标离开主图标区域时再次隐藏小图标。因此,如果用户将鼠标光标移过一个大的工具提示,然后最终移到主按钮区域之外,则鼠标事件永远不会注册新位置并且迷你图标保持可见,即使鼠标不再位于主图标区域上.
我想我可以通过创建自定义 QToolTip 并将其放置在远离主按钮区域的位置来解决这个问题,因此永远不可能通过工具提示离开主按钮区域。但这看起来很丑。
有没有人知道如何使工具提示注册到 mouseMoveEvent 以便可以避免?
这是一个示例 sn-p(希望它的格式正确,我以前从未在这里发布过):
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class FancyButtonSmall( QWidget ):
def __init__( self, parent=None ):
super( FancyButtonSmall, self ).__init__( parent )
self.setMouseTracking( True ) # TO DISPLAY "REMOVE" ICON ON MOUSE OVER
# LAYOUT
layout = QVBoxLayout()
layout.setSpacing( 0 )
self.setLayout( layout )
self.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed )
self.fixedSize = ( 80, 80 )
# BUTTON AND MOUSE STATE
self.mainButtonDown = False # TO DRAW ICON STATE PROPERLY
self.removeButtonDown = False # TO DRAW ICON STATE PROPERLY
self.mouseOver = False # TO DISPLAY DELETE ICON
# BUTTON COLOURS
self.widgetColMainUp = QColor(60, 60, 60, 0)
# LABELS
self.textCol = QColor( 150, 150, 150 )
# TOOLTIP
self.setToolTip( self.__wrapText( 100*'test ' ) )
def __wrapText( self, text, maxChar = 50 ):
'''wrap text to only contain maxChar per line'''
i = 1
charList = list( text )
while i*maxChar < len( charList ):
charList.insert( i*maxChar, '\n' )
i += 1
return ''.join( charList )
def mouseMoveEvent( self, event ):
'''Show remove icon if cursor is on top of main icon'''
print event.pos()
self.mouseOver = self.iconRect.contains( event.pos() ) or self.removeIconRect.contains( event.pos() )
self.update()
def paintEvent( self, event ):
painter = QPainter( self )
painter.setRenderHint( QPainter.Antialiasing )
# ICONS
self.mainRect = QRect( 0, 0, self.geometry().width(), self.geometry().height() )
iconSize = QSize( 58,58 )
iconPos = QPoint( ( self.mainRect.width()-iconSize.width())/2, 10 )
self.iconRect = QRect( iconPos, iconSize)
removeIconSize = QSize( 16, 16 )
removeIconPos = QPoint( iconSize.width()+iconPos.x()-10, iconPos.y()-5 )
self.removeIconRect = QRect( removeIconPos, removeIconSize )
# DRAW ICONS
self.drawIcon( painter, iconPos, iconSize )
# DRAW REMOVE ICON
if self.mouseOver:
self.drawIcon( painter, removeIconPos, removeIconSize, 'remove' )
def drawIcon( self, painter, pos, size, btn='main' ):
'''Draw icon with status'''
painter.drawRect( pos.x(), pos.y(), size.width(), size.height() )
def minimumSizeHint( self ):
return QSize( *self.fixedSize )
def sizeHint( self ):
return QSize( *self.fixedSize )
if __name__ == '__main__':
import os
app = QApplication( sys.argv )
w = QWidget()
w.setLayout( QGridLayout() )
btn2 = FancyButtonSmall()
w.layout().addWidget( btn2, 1, 0 )
w.show()
sys.exit( app.exec_() )
【问题讨论】:
【参考方案1】:在询问后答案变得显而易见的时刻之一:使用 enterEvent() 和 leaveEvent() 而不是 mouseMoveEvent() 会很有效。
【讨论】:
以上是关于工具提示停止 mouseMoveEvent的主要内容,如果未能解决你的问题,请参考以下文章
微信打开网站被提示已停止访问该网页不知道如何解决?有VJump,MaxJump和JumpTool三款工具可用!