2

我想问在我开始一些措施后如何刷新 QLCDNumbers。我创建了一个 GUI 线程来将信号连接到 QLCDNumbers,如下所示:

class BtDialog(QtGui.QDialog, Dlg):
  def __init__(self):
    QtGui.QDialog.__init__(self)
    self.setupUi(self)
    self.thread = WorkerThread()

    #Configure slots
    self.connect(self.startButton, QtCore.SIGNAL("clicked()"), self.onStart)
    self.connect(self.stopButton, QtCore.SIGNAL("clicked()"), self.onStop)

    #QLCDNumber Slot
    self.connect(self.thread, self.thread.voltage, self.lcdVoltage.display)

 def onStart(self):
    self.thread.start()

 def onStop(self):
     self.emit(self.thread.voltage, 0) #Trying to refresh
     abort()

这里我连接了两个按钮,一个用于启动工作线程,另一个用于停止进程。当我停止该过程时,我想通过显示“0”来刷新 QLCDNumber,但它不起作用。在工作线程中,我像这样初始化信号:

def __init__(self, parent = None):
    QtCore.QThread.__init__(self, parent)
    self.voltage = QtCore.SIGNAL("voltage")

当进程运行时,我发出信号

self.emit(self.voltage, volt_act)

测量后。到目前为止有效。但是在我想再次启动工作进程时停止后,信号不会再次发送到 QLCDNumber 。为此,我必须重新启动 GUI。如何解决我的两个问题,即我想刷新 QLCDNumber 并在停止和刷新再次发出信号后解决这个问题?

4

1 回答 1

1

无法从您发布的代码中判断问题出在哪里,但这应该可以帮助您修改它,还可以查看文档以获取新型信号/插槽连接进一步参考(模态对话框、计时器等)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time

from PyQt4 import QtGui, QtCore

class MyThread(QtCore.QThread):
    countChange = QtCore.pyqtSignal(int)
    countReset  = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)
        self.stopped = QtCore.QEvent(QtCore.QEvent.User)

    def start(self):
        self.stopped.setAccepted(False)
        self.count = 0

        super(MyThread, self).start()

    def run(self):
        while not self.stopped.isAccepted():
            self.count += 1
            self.countChange.emit(self.count)
            time.sleep(1)

        self.countReset.emit(0)

    def stop(self):
        self.stopped.setAccepted(True)

class MyWindow(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.lcdNumber = QtGui.QLCDNumber(self)

        self.pushButtonStart = QtGui.QPushButton(self)
        self.pushButtonStart.setText("Start")
        self.pushButtonStart.clicked.connect(self.on_pushButtonStart_clicked)

        self.pushButtonStop = QtGui.QPushButton(self)
        self.pushButtonStop.setText("Stop")
        self.pushButtonStop.clicked.connect(self.on_pushButtonStop_clicked)

        self.pushButtonDone = QtGui.QPushButton(self)
        self.pushButtonDone.setText("Done")
        self.pushButtonDone.clicked.connect(self.on_pushButtonDone_clicked)

        self.layoutHorizontal = QtGui.QHBoxLayout(self)
        self.layoutHorizontal.addWidget(self.lcdNumber)
        self.layoutHorizontal.addWidget(self.pushButtonStart)
        self.layoutHorizontal.addWidget(self.pushButtonStop)
        self.layoutHorizontal.addWidget(self.pushButtonDone)

        self.thread = MyThread(self)
        self.thread.countChange.connect(self.lcdNumber.display)
        self.thread.countReset.connect(self.lcdNumber.display)

    @QtCore.pyqtSlot()
    def on_pushButtonStart_clicked(self):
        self.thread.start()

    @QtCore.pyqtSlot()
    def on_pushButtonStop_clicked(self):
        self.thread.stop()

    @QtCore.pyqtSlot()
    def on_pushButtonDone_clicked(self):
        sys.exit()

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.exec_()

    sys.exit(app.exec_())
于 2013-03-18T21:53:01.353 回答