gpt4 book ai didi

python - 如何检测 PySide Gui 上的任何鼠标点击?

转载 作者:太空狗 更新时间:2023-10-30 03:01:54 25 4
gpt4 key购买 nike

我正在尝试实现一项功能,以便在 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_())

这是我的目标

Mouseclick.gui_clicked.connect(do_something)

如有任何建议,我们将不胜感激

最佳答案

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

关于python - 如何检测 PySide Gui 上的任何鼠标点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com