如何从QWiget接收到更改获取对象的信号?

时间:2019-06-16 16:46:10

标签: python pyqt pyqt5

你好,我有一个QWidget,如果单击它,我想获取该对象(我单击的QWidget元素)吗?

我已经找到了一些代码,但是我只能从

获取MouseClickEvent。

self.widget_34.mouseReleaseEvent = lambda事件:self.myfunction(event)

2 个答案:

答案 0 :(得分:1)

尽管@Cin提供的解决方案很有趣,但是它存在一个严重的问题:它取消了小部件的mousePressEvent,因此小部件失去了按下小部件时可能具有的行为,例如按钮no不再发出点击信号,其他小部件也将遇到相同的问题。

一种不太麻烦的解决方案是使用eventFilter:

import sys
import weakref
from PyQt5 import QtCore, QtWidgets


class ClickListener(QtCore.QObject):
    clicked = QtCore.pyqtSignal(QtWidgets.QWidget)

    def addWidget(self, widget, other_widget=None):
        if not hasattr(self, "_widgets"):
            self._widgets = {}
        widget.installEventFilter(self)
        self._widgets[widget] = widget if other_widget is None else other_widget
        weakref.ref(widget, self.removeWidget)

    def eventFilter(self, obj, event):
        if (
            obj in self._widgets
            and event.type() == QtCore.QEvent.MouseButtonPress
        ):
            self.clicked.emit(self._widgets[obj])
        return super(ClickListener, self).eventFilter(obj, event)

    def removeWidget(self, widget):
        if hasattr(self, "_widgets"):
            if widget in self._widgets:
                del self._widgets[widget]


class App(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        button = QtWidgets.QPushButton("Press Me")
        label = QtWidgets.QLabel("Stack Overflow")
        spinBox = QtWidgets.QSpinBox()
        te = QtWidgets.QTextEdit()

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(button)
        lay.addWidget(label)
        lay.addWidget(spinBox)
        lay.addWidget(te)

        listener = ClickListener(self)
        listener.clicked.connect(self.onClicked)
        listener.addWidget(button)
        listener.addWidget(label)
        listener.addWidget(spinBox.lineEdit(), spinBox)
        listener.addWidget(te.viewport(), te)

    def onClicked(self, obj):
        print("Clicked, from", obj)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

我不确定这是否是一个合适的解决方案,但我认为您可以使用 functools 模块的部分方法。就此模块而言,可伸缩对象可被视为函数。在这里您可以看到我的示例,

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
import functools

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(200,200,200,200)

        self.button = QPushButton('Button', self)
        self.button.move(50,50)

        self.label = QLabel(self)
        self.label.setText("Label")
        self.label.move(100,100)

        self.items = [self.button, self.label]

        for i in self.items:
            i.mousePressEvent = functools.partial(self.getClickedItem, source_object=i)

        self.show()

    def getClickedItem(self, event, source_object=None):
        print("Clicked, from", source_object)
        #item text
        #print(source_object.text()) 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())