工具提示会停止mouseMoveEvent

时间:2012-05-17 03:49:42

标签: qt pyqt pyqt4 pyside

我刚注意到,在工具提示中移动鼠标光标似乎不会在PySide中返回mouseMoveEvent。

在我的情况下,当鼠标移动到主图标区域时,我会在主图标上动态显示迷你图标,并在鼠标离开主图标区域时再次隐藏迷你图标,这会导致麻烦。因此,如果用户将鼠标光标移动到大工具提示上,然后在主按钮区域外移动,则鼠标事件从未注册新位置,并且迷你图标保持可见,即使鼠标不再位于主图标区域上方

我想我可以通过创建自定义QToolTip并将其远离主按钮区域来解决此问题,因此永远不可能通过工具提示离开主按钮区域。但这看起来很难看。

有没有人知道如何使用mouseMoveEvent进行工具提示注册,这样可以避免?

以下是一个示例代码段(希望它格式正确,我以前从未在此处发布):

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 个答案:

答案 0 :(得分:0)

在询问之后答案变得明显的那些时刻之一:使用enterEvent()和leaveEvent()而不是mouseMoveEvent()可以处理。