Ad

Image Doesn't Show In My Secondary Window

- 1 answer

I wanted to create a secondary window to show the fullsized image, so that's what I've made but I can't see the image. I don't know if it's why I don't open correctly or what. I'm very new to Qt so provably it's a very simple mistake.

Thanks

Here's the code:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

This is the main window, it has a open file dialog to select an image, once it have it puts the image in a label. When you click the button "Ampliar" you open a second window with should show the image with the original size.

# Classe per la finestra principal de l'App
class Ui_MainWindow(object):

    # Inicialitzacio de variables
    def __init__(self):
        self.fileName = None

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1080, 720)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Label on coloquem la nostra imatge
        self.lMarcIm = QtWidgets.QLabel(self.centralwidget)
        self.lMarcIm.setGeometry(QtCore.QRect(10, 10, 500, 500))
        self.lMarcIm.setAutoFillBackground(False)
        self.lMarcIm.setScaledContents(True)
        self.lMarcIm.setObjectName("lMarcIm")

        # Boto per carregar una imatge al programa
        self.bObrirIm = QtWidgets.QPushButton(self.centralwidget)
        self.bObrirIm.setGeometry(QtCore.QRect(10, 530, 111, 31))
        self.bObrirIm.setObjectName("bObrirIm")
        self.bObrirIm.clicked.connect(self.obrirDialegImatge)

        # Boto per ampliar la imatge carregada
        self.bAmpliarIm = QtWidgets.QPushButton(self.centralwidget)
        self.bAmpliarIm.setGeometry(QtCore.QRect(130, 530, 121, 31))
        self.bAmpliarIm.setObjectName("bAmpliarIm")
        self.bAmpliarIm.clicked.connect(self.ampliarImatge)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    ################################################################
    #                       Bloc de Funcions                       #
    ################################################################

    def obrirDialegImatge(self):
        # Obrim la finestra de dialeg i obtenim el nom
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                        None,
                        "QFileDialog.getOpenFileName()",
                        "",
                        "All Files (*);;Python Files (*.py)",
                        options=options)


        # Posem l'imatge on toca del Layout
        self.lMarcIm.setPixmap(QtGui.QPixmap(self.fileName))

    def ampliarImatge(self):
        #Comprovem que hi ha una imatge seleccionada
        if self.fileName != None:
            print("Imatge en gran")
            self.openSecondWindow()

        else: 
            self.popup_errorAmpliar()

    def openSecondWindow(self):
        self.secondWindow = QtWidgets.QMainWindow()
        self.ui = Ui_SecondaryWindow()
        self.ui.setupUi(self.secondWindow, self.fileName)
        self.secondWindow.show()


    def popup_errorAmpliar(self):
        msg = QMessageBox()
        msg.setWindowTitle("Alerta!")
        msg.setText("No hi ha cap imatge carregada")
        msg.setIcon(QMessageBox.Warning)
        x = msg.exec_()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Image Processing APP"))
        self.lMarcIm.setText(_translate("MainWindow", "TextLabel"))
        self.bObrirIm.setText(_translate("MainWindow", "Obrir Imatge"))
        self.bAmpliarIm.setText(_translate("MainWindow", "Ampliar"))

This is the secondary class, where I want to put the image I open in the other window, it recive it's path so I think it has to be easy

