[英]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 我试图在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()
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. fill需要一个int参数而不是QColor元素。
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++. 我希望这在pyside上与在c ++中完全一样。 Here is the documentation: http://doc.qt.nokia.com/4.7-snapshot/qcolor.html#qRgb
这是文档: 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. 主要问题是QImage不是小部件,因此无法将其添加到布局中。 Below is the code for initialising the Qimage with a red background and placing it inside a QLabel widget.
下面是用红色背景初始化Qimage并将其放置在QLabel小部件内的代码。 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.
我还将图像格式更改为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.