2012-05-17 54 views
0

我只注意到跨越提示移動鼠標光標似乎並沒有在PySide返回mouseMoveEvent。工具提示停止mouseMoveEvent

在我的情況下,它會造成麻煩,因爲當鼠標移動到主圖標區域時,我在主圖標上動態顯示小圖標,並在鼠標離開主圖標區域時再次隱藏小圖標。因此,如果用戶將鼠標光標移動到大的工具提示上,然後在主按鈕區域外結束,則即使鼠標不再位於主圖標區域上,鼠標事件也不會註冊新位置,並且小圖標保持可見狀態。

我想我可以解決此問題通過創建自定義QToolTip並從主鍵區定位它扔掉,所以它永遠不可能通過提示離開主鍵區。但那似乎很難看。

有誰有一個想法如何使工具提示與mouseMoveEvent註冊,以便thie可避免?

下面是一個例子片斷(希望它會被格式化吧,我以前從來沒有張貼在這裏):

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_()) 

回答

0

一凡問後答案就顯而易見正確的時刻:使用enterEvent()和leaveEvent()而不是mouseMoveEvent()是一種享受。

相關問題