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は、QColor要素の代わりにint引数を必要とします。

使用する        

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 ビット値でフォーマットされるようにします。

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 に答える