【问题标题】:How to transfer a signal, which detects if the button is clicked or not?如何传输信号,检测按钮是否被点击?
【发布时间】:2018-06-12 21:13:59
【问题描述】:

我想问一下是否可以使用信号/插槽来传输信息,例如:“这个按钮被点击了吗?”

我在这里准备了一些代码....

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class Worker(QObject):
    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)

    @pyqtSlot(str, str, int, add_one_more_signal)
    def onJob(self, strA, strB, int1, add_one_more_signal):
        print(strA, strB, int1)

        # if the signal detects the btn1 was clicked:
        # print("button 1 is clicked; button 2 is not clicked")

        # if the signal detects the btn2 was clicked:
        # print("button 1 is not clicked; button 2 is clicked")



class MyApp(QWidget):
    signal = pyqtSignal(str, str, int,  add_one_more_signal)
    def __init__(self, parent= None):
        super(MyApp, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.btn1 = QPushButton("start 1", self)
        self.btn2 = QPushButton("start 2", self)
        self.btn1.clicked.connect(self.start)
        self.btn2.clicked.connect(self.start)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btn1)
        self.layout.addWidget(self.btn2)
        self.setLayout(self.layout)
        self.show()

    def start(self):
        otherClass = Worker()
        self.signal.connect(otherClass.onJob)
        self.signal.emit("foo", "baz", 10, self.btn1.clicked(True) or self.btn2.clicked(True)) # How to write this line?

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

请不要误会我的意思。我以我非常罕见的知识知道如何实现程序目的。我只想知道,如何传输信号,它检测按钮是否被点击。 --说实话,我也想知道Signal+Slot的能力。

在我的代码中有两个按钮。它们共享相同的子功能。 (正如我提到的,仅针对这个问题。)单击其中一个时,三个参数从 MyApp-Class 传输到 Worker-Class。

现在我要介绍第四个参数,我也写在上面的代码中。这第四个参数只做一个工作,即发送信息,按钮是否被点击。

所以我的问题是:如果可行,代码怎么写?

【问题讨论】:

  • @eyllanesc 是的。谢谢指正。
  • @eyllanesc 使用第四个参数应该只传输一个信息,我可以想象。如果单击此按钮,则信息应类似于:“单击此按钮。”
  • @eyllanesc 或者信息也可能是:“此信号来自按钮 1”

标签: python pyqt pyqt5


【解决方案1】:

一个可能的解决方案是发送显示按钮的文本,为此第四个参数必须是str类型,以获取发出我们使用sender()的信号的对象,在这种情况下sender()将成为被按下的对象,然后我们得到文本并发送它。

class Worker(QObject):
    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)

    @pyqtSlot(str, str, int, str)
    def onJob(self, strA, strB, int1, text):
        print(strA, strB, int1)
        if text == "start 1":
            print("button 1 is clicked")
        elif text == "start 2":
            print("button 2 is clicked")


class MyApp(QWidget):
    signal = pyqtSignal(str, str, int, str)
    def __init__(self, parent= None):
        super(MyApp, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.btn1 = QPushButton("start 1", self)
        self.btn2 = QPushButton("start 2", self)
        self.btn1.clicked.connect(self.start)
        self.btn2.clicked.connect(self.start)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btn1)
        self.layout.addWidget(self.btn2)
        self.setLayout(self.layout)
        self.show()

    def start(self):
        otherClass = Worker()
        btn = self.sender()
        self.signal.connect(otherClass.onJob)
        self.signal.emit("foo", "baz", 10, btn.text()) 

【讨论】:

  • 谢谢!但是它无法检测到点击了哪个按钮,因为 btn 实际上是不可用的。
  • @LucasMeier 自己解释得更好,也就是说btn 不可用,你抄我的代码了吗?我已经测试过了。
  • 对不起,我监督了一条非常重要的线路。 :-) 是的,它有效!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2012-07-29
  • 2013-05-09
  • 2013-07-26
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
相关资源
最近更新 更多