简体   繁体   English

如何检测任何鼠标点击PySide Gui?

[英]How to detect any mouse click on PySide Gui?

I am trying implement a feature such that when a mouse is clicked on the gui, a function is triggered 我正在尝试实现一个功能,当在gui上单击鼠标时,会触发一个功能

Below is my mouse click detection, it doesn't work when I click on any part of the gui 下面是我的鼠标点击检测,当我点击gui的任何部分时,它不起作用

from PySide.QtCore import *
from PySide.QtGui import *

import sys


class Main(QWidget):


    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout  = QHBoxLayout(self)
        layout.addWidget(QLabel("this is the main frame"))
        layout.gui_clicked.connect(self.anotherSlot)

    def anotherSlot(self, passed):
        print passed
        print "now I'm in Main.anotherSlot"


class MyLayout(QHBoxLayout):
    gui_clicked = Signal(str)

    def __init__(self, parent=None):
        super(MyLayout, self).__init__(parent)

    def mousePressEvent(self, event):
        print "Mouse Clicked"
        self.gui_clicked.emit("emit the signal")



a = QApplication([])
m = Main()
m.show()
sys.exit(a.exec_())

This is my goal 这是我的目标

Mouseclick.gui_clicked.connect(do_something)

Any advice would be appreciated 任何意见,将不胜感激

Define mousePressEvent inside Main : Main定义mousePressEvent

from PySide.QtCore import *
from PySide.QtGui import *

import sys


class Main(QWidget):


    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout  = QHBoxLayout(self)
        layout.addWidget(QLabel("this is the main frame"))

    def mousePressEvent(self, QMouseEvent):
        #print mouse position
        print QMouseEvent.pos()


a = QApplication([])
m = Main()
m.show()
sys.exit(a.exec_())

This can get complicated depending on your needs. 根据您的需要,这可能会变得复杂。 In short, the solution is an eventFilter installed on the application. 简而言之,解决方案是在应用程序上安装eventFilter This will listen the whole application for an event. 这将听取整个应用程序的事件。 The problem is "event propagation". 问题是“事件传播”。 If a widget doesn't handle an event, it'll be passed to the parent (and so on). 如果窗口小部件不处理事件,它将被传递给父窗口(依此类推)。 You'll see those events multiple times. 你会多次看到这些事件。 In your case, for example QLabel doesn't do anything with a mouse press event, therefore the parent (your main window) gets it. 在您的情况下,例如QLabel对鼠标按下事件没有任何作用,因此父(您的主窗口)获取它。

If you actually filter the event (ie you don't want the original widget to respond to the event), you won't get that problem. 如果您实际过滤了事件(即您不希望原始窗口小部件响应事件),您将不会遇到该问题。 But, I doubt that this is your intent. 但是,我怀疑这是你的意图。

A simple example for just monitoring: 一个简单的监控示例:

import sys
from PySide import QtGui, QtCore

class MouseDetector(QtCore.QObject):
    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            print 'mouse pressed', obj
        return super(MouseDetector, self).eventFilter(obj, event)

class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        layout = QtGui.QHBoxLayout()
        layout.addWidget(QtGui.QLabel('this is a label'))
        layout.addWidget(QtGui.QPushButton('Button'))

        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    mouseFilter = MouseDetector()
    app.installEventFilter(mouseFilter)

    main = MainWindow()
    main.show()

    sys.exit(app.exec_())

You can see that, clicking on the QLabel will give you something like: 你可以看到,点击QLabel会给你一些类似的东西:

mouse pressed <PySide.QtGui.QLabel object at 0x02B92490>
mouse pressed <__main__.MainWindow object at 0x02B92440>

Because, QLabel receives the event and since it doesn't do anything with it, it's ignored and passed to the parent ( MainWindow ). 因为, QLabel收到事件,因为它没有对它做任何事情,它被忽略并传递给父( MainWindow )。 And it's caught by the filter/monitor again. 它再次被过滤器/监视器捕获。

Clicking on the QPushButton doesn't have any problem because it uses that event and does not pass to the parent. 单击QPushButton没有任何问题,因为它使用该事件而不传递给父项。

PS: Also note that this can cause performance problems since you are inspecting every single event in the application. PS: 另请注意,由于您正在检查应用程序中的每个事件,因此可能会导致性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM