Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsView's problem



  • when I want to make a GUI , a pushbutton to chose file ,and a QGraphicsView shows the image I chosen. But there are problems. The image doesn's show here. I can sure I lost some detail things.
    logic code first:

    import sys
    import os
    from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
    from PyQt5.QtGui import QPixmap, QImage
    from photo_view import Ui_Photo_view
    from PIL import Image
    import cv2
    
    
    class show_photo(QMainWindow):
    	def __init__(self):
    		super(show_photo,self).__init__()
    		self.ui =Ui_Photo_view()
    		self.ui.setupUi(self)
    		self.view = QGraphicsView()
    
    	def select_button_clicked(self):
    		file_name = QFileDialog.getOpenFileName(self, "Open File", "./", "jpg (*.jpg)")
    		image_path = file_name[0]
    		if (file_name[0] == ""):
    			QMessageBox.information(self, "提示", self.tr("没有选择图片文件!"))
    		print(image_path)
    		img = cv2.imread(image_path)  
    		img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  
    		x = img.shape[1]  
    		y = img.shape[0]
    		self.zoomscale = 1  
    		frame = QImage(img, x, y, QImage.Format_RGB888)
    		pix = QPixmap.fromImage(frame)
    		self.item = QGraphicsPixmapItem(pix)  
    		self.scene = QGraphicsScene()  
    		self.scene.addItem(self.item)
    		self.view.setScene(self.scene)
    
    if __name__ == "__main__":
    	app = QApplication(sys.argv)
    	mainWindow = show_photo()
    	mainWindow.show()
    	sys.exit(app.exec_())
    
    
    

    then the GUI code:

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'D:\Projects\photo_view/photo_view.ui'
    #
    # Created by: PyQt5 UI code generator 5.12.3
    #
    # WARNING! All changes made in this file will be lost!
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_Photo_view(object):
        def setupUi(self, Photo_view):
            Photo_view.setObjectName("Photo_view")
            Photo_view.resize(890, 682)
            self.centralwidget = QtWidgets.QWidget(Photo_view)
            self.centralwidget.setObjectName("centralwidget")
            self.select_button = QtWidgets.QPushButton(self.centralwidget)
            self.select_button.setGeometry(QtCore.QRect(770, 140, 75, 23))
            self.select_button.setCheckable(True)
            self.select_button.setObjectName("select_button")
            self.pic_show = QtWidgets.QGraphicsView(self.centralwidget)
            self.pic_show.setGeometry(QtCore.QRect(0, 0, 711, 641))
            self.pic_show.setObjectName("pic_show")
            Photo_view.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(Photo_view)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 890, 23))
            self.menubar.setObjectName("menubar")
            Photo_view.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(Photo_view)
            self.statusbar.setObjectName("statusbar")
            Photo_view.setStatusBar(self.statusbar)
    
            self.retranslateUi(Photo_view)
            self.select_button.clicked.connect(Photo_view.select_button_clicked)
            QtCore.QMetaObject.connectSlotsByName(Photo_view)
    
        def retranslateUi(self, Photo_view):
            _translate = QtCore.QCoreApplication.translate
            Photo_view.setWindowTitle(_translate("Photo_view", "MainWindow"))
            self.select_button.setText(_translate("Photo_view", "选择文件"))
    
    

  • Qt Champions 2019

    @Acton-Martin I think you forgot

    self.view.show()
    

  • Qt Champions 2019

    @Acton-Martin Please do not create a new thread for that: https://forum.qt.io/topic/110747/thank-you :-)
    It creates a new window because your self.view does not have a parent.
    Try

    self.view = QGraphicsView(self)
    


  • @jsulm Give my best thanks for your help. I make a mistake to Reply.


Log in to reply