PyQt5 - 使用QPixmap更新QLabel不起作用

时间:2017-01-29 20:58:55

标签: python pyqt

这是我的QWidget课程:

class Window(QWidget):
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.width = 0
        self.height = 0
        self.move(125,250)
        self.img = None

        self.label = QLabel()
        self.layout = QHBoxLayout()
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)

        self.setUpdatesEnabled(True)
        self.label.setUpdatesEnabled(True)

        self.show()
    def setWidth(self,width):
        self.width = width
        self.resize(self.width,self.height)
        self.img = QImage(self.width,self.height,QImage.Format_ARGB32)
    def setHeight(self,height):
        self.height = height
        self.resize(self.width, self.height)
        self.img = QImage(self.width, self.height, QImage.Format_ARGB32)
    def setPixel(self,x,y,color):
        self.img.setPixel(x,y,QColor(color.r,color.g,color.b,1).rgb())
    def updateScreen(self,samples):
        painter = QPainter(self.img)
        painter.setPen(QPen(Qt.yellow))
        painter.drawText(10,10,str(samples))
        self.label.setPixmap(QPixmap.fromImage(self.img))
        self.label.update()
        self.update()

我在名为Window的第二个类中有Renderer个实例,它使用Window这样的对象:

class Renderer:
    def __init__(self,scene,width,height,camera):
        self.buffer = [0] * (self.width * self.height * 3 + 1)

        self.qApp = QApplication(sys.argv)
        self.window = Window()
        self.window.setWidth(self.width)
        self.window.setHeight(self.height)
    def updateScreen(self):
        i = 0
        for w in range(0,self.width):
            for h in range(0,self.height):
                r = self.buffer[i * 3 + 0] / self.samples
                g = self.buffer[i * 3 + 1] / self.samples
                b = self.buffer[i * 3 + 2] / self.samples
                self.window.setPixel(w,h,Color(r,g,b))
                i += 1
        self.window.updateScreen(self.samples)
    def renderOneSample(self):
        self.calculate()

我像这样使用Renderer

renderer = Renderer(scene,125,125,Point3D(0.0,0.0,10.0))

for i in range(0,4):
    renderer.calculate()
    renderer.updateScreen()

sys.exit(renderer.qApp.exec_())

我想看看我的GUI(1,2,3,4)中的每一步,而不仅仅是第4步。我必须在另一个线程中更新GUI,我该怎么做?

0 个答案:

没有答案