如何更改已在QWidget中设置和显示的QImage?

时间:2017-05-14 12:32:34

标签: python-3.x pyqt4

我想用鼠标更改图像。所以,每次我点击某个地方,图像都应该改变。我只能展示一次图像。因此,我需要将显示图像所需的所有内容的初始化与负责构建图像的代码部分分开。

这是我到目前为止所得到的

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSlot

class Example(QWidget):



    def __init__(self):
        super(Example, self).__init__()
        self.gx=1
        self.gy=1
        self.tlb=QLabel()
        self.lbl=QLabel()
        self.image = QImage(512, 512, QImage.Format_RGB32)
        self.hbox = QHBoxLayout()
        self.pixmap = QPixmap()
        self.initUI()

    def mousePressEvent(self, QMouseEvent):
        px = QMouseEvent.pos().x()
        py = QMouseEvent.pos().y()

        size = self.frameSize()

        self.gx = px-size.width()/2
        self.gy = py-size.height()/2

        self.fillImage()


    def initUI(self):    
        self.hbox = QHBoxLayout(self)
        self.pixmap = QPixmap()

        size = self.frameSize()

        self.fillImage()

        self.lbl = QLabel(self)
        self.lbl.setPixmap(self.pixmap)

        self.hbox.addWidget(self.lbl)
        self.setLayout(self.hbox)

        self.move(300, 200)
        self.setWindowTitle('Red Rock')

        self.tlb = QLabel(str(self.gx)+" : "+str(self.gy), self)
        self.tlb.move(12,3)
        self.show()  

    def fillImage(self):
        for x in range(0, 512):
            t = -1+(x/512)*2
            color =  (1 - (3 - 2*abs(t))*t**2) 

            for y in range(0, 512):

                t1 = -1+(y/512)*2
                color1 = (1 - (3 - 2*abs(t1))*t1**2)
                result = (255/2)+(color * color1 * (t*self.gx+t1*self.gy) )*(255/2)

                self.image.setPixel(x, y, qRgb(result, result, result))

        self.pixmap = self.pixmap.fromImage(self.image)

        self.tlb = QLabel(str(self.gx)+" : "+str(self.gy), self)

        print(self.gx)

        self.update()


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    

print(self.gx)告诉我self.gx已更改,但图片根本没有更改。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您必须告诉GUI它需要刷新图像。

在QT中,您似乎需要调用窗口小部件的update()repaint()方法。

答案 1 :(得分:0)

我已在self.lbl.setPixmap(self.pixmap)fillImage之前将self.repaint()添加到self.update()中,现在它可以正常工作,然后我更改了一些代码,现在它看起来像这样

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSlot

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.gx=1
        self.gy=1
        self.lbl=QLabel()
        self.tlb = None
        self.image = QImage(512, 512, QImage.Format_RGB32)
        self.hbox = QHBoxLayout()
        self.pixmap = QPixmap()
        self.length = 1
        self.initUI()

    def mousePressEvent(self, QMouseEvent):
        px = QMouseEvent.pos().x()
        py = QMouseEvent.pos().y()

        size = self.frameSize()

        self.gx = px-size.width()/2
        self.gy = py-size.height()/2

        h = (self.gx**2+self.gy**2)**0.5

        self.gx/=h
        self.gy/=h

        self.gx*=self.length
        self.gy*=self.length

        self.fillImage()

    def wheelEvent(self,event):
        self.length+=(event.delta()*0.001)
        print(self.length)


    def initUI(self):    
        self.hbox = QHBoxLayout(self)
        self.pixmap = QPixmap()
        self.move(300, 200)
        self.setWindowTitle('Red Rock')

        self.addedWidget = None

        self.fillImage()

        self.setLayout(self.hbox)

        self.show()  

    def fillImage(self):
        for x in range(0, 512):
            t = -1+(x/512)*2
            color =  (1 - (3 - 2*abs(t))*t**2) 

            for y in range(0, 512):

                t1 = -1+(y/512)*2
                color1 = (1 - (3 - 2*abs(t1))*t1**2)
                result = (255/2)+(color * color1 * (t*self.gx+t1*self.gy) )*(255/2)

                self.image.setPixel(x, y, qRgb(result, result, result))


        self.pixmap = self.pixmap.fromImage(self.image)

        if self.lbl == None:
            self.lbl = QLabel(self)
        else:
            self.lbl.setPixmap(self.pixmap)

        if self.addedWidget == None:
            self.hbox.addWidget(self.lbl)
            self.addedWidget = True

        if self.tlb==None:
            self.tlb = QLabel(str(self.gx)+" : "+str(self.gy), self)
            self.tlb.move(12,3)
        else:
            self.tlb.setText(str(self.gx)+" : "+str(self.gy))

        self.repaint()
        self.update()


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()