2

按下按钮开始 100 轮循环。随着QLabel.setText()我们self.label从功能范围内更新clicked()

除了更新self.label,我们还想更新progressbar。但是由于progressbar它是一个局部变量,我们不能从onClick()函数内部对其进行更新。

在此处输入图像描述

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

2 回答 2

1

该代码使用以下方法声明progressbar连接到自定义“customSignal”的本地对象:

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

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

第一个参数self是将发出信号的对象。由于将执行“每秒睡眠处理”的函数是在Dialog我们在这里传递的主实例下声明的self

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

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

在此处输入图像描述

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()
于 2016-05-06T04:59:29.807 回答
1

将进度条声明为:

self.progressbar = ProgressBar(self)
于 2016-05-06T03:32:08.133 回答