マノスChaniotakis:
私は2つのボタン、QDialogでbuttonAとbuttonBを持っている場合。私のマウスはbuttonAをクリックすると、まだbuttonBにクリックされたマウスでの私のカーソルを移動すると、私はHoverEnterイベントを検出傾けます。これはおそらくbuttonAでマウスのグラブを設定するのPyQtする予定です。マウスがクリックされたときにbuttonBに入っている間、私は入ってくるイベントを検出する方法を見つける傾けます。サンプルコード:
class MainDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__()
self.installEventFilter(self)
self.buttonA= QtWidgets.QPushButton(MainDialog)
self.buttonB= QtWidgets.QPushButton(MainDialog)
self.buttonA.installEventFilter(self)
self.buttonB.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.HoverEnter:
print("HOVERE ENTER")
return True
私のプラットフォームは、Python 3.7とPyQtは上のWindows 10で5
(イベントフィルタで)まだ運:私はまた、マウスのグラブを失うことなく、またself.buttonB.underMouse場合()するために、最初のマウスクリックの後に()イベントを無視しようとしています。
musicamante:
短い答えは、マウスボタンがボタン押下イベントを受け入れたウィジェット上でクリックされたときにということであることを、マウスボタンがリリースされるまでウィジェットは、マウス移動イベントを受け取ることになります。
あなたが欲しいものを得るためにあなたを防ぐため、あなたのコード内の様々な問題があります。
- あなたはHoverEnterに対してチェックされているが、これは唯一のウィジェットを入力するときに何のマウスボタンが押されなかったときに動作します。
- あなたは返すのを忘れ
super().eventFilter(source, event)
た場合にイベントタイプが(これは非常に重要です)異なっていました。 - あなたが使用しているクラスをボタンのパラメータ引数として、あなたは、インスタンスを(使用する必要があります
self
)の代わりに、 - 「クラス」は小文字です。
最後に、別のウィジェットは、ボタンのクリックによってマウスを「つかんで」いるとき、あなたはそれだけを見つけるために、マウス移動イベントを使用することができるので、一度だけそれを得るための別の方法を見つける必要がありますので、イベントは、追跡することができません入力してください。
これが可能な実装であります:
class MainDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__()
layout = QtWidgets.QHBoxLayout(self)
self.buttonA = QtWidgets.QPushButton('A')
self.buttonB = QtWidgets.QPushButton('B')
layout.addWidget(self.buttonA)
layout.addWidget(self.buttonB)
self.buttonA.installEventFilter(self)
self.buttonB.installEventFilter(self)
self.enterButton = self.pressedButton = None
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonPress and event.button() == QtCore.Qt.LeftButton:
self.pressedButton = source
elif event.type() == QtCore.QEvent.MouseMove:
widget = QtWidgets.QApplication.widgetAt(event.globalPos())
if widget in (self.buttonA, self.buttonB):
if widget != self.pressedButton:
if self.enterButton != widget:
self.enterButton = widget
print('Button {} entered!'.format(widget.text()))
else:
self.enterButton = None
return super().eventFilter(source, event)