简体   繁体   中英

Python: How can I refresh QLCDNumbers + emiting again after stopping

I want to ask how is it possible to refresh QLCDNumbers after I have started some measures. I created an GUI thread to connect the signals to QLCDNumbers like that:

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

Here I connect two buttons, one for starting the worker thread and the other to stop the process. When I stop the process I want to refresh the QLCDNumber by displaying '0' but it doesn't work. In worker thread i initialize the signal like that:

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

And when the process runs I emit the signal with

self.emit(self.voltage, volt_act)

after measuring. That works so far. But after stopping when I want to start the worker process again the signal doesn't emit to QLCDNumber again. For that I have to restart the GUI. How can I fix the two problems of mine for that I want to refresh QLCDNumber and over that after stopping and refreshing emitting the signal again?

Can't tell where the issue is from the code you posted, but this should help you modify it, also checkout the docs for new-style signal/slot connections and further reference (modal dialogs, timers, etc) :

#!/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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM