Pyqt 简明教程
PyQt - QPixmap Class
QPixmap 类提供图像的非屏幕表示。它可以用作 QPaintDevice 对象,也可以加载到另一个小部件,通常是标签或按钮。
Qt API 还有另一个相似的类 QImage,针对 I/O 和其他像素操作进行了优化。另一方面,Pixmap 针对在屏幕上显示进行了优化。这两种格式可以互相转换。
可以读入 QPixmap 中的图像文件类型如下 -
BMP |
Windows Bitmap |
GIF |
Graphic Interchange Format (optional) |
JPG |
Joint Photographic Experts Group |
JPEG |
Joint Photographic Experts Group |
PNG |
Portable Network Graphics |
PBM |
Portable Bitmap |
PGM |
Portable Graymap |
PPM |
Portable Pixmap |
XBM |
X11 Bitmap |
XPM |
X11 Pixmap |
以下方法对于处理 QPixmap 对象很有用 -
Sr.No. |
Methods & Description |
1 |
copy() 从 QRect 对象复制像素图数据 |
2 |
fromImage() 将 QImage 对象转换为 QPixmap |
3 |
grabWidget() 从给定小部件创建像素图 |
4 |
grabWindow() 创建窗口中数据的像素图 |
5 |
Load() 将图像文件加载为像素图 |
6 |
save() 保存 QPixmap 对象为文件 |
7 |
toImage 将 QPixmap 转换为 QImage |
QPixmap 最常见的用途是在标签/按钮上显示图像。
Example
以下示例展示了使用 setPixmap() 方法在 QLabel 上显示图像。完整的代码如下 -
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def window():
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel()
l1.setPixmap(QPixmap("python.jpg"))
vbox = QVBoxLayout()
vbox.addWidget(l1)
win.setLayout(vbox)
win.setWindowTitle("QPixmap Demo")
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
上述代码生成以下输出 -