进度条与pyqt

时间:2012-03-13 10:34:24

标签: python user-interface qt4 pyqt pyqt4

我有一个运行许多线程的程序,其中一个控制接口,另一个每隔几秒启动一些函数。我希望时间线程在我做的时候更新进度条购买,我得到:

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

1 个答案:

答案 0 :(得分:4)

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

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)

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

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