python pyqt支持两个值的堆叠进度条吗?

uqzxnwby  于 2023-02-02  发布在  Python
关注(0)|答案(3)|浏览(132)

类似于this question,但针对pyqt。我有一个应用程序,它有两个线程,其中一个线程处理一些数据(耗时),第二个线程显示结果并要求对结果进行验证。我想在进度条中显示处理的对象数。但是,我希望显示用户验证的对象数。处理的对象数将始终等于或大于验证的对象数(因为你无法验证未验证的内容)。本质上,它有点像YouTube视频的加载栏或其他东西,显示灰色部分为"加载",红色部分为"监视"。pyqt中是否支持这种功能?QProgressBar的文档似乎没有暗示有任何支持。使用PyQt5和Python 3.6。
它应类似于以下内容:

这里有一个最小可行的代码,有两个单独的进度条,一个为处理的对象数量,另一个为验证的数量,但我希望他们重叠...

import sys

from PyQt5.QtWidgets import (QApplication, QDialog,
                             QProgressBar, QPushButton)

class Actions(QDialog):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')
        self.objectsToProcess = 100
        self.objectsProcessed = 0
        self.objectsVerified = 0

        self.processProgress = QProgressBar(self)
        self.processProgress.setGeometry(5, 5, 300, 25)
        self.processProgress.setMaximum(self.objectsToProcess)

        self.verifyProgress = QProgressBar(self)
        self.verifyProgress.setGeometry(5, 35, 300, 25)
        self.verifyProgress.setMaximum(self.objectsToProcess)

        self.processButton = QPushButton('Process', self)
        self.processButton.move(5, 75)

        self.verifyButton = QPushButton('Verify', self)
        self.verifyButton.move(90, 75)

        self.show()

        self.processButton.clicked.connect(self.process)
        self.verifyButton.clicked.connect(self.verify)

    def process(self):
        if self.objectsProcessed + 1 < self.objectsToProcess:
            self.objectsProcessed += 1
            self.processProgress.setValue(self.objectsProcessed)

    def verify(self):
        if self.objectsVerified < self.objectsProcessed:
            self.objectsVerified += 1
            self.verifyProgress.setValue(self.objectsVerified)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Actions()
    sys.exit(app.exec_())

上述代码的结果:

sgtfey8w

sgtfey8w1#

一个可能的解决方案是在QProgressBar中创建一个新属性来显示可选的进度,并且我们可以使用QProxyStyle来进行绘制:

from PyQt5 import QtCore, QtGui, QtWidgets

class ProxyStyle(QtWidgets.QProxyStyle):
    def drawControl(self, element, option, painter, widget):
        if element == QtWidgets.QStyle.CE_ProgressBar:
            super(ProxyStyle, self).drawControl(element, option, painter, widget)
            if hasattr(option, 'alternative'):
                alternative = option.alternative

                last_value = option.progress
                last_pal = option.palette
                last_rect = option.rect

                option.progress = alternative
                pal = QtGui.QPalette()
                # alternative color
                pal.setColor(QtGui.QPalette.Highlight, QtCore.Qt.red)
                option.palette = pal
                option.rect = self.subElementRect(QtWidgets.QStyle.SE_ProgressBarContents, option, widget)
                self.proxy().drawControl(QtWidgets.QStyle.CE_ProgressBarContents, option, painter, widget)

                option.progress = last_value 
                option.palette = last_pal
                option.rect = last_rect
            return
        super(ProxyStyle, self).drawControl(element, option, painter, widget)

class ProgressBar(QtWidgets.QProgressBar):
    def paintEvent(self, event):
        painter =  QtWidgets.QStylePainter(self)
        opt = QtWidgets.QStyleOptionProgressBar()
        if hasattr(self, 'alternative'):
            opt.alternative = self.alternative()
        self.initStyleOption(opt)
        painter.drawControl(QtWidgets.QStyle.CE_ProgressBar, opt)

    @QtCore.pyqtSlot(int)
    def setAlternative(self, value):
        self._alternative = value
        self.update()

    def alternative(self):
        if not hasattr(self, '_alternative'):
            self._alternative = 0
        return self._alternative

class Actions(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')

        self.objectsToProcess = 100
        self.objectsProcessed = 0
        self.objectsVerified = 0

        self.progress_bar = ProgressBar(maximum=self.objectsToProcess)
        self.process_btn = QtWidgets.QPushButton('Process')
        self.verify_btn = QtWidgets.QPushButton('Verify')

        self.process_btn.clicked.connect(self.process)
        self.verify_btn.clicked.connect(self.verify)

        lay = QtWidgets.QGridLayout(self)
        lay.addWidget(self.progress_bar, 0, 0, 1, 2)
        lay.addWidget(self.process_btn, 1, 0)
        lay.addWidget(self.verify_btn, 1, 1)

    def process(self):
        if self.objectsProcessed + 1 < self.objectsToProcess:
            self.objectsProcessed += 1
            self.progress_bar.setValue(self.objectsProcessed)

    def verify(self):
        if self.objectsVerified < self.objectsProcessed:
            self.objectsVerified += 1
            self.progress_bar.setAlternative(self.objectsVerified)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle(ProxyStyle(app.style()))
    w = Actions()
    w.show()
    sys.exit(app.exec_())

oug3syen

oug3syen2#

感谢@eyllanesc提供了一个健壮的解决方案,我选择了一个更轻松(公认的是有点黑客)的解决方案,即重叠两个进度条,并使用QGraphicsOpacityEffect使顶部的进度条稍微透明。

# Opaque prog bar
    self.verifyProgress = QProgressBar(self)
    self.verifyProgress.setGeometry(5, 5, 300, 25)
    self.verifyProgress.setMaximum(self.objectsToProcess)
    self.verifyProgress.setFormat('%p% /                 ')
    self.verifyProgress.setAlignment(Qt.AlignCenter)

    # Must set the transparent prog bar second to overlay on top of opaque prog bar
    self.processProgress = QProgressBar(self)
    self.processProgress.setGeometry(5, 5, 300, 25)
    self.processProgress.setMaximum(self.objectsToProcess)
    self.processProgress.setFormat('   %p%')
    self.processProgress.setAlignment(Qt.AlignCenter)
    op = QGraphicsOpacityEffect(self.processProgress)
    op.setOpacity(0.5)
    self.processProgress.setGraphicsEffect(op)

结果:

snz8szmq

snz8szmq3#

我最近需要做一些类似的事情,并选择使用渐变颜色的进度条块,因为我需要使用样式表了。

def set_pb_value(self, pb, value_1, value_2):
        if value_2 > value_1:
            pb.setValue(value_2)
            pb.setFormat("{} / {}".format(value_1, value_2))
            pb.setStyleSheet('QProgressBar::chunk {' +
                         'background-color: qlineargradient(spread:pad, x1:' + str(value_1/pb.maximum()) + ', y1:0, x2:' +
                         str(value_1/value_2) + ', y2:0, stop:' + str(value_1/value_2) + ' rgba(0, 255, 0, 255), stop:1 '
                                            'rgba(255, 0, 0, 255)); width: -1px; margin: -1px;}')
        else:
            pb.setValue(value_1)
            pb.setFormat("%v")

我的值是整数,所以value_1/pb.maximum()对我来说是必要的,但根据您的需要更改它。我还对其他样式表和进度条边距有一些问题,这就是为什么它们现在设置为-1,您可能不需要包括它。

相关问题