0

我刚刚注意到在工具提示上移动鼠标光标似乎不会在 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_() )
4

1 回答 1

0

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

于 2012-07-02T07:34:03.413 回答