pyqt5 qimage 读取内存数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5 qimage 读取内存数据相关的知识,希望对你有一定的参考价值。
参考技术A 读取方法如下1、进入python交互窗口,并引入io模块的StringIO。
2、使用函数StringIO()创建一个对象f,使用函数f.write(),向此对象中写入数据。
3、使用函数f.getvalue(),获取写入的数据,将此数据打印出来。
4、使用函数StringIO(str)直接创建含有字符串str的对象f,可以使用函数f.readlines()读取内存中的数据,并打印出来。
如何在 PyQt4 中将 QImage 插入到 NxN 网格布局中?
【中文标题】如何在 PyQt4 中将 QImage 插入到 NxN 网格布局中?【英文标题】:How to insert a QImage into an NxN grid layout in PyQt4? 【发布时间】:2013-12-25 15:28:43 【问题描述】:我在 PyQt4 的网格布局中有一个 4x4 网格:
self.grid = QtGui.QGridLayout()
pos = [(0, 0), (0, 1), (0, 2),(0, 3),
(1, 0), (1, 1), (1, 2), (1, 3),
(2, 0), (2, 1), (2, 2), (2, 3),
(3, 0), (3, 1), (3, 2), (3, 3)]
for position in pos:
button = QtGui.QPushButton('button 1')
self.grid.addWidget(button, position[0], position[1])
self.setLayout(self.grid)
self.move(300, 150)
self.show()
我需要用包含图像的网格替换QPushButton
s,这样它就形成了一个 4X4 的图像网格。为此,我知道我将不得不使用QImage
,但我不确定如何使用!谁能帮帮我?
谢谢
【问题讨论】:
【参考方案1】:一种方法是使用QLabel 并使用setPixmap 添加图像。 pixmaps 本身可以直接从文件路径创建。
这是一个简单的示例(将图像文件路径指定为脚本的参数):
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self, path):
QtGui.QWidget.__init__(self)
pixmap = QtGui.QPixmap(path)
layout = QtGui.QGridLayout(self)
for row in range(4):
for column in range(4):
label = QtGui.QLabel(self)
label.setPixmap(pixmap)
layout.addWidget(label, row, column)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(sys.argv[1] if len(sys.argv) else '')
window.setGeometry(500, 300, 300, 300)
window.show()
sys.exit(app.exec_())
【讨论】:
谢谢@ekhumoro。这就是我一直在寻找的。一般来说,两者中的哪一个是“首选”方式 - QImage 或 QPixmap?两者如何相互竞争? QPixmap 针对在屏幕上显示图像进行了优化。 QImage 更底层,设计用于直接访问像素。显示图像的小部件(例如 QLabel)通常会有设置 QPixmap 的方法,但没有设置 QImage 的方法;所以 QImage 需要先转换为像素图。大概 QPixmap 是一般 GUI 设计中最常用的图像类;其他三个图像类(QImage、QBitmap 和 QPicture)更专业。以上是关于pyqt5 qimage 读取内存数据的主要内容,如果未能解决你的问题,请参考以下文章