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

PySide2 display only few images and show error in case high quality image



  • I am trying to display image in PySide2 but It only open few images and show this error when it failed to open image

    QPixmap::scaled: Pixmap is a null pixmap
    

    Here is complete code of the app

    
    import sys
    from PySide2 import QtGui, QtCore
    from PySide2.QtWidgets import QFileDialog,QLabel,QAction,QMainWindow,QApplication
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.setGeometry(100, 100, 500, 300)
            self.setWindowTitle("PyQT Show Image")
    
            openFile = QAction("&File", self)
            openFile.setShortcut("Ctrl+O")
            openFile.setStatusTip("Open File")
            openFile.triggered.connect(self.file_open)
    
            self.statusBar()
    
            mainMenu = self.menuBar()
    
            fileMenu = mainMenu.addMenu('&File')
            fileMenu.addAction(openFile)
    
            self.lbl = QLabel(self)
            self.setCentralWidget(self.lbl)
    
            self.home()
    
        def home(self):
            self.show()
    
        def file_open(self):
            name = QFileDialog.getOpenFileName(self, 'Open File')
            #print(name)
            pixmap = QtGui.QPixmap(name[0])
            self.lbl.setPixmap(pixmap.scaled(self.lbl.size() , QtCore.Qt.KeepAspectRatio , QtCore.Qt.SmoothTransformation))
    
    
    def run():
        app = QApplication(sys.argv)
        GUI = Window()
        sys.exit(app.exec_())
    
    run()
    

    Please help me How I can solve this I am using windows os



  • @haider said in PySide2 display only few images and show error in case high quality image:

    pixmap = QtGui.QPixmap(name[0])

    I don't see how we can help you. If that returns nullptr/None, what would you like us to do? Find out why it did so, just as an e.g. you did not pick an acceptable file for conversion to a pixmap.


  • Lifetime Qt Champion

    Hi,

    To add to @JonB, the user may have canceled the dialog. You should check that as well.


  • Banned

    Okay first I am not quite sure what was wrong with your code but I have re-implemented it using PyQt and this slight re-implementation of your code works just fine for me -- all you should have to do is change the PyQt5. reference to a PySide2. reference and it should work for you as well and if it does not then we would need a link to one of those images to help you troubleshoot what is occurring. Oh and if you have absolutely any questions about what I did and/or why please do ask. I am all about teaching folks the hows and whys of coding.

    from PyQt5.QtCore    import Qt, pyqtSlot
    from PyQt5.QtGui     import QPixmap
    from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget
    from PyQt5.QtWidgets import QFileDialog, QLabel, QAction
    
    class MenuToolBar(QDockWidget):
        def __init__(self, parent):
            QDockWidget.__init__(self)
            self.Parent = parent
            self.MainMenu = self.Parent.menuBar()
    
            self.MenuActRef = {'OpenFileAct':0}
    
            # ******* Create the File Menu *******
            self.FileMenu  = self.MainMenu.addMenu('File')
    
            # ******* Create File Menu Items *******
    #        self.OpnFileAct = QAction(QIcon('Images/open.ico'), 'Open File', self)
            self.OpnFileAct = QAction('Open File', self)
            self.OpnFileAct.setShortcut('Ctrl+O')
            self.OpnFileAct.setStatusTip('Open File')
            self.OpnFileAct.triggered.connect(self.OpenFile)
            self.MenuActRef['OpenFileAct'] = self.OpnFileAct
    
            self.FileMenu.addAction(self.OpnFileAct)
    
    #        self.InitToolBar()
    
      # In case you ever wanted to add something to a Tool Bar
        def InitToolBar(self):
            # Add Items to the Toolbar
            # This needs to be dynamically based on user adjustments if any
            self.mainToolBar = self.MainWin.addToolBar('Quick Access')
    
          # This is would be a call to a Database or Config File
          #  ToolBarLayout = self.MainWin.GetToolBarLayout()
    
            for item in ToolBarLayout:
                if item == 'Spacer':
                    self.mainToolBar.addSeparator()
                
                else:
                    self.mainToolBar.addAction(self.MenuActRef[item])  
    
    # These are the Menu/Tool Bar Actions
        @pyqtSlot()
        def OpenFile(self):
            self.Parent.OpenNewFile()
    
    class CentralPanel(QLabel):
        def __init__(self, parent):
            QLabel.__init__(self)
            self.Parent = parent
          # In case you ever need to expand this functionality
    
    class MainWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
          # Set QMainWindow settings
            self.setGeometry(100, 100, 500, 300)
            self.setWindowTitle("PyQT Show Image")
          # Setup the CentralWidget
            self.CntrPane = CentralPanel(self)
            self.setCentralWidget(self.CntrPane)
          # Setup the Menu and Tool Bar
            self.MenuToolBar = MenuToolBar(self)
          # Setup the Status Bar
            self.SetStatusBar(self)
    
        def SetStatusBar(self, parent):
          # Using Parent in case it ever becomes a stand-alone Class
            StatusMsg = ''
            parent.StatBar = parent.statusBar()
    
            if len(StatusMsg) < 1:
                StatusMsg = 'Ready'
    
            parent.StatBar.showMessage(StatusMsg)
    
        def OpenNewFile(self):
            name = QFileDialog.getOpenFileName(self, 'Open File')
            #print(name)
            pixmap = QPixmap(name[0])
            self.CntrPane.setPixmap(pixmap.scaled(self.CntrPane.size() , Qt.KeepAspectRatio , Qt.SmoothTransformation))
    
    if __name__ == '__main__':
        MainEventThread = QApplication([])
    
        MainApp = MainWindow()
        MainApp.show()
    
        MainEventThread.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

Log in to reply