# Classe per la finestra secundaria de l'App
class Ui_SecondaryWindow(object):
    def setupUi(self, SecondaryWindow, im):
        SecondaryWindow.setObjectName("SecondaryWindow")
        SecondaryWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(SecondaryWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.lmidaOriginal = QtWidgets.QLabel(self.centralwidget)
        self.lmidaOriginal.setGeometry(QtCore.QRect(150, 60, 251, 101))
        self.lmidaOriginal.setObjectName("lmidaOriginal")
        self.openImage(im)

        SecondaryWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(SecondaryWindow)
        QtCore.QMetaObject.connectSlotsByName(SecondaryWindow)

    def openImage(self, imRoute):
        print(imRoute)
        pixmap = QtGui.QPixmap("imRoute")
        self.lmidaOriginal.setPixmap(pixmap)
        #self.lmidaOriginal.resize(pixmap.width(), pixmap.height())


    def retranslateUi(self, SecondaryWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondaryWindow.setWindowTitle(_translate("SecondaryWindow", "Apmliated Image"))
        self.lmidaOriginal.setText(_translate("SecondaryWindow", "TextLabel"))

this is the main

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Ad

Answer

Try it:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox


# Classe per la finestra secundaria de l'App
class Ui_SecondaryWindow(object):
    def setupUi(self, SecondaryWindow): #, im):

        SecondaryWindow.setObjectName("SecondaryWindow")
#        SecondaryWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(SecondaryWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.lmidaOriginal = QtWidgets.QLabel(self.centralwidget)
#        self.lmidaOriginal.setGeometry(QtCore.QRect(150, 60, 251, 101))
        self.lmidaOriginal.setObjectName("lmidaOriginal")
#        self.openImage(im)

        SecondaryWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(SecondaryWindow)
        QtCore.QMetaObject.connectSlotsByName(SecondaryWindow)
    """
    def openImage(self, imRoute):
        print("imRoute---", imRoute)
        pixmap = QtGui.QPixmap("imRoute")
        pixmap = QtGui.QPixmap(imRoute)  #("im.png") #(imRoute)
        self.lmidaOriginal.setPixmap(pixmap)
    """


    def retranslateUi(self, SecondaryWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondaryWindow.setWindowTitle(_translate("SecondaryWindow", "Apmliated Image"))
        self.lmidaOriginal.setText(_translate("SecondaryWindow", "TextLabel"))


# Classe per la finestra principal de l'App
class Ui_MainWindow(object):

    # Inicialitzacio de variables
    def __init__(self):
        self.fileName = None

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1080, 720)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Label on coloquem la nostra imatge
        self.lMarcIm = QtWidgets.QLabel(self.centralwidget)
        self.lMarcIm.setGeometry(QtCore.QRect(10, 10, 500, 500))
        self.lMarcIm.setAutoFillBackground(False)
        self.lMarcIm.setScaledContents(True)
        self.lMarcIm.setObjectName("lMarcIm")

        # Boto per carregar una imatge al programa
        self.bObrirIm = QtWidgets.QPushButton(self.centralwidget)
        self.bObrirIm.setGeometry(QtCore.QRect(10, 530, 111, 31))
        self.bObrirIm.setObjectName("bObrirIm")
        self.bObrirIm.clicked.connect(self.obrirDialegImatge)

        # Boto per ampliar la imatge carregada
        self.bAmpliarIm = QtWidgets.QPushButton(self.centralwidget)
        self.bAmpliarIm.setGeometry(QtCore.QRect(130, 530, 121, 31))
        self.bAmpliarIm.setObjectName("bAmpliarIm")
        self.bAmpliarIm.clicked.connect(self.ampliarImatge)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    ################################################################
    #                       Bloc de Funcions                       #
    ################################################################

    def obrirDialegImatge(self):
        # Obrim la finestra de dialeg i obtenim el nom
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                        None,
                        "Select Image",
                        "",
                        "Image Files (*.png *.jpg *.jpeg *.bmp)",             # +++
                        options=options)


        # Posem l'imatge on toca del Layout
        self.lMarcIm.setPixmap(QtGui.QPixmap(self.fileName))

    def ampliarImatge(self):
        #Comprovem que hi ha una imatge seleccionada
        if self.fileName != None:
            print("Imatge en gran")
            self.openSecondWindow()
        else: 
            self.popup_errorAmpliar()

    def openSecondWindow(self):
        self.secondWindow = QtWidgets.QMainWindow()
        self.ui = Ui_SecondaryWindow()

#        self.ui.setupUi(self.secondWindow, self.fileName)

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        self.ui.setupUi(self.secondWindow)  
        self.ui.lmidaOriginal.setPixmap(QtGui.QPixmap(self.fileName))        
        self.ui.lmidaOriginal.adjustSize()
        self.secondWindow.resize(self.ui.lmidaOriginal.width(), self.ui.lmidaOriginal.height())
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        self.secondWindow.show()



    def popup_errorAmpliar(self):
        msg = QMessageBox()
        msg.setWindowTitle("Alerta!")
        msg.setText("No hi ha cap imatge carregada")
        msg.setIcon(QMessageBox.Warning)
        x = msg.exec_()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Image Processing APP"))
        self.lMarcIm.setText(_translate("MainWindow", "TextLabel"))
        self.bObrirIm.setText(_translate("MainWindow", "Obrir Imatge"))
        self.bAmpliarIm.setText(_translate("MainWindow", "Ampliar"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())        

enter image description here

Ad
source: stackoverflow.com
Ad