pyqt 进度条 [英] Progress bar with pyqt

查看:100
本文介绍了pyqt 进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序运行多个线程,其中一个控制界面,另一个每隔几秒钟启动一些功能.我希望计时线程在我这样做时更新进度条,我得到:

I have a program running many threads, one of them controls the interface and another launches some functions every few seconds. I want the timing thread to update a progress bar buy when I do, i get:

X Error: RenderBadGlyphSet (invalid GlyphSet parameter) 165
  Extension:    150 (RENDER)
  Minor opcode: 25 (RenderCompositeGlyphs32)
  Resource id:  0x25
X Error: RenderBadGlyphSet (invalid GlyphSet parameter) 165
  Extension:    150 (RENDER)
  Minor opcode: 25 (RenderCompositeGlyphs32)
  Resource id:  0x25
X Error: RenderBadGlyphSet (invalid GlyphSet parameter) 165
  Extension:    150 (RENDER)
  Minor opcode: 25 (RenderCompositeGlyphs32)
  Resource id:  0x25
X Error: RenderBadGlyphSet (invalid GlyphSet parameter) 165
  Extension:    150 (RENDER)
  Minor opcode: 25 (RenderCompositeGlyphs32)
  Resource id:  0x25
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread

这是我的代码:

#!/usr/bin/python
from PyQt4 import QtCore, QtGui
import time

class WTrainning(wMeta.WMeta, QtGui.QWidget):

    def __init__(self):
        super(WTrainning, self).__init__()

    def createUI(self):

        ...
        self.progressBar = QtGui.QProgressBar(self)
        self.progressBar.setGeometry(QtCore.QRect(30, 70, 481, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        QtCore.QObject.connect(self.progressBar, QtCore.SIGNAL("valueChanged(int)"), self.progressBar.setValue) 
        ...

    def modifyBarr(self, number):
        self.progressBar.setValue(number)

class Crono(QtCore.QThread):

    def __init__(self, parent):

        QtCore.QThread.__init__(self,parent)


    def checkStatus(self):

        for x in range(1,101):
            self.wtobject.modifyBarr(x)                         
            time.sleep(1)

我尝试过使用信号(新旧版本)并为 UI 更新创建一个专用线程,但没有任何效果.我使用的是 python 2.6.6 和 pyqt 4.7.4

I'd tried with signals (new and old version) and making a dedicated thread for UI updating, but nothing worked. I'm using python 2.6.6 and pyqt 4.7.4

推荐答案

你必须像这样为 Crono 对象定义一个信号:

You have to define a signal for the Crono object like this:

class Crono(QtCore.QThread):
    tick = QtCore.pyqtSignal(int, name="changed") #New style signal

    def __init__(self, parent):
        QtCore.QThread.__init__(self,parent)

    def checkStatus(self):
        for x in range(1,101):
            self.tick.emit(x)                     
            time.sleep(1)

然后将其连接到进度条的插槽.

Then connect it to a slot of the progress bar.

class WTrainning(wMeta.WMeta, QtGui.QWidget):

    def __init__(self):
        super(WTrainning, self).__init__()
        self.crono = Crono()

    def createUI(self):
        #Create GUI stuff here

        #Connect signal of self.crono to a slot of self.progressBar
        self.crono.tick.connect(self.progressBar.setValue)

您所做的是将progressBar 的SIGNAL valueChanged 连接到它自己的SLOT setValue

What you were doing is connect the SIGNAL valueChanged of the progressBar to its own SLOT setValue

这篇关于pyqt 进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