在使用 Qt Designer 设计 GUI 时,用户希望在 QGraphicsView 中加载图像,但是在打开图像文件后,无法在 GUI 中看到加载的图像。
2、解决方案
要解决此问题,需要在加载图像后对 QGraphicsView 进行更新,以便显示加载的图像。以下是具体步骤:
- 在主窗口类中声明 QGraphicsScene 对象。
- 在加载图像后,将图像转换为 QPixmap 对象。
- 将 QPixmap 对象添加到 QGraphicsScene 对象中。
- 将 QGraphicsScene 对象添加到 QGraphicsView 中。
以下代码演示了如何实现上述步骤:
import sys
from PyQt4 import QtCore, QtGui, uic
form_class = uic.loadUiType("try2gui.ui")[0]
http://www.jshk.com.cn/mb/reg.asp?kefu=xiaoding;//爬虫IP免费获取;
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.initMenu()
self.scene