Qt World Summit: Register Today!

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.setStatusTip("Open File")
            mainMenu = self.menuBar()
            fileMenu = mainMenu.addMenu('&File')
            self.lbl = QLabel(self)
        def home(self):
        def file_open(self):
            name = QFileDialog.getOpenFileName(self, 'Open File')
            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()

    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


    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):
            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.setStatusTip('Open File')
            self.MenuActRef['OpenFileAct'] = 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':
    # These are the Menu/Tool Bar Actions
        def OpenFile(self):
    class CentralPanel(QLabel):
        def __init__(self, parent):
            self.Parent = parent
          # In case you ever need to expand this functionality
    class MainWindow(QMainWindow):
        def __init__(self):
          # Set QMainWindow settings
            self.setGeometry(100, 100, 500, 300)
            self.setWindowTitle("PyQT Show Image")
          # Setup the CentralWidget
            self.CntrPane = CentralPanel(self)
          # Setup the Menu and Tool Bar
            self.MenuToolBar = MenuToolBar(self)
          # Setup the Status Bar
        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'
        def OpenNewFile(self):
            name = QFileDialog.getOpenFileName(self, 'Open File')
            pixmap = QPixmap(name[0])
            self.CntrPane.setPixmap(pixmap.scaled(self.CntrPane.size() , Qt.KeepAspectRatio , Qt.SmoothTransformation))
    if __name__ == '__main__':
        MainEventThread = QApplication([])
        MainApp = MainWindow()
      # 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