首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在QT设计器中生成动态进度条?

如何在QT设计器中生成动态进度条?
EN

Stack Overflow用户
提问于 2018-07-26 04:47:05
回答 1查看 0关注 0票数 0

我想制作一个动态进度条,它以恒定速率达到最大值。

代码语言:txt
复制
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1007, 837)
        self.textBrowser = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser.setGeometry(QtCore.QRect(170, 80, 661, 81))
        self.textBrowser.setObjectName("textBrowser")
        self.progressBar = QtWidgets.QProgressBar(Dialog)
        self.progressBar.setGeometry(QtCore.QRect(330, 320, 261, 32))
        #self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(210, -10, 691, 471))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(200, 490, 97, 30))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.label.raise_()
        self.textBrowser.raise_()
        self.label_2.raise_()
        self.progressBar.raise_()
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)


    def bar(self):
        print "here"
        self.completed = 0

        while self.completed < 100:
            self.completed += 0.0000000000  1
            self.progressBar.setValue(self.completed)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.textBrowser.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Cantarell\'; font-size:13.75pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">M-CAD</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">MALARIA COMPUTER AIDED DIAGNOSIS</p></body></html>"))
        self.label.setText(_translate("Dialog", "<html><head/><body><p><img src=\":/newPrefix/drdo-slide.jpg\"/></p></body></html>"))

import test_rc

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    ui.bar()
    Dialog.show()

    sys.exit(app.exec_())
EN

Stack Overflow用户

发布于 2018-07-26 14:12:19

while循环阻止了Qt事件循环的正常工作,解决方案是用GUI友好的对象(例如QTimer或更好的QTimeLine)实现相同的逻辑。

另一方面,建议不要修改Qt Designer提供的类,而是实现继承适当的小部件和设计的新类

代码语言:txt
复制
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        ...

    def retranslateUi(self, Dialog):
        ...

import test_rc

class Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)

    def bar(self):
        timeLine = QtCore.QTimeLine(1000, self)
        timeLine.setFrameRange(0, 100)
        timeLine.frameChanged.connect(self.progressBar.setValue)
        timeLine.start()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.bar()
    w.show()
    sys.exit(app.exec_())
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档