[英]initialise a blank QImage with pyside
我试图在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)
编辑:当给出此答案时,问题是一个不同于现在的问题...
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
主要问题是QImage不是小部件,因此无法将其添加到布局中。 下面是用红色背景初始化Qimage并将其放置在QLabel小部件内的代码。 我还将图像格式更改为ARGB32,以使图像使用Alpha,Red,Green和blue的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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.