qt designer qgraphicsview load image

7.6k views Asked by At

I've build a GUI using Qt Designer, the Gui have a central widget which is a horizontal layout, and children which are QGraphicView (name leftImage and RightImage)

In the menu bar I've created two buttons - "open left image" and "open right image"

I've manage to use this buttons (it opens the open dialog and choose the right file correctly), but I can't see the images on the GUI.

This is my code:

#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui, uic
 
form_class = uic.loadUiType("try2gui.ui")[0]                 
 
class MyWindowClass(QtGui.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)       
        self.setupUi(self)
 
        self.initMenu()

        image = QtGui.QImage('image.pgm')

        self.LeftImage = QtGui.QPixmap(image)
        self.RightImage  = QtGui.QPixmap(image)
        self.show()

    def initMenu (self):
        self.actionOpen_Left.triggered.connect(self.open_left)
        self.actionOpen_Right.triggered.connect(self.open_Right)
        self.actionExit.triggered.connect(self.close)

    def open_left(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath())
        if fileName:
            image = QtGui.QImage(fileName)
            if image.isNull():
                QtGui.QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
                return

        self.centralwidget.LeftImage  = QtGui.QPixmap(image)    
        self.scaleFactor = 1.0
        print fileName

    def open_Right(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath())
        if fileName:
            image = QtGui.QImage(fileName)
            if image.isNull():
                QtGui.QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
                return

        self.RightImage  = QtGui.QPixmap(image)
        self.scaleFactor = 1.0
        
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindowClass(None)
    myWindow.show()

    app.exec_()

How can I update the GUI to see the loaded Image?

2

There are 2 answers

0
Shai Zarzewski On

I've found a solution:

I've changed the open-left function to be: (same goes to open_right):

def open_left(self):
    fileName = QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath())
    if fileName:
        image = QtGui.QImage(fileName)
        if image.isNull():
            QtGui.QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
            return

#added this code:
        leftPixelMap = QtGui.QPixmap(fileName)
        lable = QtGui.QLabel(self.LeftImage)
        lable.setPixmap(leftPixelMap)
        lable.show()

    self.scaleFactor = 1.0

and it works now

0
ngulam On

In your question you used a QGraphicView, which isn't the right widget for showing a picture.

As you found as a solution, use a QLabel and set a pixmap.

Also, self.LeftImage = QtGui.QPixmap(image) isn't working.
This should be self.LeftImage.setPixmap(QtGui.QPixmap(image))

And: in your "open" routines, refer to the widget by self.LeftImage - not by .centralwidget, as you assign them to the central widget, but they are not owned (in hierarchy) to them.