QGraphicsView错误 - 试图用Python显示jpg

时间:2011-04-27 21:12:41

标签: python qgraphicsview qt-designer

我正在使用Python 3.2与Eric5和QTDesigner一起尝试在QGraphicsView场景中显示jpg。以下代码给了我一个 无法打开文件。原因:[Errno 22]参数无效:“ 第一次传递时出错,然后以a结束 调试的程序引发了未处理的异常AttributeError “'MyForm'对象没有属性'QGraphicsView'” 档案:,行:17

引用的screentest.ui文件只是表单上的一个简单的QGraphicsView对象框。

非常感谢任何建议。

 #UIGraphicTest.py

import sys
# from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow

class MyForm(QtGui.QMainWindow):
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  myapp = MyForm()
  grview = myapp.QGraphicsView()
  scene = myapp.QGraphicsScene()
  scene.addPixmap(QtGui.QPixmap('att-logo.jpg'))
  grview.setScene(scene)

  myapp.show()
  sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

看起来我需要将我的场景移动到类,然后引用QGraphicsView对象的名称。我还添加了在Windows中使用的ctypes。这是工作代码:

#UIGraphicTest.py

import sys,  ctypes
#from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow

class MyForm(QtGui.QMainWindow):
  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    #Prevent grouping with python in Windows
    myappid = 'UIGraphicTest'
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
    # Create a Scene
    self.scene = QtGui.QGraphicsScene()
    self.scene.addPixmap(QtGui.QPixmap('logo.jpg')) 
    self.ui.graphicsView.setScene(self.scene)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  myform = MyForm()
  myform.show()

  sys.exit(app.exec_())