Alex
Alex

Reputation: 41

How to add image to QGraphicsView

I am going to put an image on a QGraphicsView. But I am having some difficulty in adding dynamically. I load QGraphicsView itself from test.ui. The window has a button (id open) and QGraphicsView itself (id gv). The program crashes on line

self.scene.setSceneRect (0, 0, 400, 400)

That is, the program starts. The button works. But after selecting an image, it crashes. I don't know what this means, but maybe it will be useful:

Process finished with exit code -1073740791 (0xC0000409)

Here is the code:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5 import QtWidgets


class MyWin(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        uic.loadUi("test.ui", self)

        self.open.clicked.connect(self.load)

    def load(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, 'Open file', '.', 'Image Files (*.png *.jpg *.bmp)')
        if not file_name:
            return
        self.image_qt = QImage(file_name)

        pic = QGraphicsPixmapItem()
        pic.setPixmap(QPixmap.fromImage(self.image_qt))
        self.scene.setSceneRect(0, 0, 400, 400)
        self.orig_gv.scene.addItem(pic)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dlgMain = MyWin()
    dlgMain.show()
    sys.exit(app.exec_())

Here is test.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QPushButton" name="open">
      <property name="text">
       <string>open</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QGraphicsView" name="gv"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Upvotes: 1

Views: 6339

Answers (1)

eyllanesc
eyllanesc

Reputation: 244291

Note: It seems that the OP has copied code without understanding it, so it is normal for this type of problem to be generated.

There are many undefined elements such as scene and orig_gv. On the other hand a QGraphicsView does not have a QGraphicsScene set so you have to do it and use that scene to place the item image there.

class MyWin(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        uic.loadUi("test.ui", self)
        print(self.gv.scene())
        self.scene = QGraphicsScene()
        self.gv.setScene(self.scene)

        self.open.clicked.connect(self.load)

    def load(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "Open file", ".", "Image Files (*.png *.jpg *.bmp)"
        )
        if not file_name:
            return
        self.image_qt = QImage(file_name)

        pic = QGraphicsPixmapItem()
        pic.setPixmap(QPixmap.fromImage(self.image_qt))
        self.scene.setSceneRect(0, 0, 400, 400)
        self.scene.addItem(pic)

Upvotes: 3

Related Questions