1

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>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alex
  • 41
  • 1
  • 8

1 Answers1

3

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for the answer! Yes, you're right, I'm just getting started. I wrote the code myself, based on examples from books. The topic with QGraphicsView and QGraphicsScene is very difficult for me. It's hard to understand how this works. At the fundamental level. I still have a question about aligning the placed image. The loaded image is not centered. Can I put it natively in the center of the QGraphicsView? – Alex Sep 22 '21 at 23:17
  • @Alex use `self.gv.setAlignment(QtCore.Qt.AlignCenter)` – eyllanesc Sep 22 '21 at 23:18
  • I am getting warning: Unresolved attribute reference 'AlignCenter' for class 'Qt'. What am I doing wrong? – Alex Sep 22 '21 at 23:30
  • Have you import: `from PyQt5 import QtCore`? – eyllanesc Sep 22 '21 at 23:33
  • yes i connected it – Alex Sep 22 '21 at 23:35
  • I just needed to remove the line `self.scene.setSceneRect (0, 0, 400, 400)` – Alex Sep 23 '21 at 00:34