上的任何鼠标点击

上的任何鼠标点击

本文介绍了如何检测 PySide Gui 上的任何鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个功能,当在 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

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

这是我的目标

Mouseclick.gui_clicked.connect(do_something)

任何建议将不胜感激

推荐答案

Define mousePressEvent inside Main:

Define mousePressEvent inside Main:

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

这篇关于如何检测 PySide Gui 上的任何鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:47