简体   繁体   中英

initialise a blank QImage with pyside

Im trying to initialise a blank QImage widget in a pyside gui but its throwing errors and I cant figure out what I'm supposed to do from the docs, does anyone know what steps i need to do to get this QImage Widget working

import sys
from PySide import QtGui, QtCore
import os

class ms_ImageViewer(QtGui.QWidget):

    def __init__(self):
        super(ms_ImageViewer, self).__init__()
        self.initUI()

    def initUI(self):               

        main_layout = QtGui.QVBoxLayout()
        self.setLayout(main_layout)

        self.image = QtGui.QImage(50, 50, QtGui.QImage.Format_Indexed8)
        self.image.fill(QtGui.qRgb(50,50,50))
        button = QtGui.QPushButton('select file', self)
        main_layout.addWidget(button)
        main_layout.addWidget(self.image)

        self.setGeometry(300, 300, 600, 30)
        self.setWindowTitle('ms_image_viewer')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = ms_ImageViewer()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

and here's the error i get:

 /projects/Mayaseed/src/tools/ms_image_viewer.py 
    Traceback (most recent call last):
      File "/projects/Mayaseed/src/tools/ms_image_viewer.py", line 34, in <module>
        main()
      File "/projects/Mayaseed/src/tools/ms_image_viewer.py", line 29, in main
        ex = ms_ImageViewer()
      File "/projects/Mayaseed/src/tools/ms_image_viewer.py", line 9, in __init__
        self.initUI()
      File "/projects/Mayaseed/src/tools/ms_image_viewer.py", line 20, in initUI
        main_layout.addWidget(self.image)
    TypeError: 'PySide.QtGui.QBoxLayout.addWidget' called with wrong argument types:
      PySide.QtGui.QBoxLayout.addWidget(PySide.QtGui.QImage)
    Supported signatures:
      PySide.QtGui.QBoxLayout.addWidget(PySide.QtGui.QWidget, int = 0, PySide.QtCore.Qt.Alignment = 0)

EDIT: when giving this answer, the question was a different one than now...

fill expects an int argument instead of a QColor element.

Use

self.image.fill(qRgb(50,50,50))

instead of

self.image.fill(QtGui.QColor(50,50,50))

I hope this works exactly the same on pyside as in c++. Here is the documentation: http://doc.qt.nokia.com/4.7-snapshot/qcolor.html#qRgb

The main issue is that a QImage is not a widget so it cannot be added to a layout. Below is the code for initialising the Qimage with a red background and placing it inside a QLabel widget. I also change the image format to ARGB32 so that the image is formatted with 4 x 8 bit values for Alpha, Red, Green and blue.

import sys
from PySide import QtGui, QtCore
import os

class ms_ImageViewer(QtGui.QWidget):

    def __init__(self):
        super(ms_ImageViewer, self).__init__()
        self.initUI()

    def initUI(self):               

        main_layout = QtGui.QVBoxLayout()
        self.setLayout(main_layout)

        self.image = QtGui.QImage(100, 150, QtGui.QImage.Format_ARGB32)
        intial_color = QtGui.qRgb(189, 149, 39)
        self.image.fill(QtGui.qRgb(255,0,0))
        # self.image.load('/projects/test.png')
        image_label = QtGui.QLabel(" ")
        image_label.setPixmap(QtGui.QPixmap.fromImage(self.image))
        button = QtGui.QPushButton('select file', self)
        main_layout.addWidget(button)
        main_layout.addWidget(image_label)

        self.setGeometry(300, 300, 600, 30)
        self.setWindowTitle('ms_image_viewer')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = ms_ImageViewer()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM