【问题标题】:connecting multiple signals to将多个信号连接到
【发布时间】:2016-06-24 07:17:59
【问题描述】:

我目前正在开发控制系统 GUI,但在跨线程存储和访问数据时遇到了障碍。目前,我正在从仪表读取压力,并更新 UI 中的 LCD 显示屏,效果很好。我遇到的这个问题是当我试图控制我的系统时。我需要在同时更新压力显示的同时实时访问我的控制回路的压力表数据,但如果我尝试同时向压力表发送两个命令(控制反馈和显示),我会收到串行读取错误.因此,我想知道是否有一种好方法可以让函数从仪表中读取数据,存储它,并在必要时将数据发送到压力显示器和控制功能。这是我的简化代码:

类 MainWindow():

#has a bunch of buttons and images that are used
#Define the threading object
self.obj=Reader.worker()
self.thread=QThread()
self.obj.moveToThread(self.thread)

def PresDisplay(self,i):
     self.presNumber.display("%s", i)

def on_presReadButton_clicked(self):
     self.obj.intReady.connect(self.presDisplay)
     self.thread.started.connect(self.obj.collector)
     self.thread.start()

这部分代码运行良好。但是,从这里开始,我对自己应该做什么感到有些迷茫。在我的工人阶级中,我有以下内容: #

类工作者(QObject):

  intReady=pyqtSignal(str)
  pres=pyqtSignal(str)

  def collector(self):
    while True:
        i=gauge.check()   #read the pressure from the gauge
        self.pres.connect(self.presSender)
        self.pres.emit(i)

  def presSender(self,i):
        self.intReady.emit(i)

  def control(self, setpoint, i):
     #This method must take in the pressure setpoint from the main GUi thread and use the pressure readings from collector at any given time to conduct the close loop control. It will be initiated from the click of a button (not the pressure read button above)

我知道这是一个非常开放的问题,但可能有很多方法可以解决它。

如果有人有任何想法,我愿意尝试一下。三个星期以来,我一直在努力解决这个问题。

【问题讨论】:

  • 你为什么要将工作器上的信号连接到自身,只是为了发出具有相同参数的另一个信号?

标签: python multithreading user-interface pyqt


【解决方案1】:

您可能不想在循环的每次迭代中都重新连接信号。它将创建重复的连接,并且每次发出信号时都会多次调用相同的回调

while True:
    i=gauge.check() 
    self.pres.connect(self.presSender) # This is bad
    self.pres.emit(i)

您通常只想连接一次信号,通常使用__init__ 方法。在这种情况下,您发出一个信号 (pres) 只是为了发出另一个具有完全相同参数的信号 (intReady)。只需去掉pres 信号,直接从collector 方法发出intReady

while True:
    i=gauge.check()   #read the pressure from the gauge
    self.intReady.emit(i)

另外,我不确定gauge.check() 需要多长时间才能响应,但您可能希望在每次迭代中插入一个sleep,这样您就不会在只需要时不断向主线程发送数百万个信号分辨率约为 1-10 赫兹。

while True:
    i=gauge.check()   #read the pressure from the gauge
    self.intReady.emit(i)
    QtCore.QThread.msleep(100)

主线程也是如此。您可能不想在每次按下按钮时连接信号并重新启动线程。要么将 on_click 处理程序中的所有内容移到 __init__ 方法中,要么进行检查以仅连接信号并在尚未完成时启动线程。

【讨论】:

  • 感谢您的意见。你是绝对正确的,我已经改变了与主线程的连接。不过,我仍然很好奇是否有一种将多个信号连接到单个插槽的好方法。
  • 您可以将多个信号连接到一个插槽,它必须是有意义的。例如,您的小部件可能只有一个 refresh 函数。您可以将多个“onChanged”信号连接到该插槽,因此当任何小部件更改值时,它都会更新整个小部件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多