出于某种原因,每当单击QToolButton的菜单按钮部分时,它都会生成一个瞬间的leaveEvent(至少在它位于工具栏中时)。我甚至在leaveEvent中测试了underMouse(),它返回false。为什么是这样?有办法解决这个问题吗?
测试样本(Py3.3,更改py2.7的super()):
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
toolbar = QToolBar(self)
toolbar.addWidget(ToolButton())
class ToolButton(QToolButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setText('test')
self.setPopupMode(QToolButton.MenuButtonPopup)
self.setMenu(QMenu())
self.menu().addAction('Stub')
def enterEvent(self, event):
print('entered')
super().enterEvent(event)
def leaveEvent(self, event):
print('left')
super().leaveEvent(event)
if __name__ == '__main__':
import sys
application = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(application.exec_())
答案 0 :(得分:1)
以下内容可用于双重检查;与leaveEvent不同,它总是返回正确的信息:
def leaveEvent(self, event):
if not QApplication.widgetAt(QCursor().pos()) is self:
#do stuff
super().leaveEvent(event)