2013-03-13 85 views
2

我想問一下在我開始採取一些措施後如何刷新QLCDNumber。 我創建了一個GUI線程的信號連接到QLCDNumbers那樣:Python:我怎樣才能刷新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,並在停止並刷新信號後再次發出信號?

回答

1

說不出哪裏的問題是從您發佈的代碼,但是這應該幫助你修改它,也檢出文檔爲new-style signal/slot connectionsfurther 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_())