[英]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.