如何用Signal控制QProgressBar

时间:2016-05-06 03:20:34

标签: python qt pyqt qprogressbar

按下按钮开始100轮循环。使用QLabel.setText(),我们会从self.label函数范围内更新clicked()

除了更新self.label之外,我们还希望更新progressbar。 但由于progressbar是局部变量,我们无法从onClick()函数内部更新它。

enter image description here

import time

class ProgressBar(QtGui.QProgressBar):
    def __init__(self, parent=None, total=20):
        super(ProgressBar, self).__init__(parent=parent)

        self.setMinimum(1)
        self.setMaximum(105)        
        self.setTextVisible(True) 

    def set_to_value(self, value):
        self.setValue(value)
        QtGui.qApp.processEvents()

    def closeEvent(self, event):
        self._active=False


class Dialog(QtGui.QDialog):
    def __init__(self):
        super(QtGui.QDialog,self).__init__()

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.label = QtGui.QLabel('idle...')
        layout.addWidget(self.label)

        progressbar = ProgressBar(self)
        layout.addWidget(progressbar) 

        button = QtGui.QPushButton('Process')
        button.clicked.connect(self.onClick)
        layout.addWidget(button) 

    def onClick(self):
        for i in range(101):
            message = '...processing %s of 100'%i
            self.label.setText(message)
            QtGui.qApp.processEvents()
            time.sleep(1)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    dialog = Dialog()
    dialog.resize(300, 100)
    dialog.show()
    app.exec_()

2 个答案:

答案 0 :(得分:1)

将进度条声明为:

self.progressbar = ProgressBar(self)

答案 1 :(得分:0)

代码使用以下命令声明连接到自定义'customSignal`的本地progressbar对象:

QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar, QtCore.SLOT("setValue(int)"))

将四个参数传递给QtCore.QObject.connect()

第一个参数self是将发出信号的对象。由于将执行“sleep-every-second-processing”的函数在主Dialog实例下声明,我们在这里传递self

第二个参数是信号本身的名称:'customSignal'。

progressbar对象用作第三个,其方法setValue用作第四个参数。

enter image description here

class ProgressBar(QtGui.QProgressBar):
    def __init__(self, parent=None):
        super(ProgressBar, self).__init__(parent=parent)

class Dialog(QtGui.QDialog):
    def __init__(self):
        super(QtGui.QDialog,self).__init__()
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.label = QtGui.QLabel('idle...')
        layout.addWidget(self.label)

        progressbar = ProgressBar(self)
        QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar, QtCore.SLOT("setValue(int)"))

        layout.addWidget(progressbar) 

        button = QtGui.QPushButton('Process')
        button.clicked.connect(self.clicked)
        layout.addWidget(button) 

    def clicked(self):
        for value in range(101):
            message = '...processing %s of 100'%value
            self.label.setText(message)
            self.emit(QtCore.SIGNAL("customSignal(int)"), value)
            QtGui.qApp.processEvents()
            time.sleep(1)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    dialog = Dialog()
    dialog.resize(300, 100)
    dialog.show()
    app.exec_()

--------------------

除了链接到进度条方法之外,这是相同解决方案的变体。     进口时间

class ProgressBar(QtGui.QProgressBar):
    def __init__(self, parent=None):
        super(ProgressBar, self).__init__(parent=parent)

    def set_to_value(self, value):
        self.setValue(value)
        QtGui.qApp.processEvents()
        return True

    def closeEvent(self, event):
        self._active=False

class Dialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self, parent=None)
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.label = QtGui.QLabel('idle...')
        layout.addWidget(self.label)

        progressbar = ProgressBar(self)
        QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar.set_to_value )
        layout.addWidget(progressbar) 

        button = QtGui.QPushButton('Process')
        button.clicked.connect(self.clicked)
        layout.addWidget(button) 

    def clicked(self):
        for value in range(101):
            message = '...processing %s of 100'%value
            self.label.setText(message)
            self.emit(QtCore.SIGNAL("customSignal(int)"), value)
            QtGui.qApp.processEvents()
            time.sleep(1)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    dialog = Dialog()
    dialog.resize(300, 100)
    dialog.show()
    app.exec_()

======================

此代码现在将自定义信号链接到Qt中称为Slot的函数。

from PySide import QtCore, QtGui
import time

class ProgressBar(QtGui.QProgressBar):
    def __init__(self, parent=None):
        super(ProgressBar, self).__init__(parent=parent)

    @QtCore.Slot(int)
    def set_to_value(self, value):
        self.setValue(value)
        QtGui.qApp.processEvents()
        return True

    def closeEvent(self, event):
        self._active=False

class Dialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self, parent=None)
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.label = QtGui.QLabel('idle...')
        layout.addWidget(self.label)

        progressbar = ProgressBar(self)
        # QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar.set_to_value )
        QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar, QtCore.SLOT("set_to_value(int)"))

        layout.addWidget(progressbar) 

        button = QtGui.QPushButton('Process')
        button.clicked.connect(self.clicked)
        layout.addWidget(button) 

    def clicked(self):
        for value in range(101):
            message = '...processing %s of 100'%value
            self.label.setText(message)
            self.emit(QtCore.SIGNAL("customSignal(int)"), value)
            QtGui.qApp.processEvents()
            time.sleep(1)

if __name__ == '__main__':
    dialog = Dialog()
    dialog.resize(300, 100)
    dialog.show()