PDA

View Full Version : Why does clicking a QToolButton generate a leaveEvent? Is there a workaround?



slobberchopz
24th December 2013, 20:50
For some reason, whenever the menu-button part of the QToolButton gets clicked, it generates a momentary leaveEvent (at least when it is in a toolbar). I even tested underMouse() in the leaveEvent, and it returns false. Why is this? Is there a way to fix this?

Sample for testing:



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


I'm using PyQt 4.10.1 Qt4.8.4 x64 with Py3.3.1 x64 with Win7

folibis
25th December 2013, 02:56
Right, you click on your button, it brings up the menu window and focus moved from button to menu

slobberchopz
25th December 2013, 07:43
Nope. Because a moment later it switches back to enterEvent(); and anyways, even if focus changed, the mouse didn't. leave/enterEvents reflect mouse position, not focus.

Well, I've found a way to avoid dealing with leave/enter that suits my needs. For anyone who does care, and who's PyQt version/setup presents such a problem, simply double check with QApplication.widgetAt(QCursor().pos()), that *does* return the correct info.