Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QFileDialog and QPixmap
Qt 6.12 LTS is out! See what's new in the release blog

QFileDialog and QPixmap

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
7 Posts 4 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kurtan
    wrote on last edited by
    #1

    Hi , I'm the newer one about Python.
    I'm happy to write here about my python issue.
    I'm trying to open file and load image,i did something to let it works but i had the syntax error.
    What can i do for browsing an image in directory and load it into ?

    code# 
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QFileDialog,QLabel,QWidget
    from PyQt5.QtGui import QPixmap
    
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setGeometry(QtCore.QRect(170, 100, 491, 351))
            self.label.setText("")
            self.label.setPixmap(QPixmap("../../../../../Desktop/Tarih/Native Americans/Flag_of_the_Oneida_Nation_of_Wisconsin.png"))
            self.label.setObjectName("label")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.menubar.setObjectName("menubar")
            self.menuFile = QtWidgets.QMenu(self.menubar)
            self.menuFile.setObjectName("menuFile")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.actionOpen = QtWidgets.QAction(MainWindow)
            self.actionOpen.setObjectName("actionOpen")
            self.menuFile.addAction(self.actionOpen)
            self.menubar.addAction(self.menuFile.menuAction())
    
            self.retranslateUi(MainWindow)
            self.actionOpen.triggered.connect(self.label.show) # type: ignore
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
            
            self.actionOpen.triggered.connect(self.OpenFile)
            self.le = QLabel
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.menuFile.setTitle(_translate("MainWindow", "File "))
            self.actionOpen.setText(_translate("MainWindow", "Open"))
        
        def OpenFile(self):
            options=QFileDialog.Options
            fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options)
            self.le.setPixmap(QPixmap(fname))
            
            
    
    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_())
    _text
    

    I need to work on image process but i already didn't load an image .

    jsulmJ 1 Reply Last reply
    0
    • K Kurtan

      Hi , I'm the newer one about Python.
      I'm happy to write here about my python issue.
      I'm trying to open file and load image,i did something to let it works but i had the syntax error.
      What can i do for browsing an image in directory and load it into ?

      code# 
      
      
      from PyQt5 import QtCore, QtGui, QtWidgets
      from PyQt5.QtWidgets import QFileDialog,QLabel,QWidget
      from PyQt5.QtGui import QPixmap
      
      
      
      class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
              MainWindow.setObjectName("MainWindow")
              MainWindow.resize(800, 600)
              self.centralwidget = QtWidgets.QWidget(MainWindow)
              self.centralwidget.setObjectName("centralwidget")
              self.label = QtWidgets.QLabel(self.centralwidget)
              self.label.setGeometry(QtCore.QRect(170, 100, 491, 351))
              self.label.setText("")
              self.label.setPixmap(QPixmap("../../../../../Desktop/Tarih/Native Americans/Flag_of_the_Oneida_Nation_of_Wisconsin.png"))
              self.label.setObjectName("label")
              MainWindow.setCentralWidget(self.centralwidget)
              self.menubar = QtWidgets.QMenuBar(MainWindow)
              self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
              self.menubar.setObjectName("menubar")
              self.menuFile = QtWidgets.QMenu(self.menubar)
              self.menuFile.setObjectName("menuFile")
              MainWindow.setMenuBar(self.menubar)
              self.statusbar = QtWidgets.QStatusBar(MainWindow)
              self.statusbar.setObjectName("statusbar")
              MainWindow.setStatusBar(self.statusbar)
              self.actionOpen = QtWidgets.QAction(MainWindow)
              self.actionOpen.setObjectName("actionOpen")
              self.menuFile.addAction(self.actionOpen)
              self.menubar.addAction(self.menuFile.menuAction())
      
              self.retranslateUi(MainWindow)
              self.actionOpen.triggered.connect(self.label.show) # type: ignore
              QtCore.QMetaObject.connectSlotsByName(MainWindow)
              
              self.actionOpen.triggered.connect(self.OpenFile)
              self.le = QLabel
      
          def retranslateUi(self, MainWindow):
              _translate = QtCore.QCoreApplication.translate
              MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
              self.menuFile.setTitle(_translate("MainWindow", "File "))
              self.actionOpen.setText(_translate("MainWindow", "Open"))
          
          def OpenFile(self):
              options=QFileDialog.Options
              fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options)
              self.le.setPixmap(QPixmap(fname))
              
              
      
      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_())
      _text
      

      I need to work on image process but i already didn't load an image .

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Kurtan said in QFileDialog and QPixmap:

      i had the syntax error

      Please post the error and in which line it occurs

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Kurtan said in QFileDialog and QPixmap:

        i had the syntax error

        Please post the error and in which line it occurs

        K Offline
        K Offline
        Kurtan
        wrote on last edited by
        #3

        @jsulm This is a syntax Type-Error :

        File "C:\Users\user\AppData\Local\Programs\Python\pyqt tutorial\Open.py", line 58, in OpenFile
            fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options)
        
        TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'Ui_MainWindow'
        
        jsulmJ 1 Reply Last reply
        0
        • K Kurtan

          @jsulm This is a syntax Type-Error :

          File "C:\Users\user\AppData\Local\Programs\Python\pyqt tutorial\Open.py", line 58, in OpenFile
              fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options)
          
          TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'Ui_MainWindow'
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Kurtan said in QFileDialog and QPixmap:

          fname = QFileDialog.getOpenFileName(self,

          First argument is wrong: it has to be QWidget or derived class. You are passing Ui_MainWindow which is not derived from QWidget. Either pass None or something derived from QWidget.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            If you want to do things cleanly, you should make MainWindow a class and move your OpenFile method there.

            On a side note, you should follow either the Qt or the Python coding guidelines, it will make your code easier to read and follow.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi and welcome to devnet,

              If you want to do things cleanly, you should make MainWindow a class and move your OpenFile method there.

              On a side note, you should follow either the Qt or the Python coding guidelines, it will make your code easier to read and follow.

              K Offline
              K Offline
              Kurtan
              wrote on last edited by
              #6

              @SGaist @jsulm Thank you .I achieved first step to browse and load an image.

              1 Reply Last reply
              0
              • CristianMaureiraC Offline
                CristianMaureiraC Offline
                CristianMaureira
                wrote on last edited by
                #7

                Also, don't modify the generated file directly and import it, otherwise next time you use uic again, your changed will be overwritten https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved