1

我试图在 pyside gui 中初始化一个空白 QImage 小部件,但它抛出错误,我无法从文档中弄清楚我应该做什么,有谁知道我需要做什么步骤才能让这个 QImage 小部件工作

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()

这是我得到的错误:

 /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)
4

2 回答 2

1

编辑:给出这个答案时,问题与现在不同......

fill 需要一个 int 参数而不是 QColor 元素。

利用        

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

代替

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

我希望这在 pyside 上的工作方式与在 c++ 中的工作方式完全相同。这是文档:http ://doc.qt.nokia.com/4.7-snapshot/qcolor.html#qRgb

于 2012-08-17T12:10:11.160 回答
0

主要问题是 QImage 不是小部件,因此无法将其添加到布局中。下面是使用红色背景初始化 Qimage 并将其放置在 QLabel 小部件中的代码。我还将图像格式更改为 ARGB32,以便使用 4 x 8 位的 Alpha、红色、绿色和蓝色值对图像进行格式化。

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()
于 2012-08-17T14:08:17.320 回答