Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. TypeError: unbound method QFileDialog.selectedFiles() needs an argument
Qt 6.11 is out! See what's new in the release blog

TypeError: unbound method QFileDialog.selectedFiles() needs an argument

Scheduled Pinned Locked Moved Solved Qt for Python
pysidepython
6 Posts 3 Posters 102 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.
  • S Offline
    S Offline
    SamuraiDestroy
    wrote last edited by
    #1

    I'm trying to make a file opening system using a QFileDialog, but when I try to get the selected files from it it throws a TypeError, despite the fact that nowhere in any documentation does this method seem to take an argument.
    Here is the relevant portion of my code:

    def openFile():
        getFile = QtWidgets.QFileDialog
        getFile.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")
        openedFile = getFile.selectedFiles() # This is the code that throws the error
    
    
    def menuTriggers(button):
        if button.objectName() == "actionOpen":
            openFile()
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = uiLoader.load(mainWindow)
        help = QtWidgets.QMenuBar
        window.show()
        help = window.menubar.triggered.connect(menuTriggers)
        sys.exit(app.exec())
    

    I tried using getFile variable as the argument, however that returned the error TypeError: descriptor 'selectedFiles' for 'PySide6.QtWidgets.QFileDialog' objects doesn't apply to a 'Shiboken.ObjectType' object
    I also noticed that png files do not show up in the file dialog despite being in my code
    This code uses PySide6

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

          getFile = QtWidgets.QFileDialog # Parenthesis are missing here
      

      Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #4

      @SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

      getFile = QtWidgets.QFileDialog # Parenthesis are missing here
      

      Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

      I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)

      @SamuraiDestroy
      You were not necessarily missing parentheses on getFile = QtWidgets.QFileDialog --- it depends which subsequent line you look at. It is correct for the getFile.getOpenFileName() statement but wrong for the getFile.selectedFiles() one.

      You have a problem here with your attempted code. QtWidgets.QFileDialog.getOpenFileName() is a static method (see the docs) while QtWidgets.QFileDialog.selectedFiles() is an instance method (see the docs). This means you cannot use them together, they do not work on the same object. selectedFiles() will never look at or see what has happened in the getOpenFileName() call.

      Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need selectedFiles() at all and can use the simple static method:

      (openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")
      

      The static getOpenFileName() puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the Python getOpenFileName() works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]

      If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:

      dialog = QtWidgets.QFileDialog()
      dialog.setWindowTitle("Open Image")
      dialog.setDirectory("~")    # if "~" works, else path to home directory
      dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
      dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
      openedFiles = []
      if dialog.exec():
          openedFiles = dialog.selectedFiles()
      

      More complicated, and the sort of thing which the static getOpenFileName() does internally.

      I suspect you do not want multiple files selected and need only the static code per the first example.

      P.S.
      I just noticed that even if you do want multiple files selected there is actually a static function for that too:

      (openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
      

      Notice getOpenFileNames() plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.

      SGaistS S 2 Replies Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote last edited by SGaist
        #2

        Hi and welcome to devnet,

            getFile = QtWidgets.QFileDialog # Parenthesis are missing here
        

        Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

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

        S JonBJ 2 Replies Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

              getFile = QtWidgets.QFileDialog # Parenthesis are missing here
          

          Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

          S Offline
          S Offline
          SamuraiDestroy
          wrote last edited by
          #3

          @SGaist Thank you so much, that fixed the error, however when I print out the openedFile variable, it only prints an empty list regardless of what was selected. The issue of .png files not showing up is still present however.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

                getFile = QtWidgets.QFileDialog # Parenthesis are missing here
            

            Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by JonB
            #4

            @SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

            getFile = QtWidgets.QFileDialog # Parenthesis are missing here
            

            Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

            I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)

            @SamuraiDestroy
            You were not necessarily missing parentheses on getFile = QtWidgets.QFileDialog --- it depends which subsequent line you look at. It is correct for the getFile.getOpenFileName() statement but wrong for the getFile.selectedFiles() one.

            You have a problem here with your attempted code. QtWidgets.QFileDialog.getOpenFileName() is a static method (see the docs) while QtWidgets.QFileDialog.selectedFiles() is an instance method (see the docs). This means you cannot use them together, they do not work on the same object. selectedFiles() will never look at or see what has happened in the getOpenFileName() call.

            Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need selectedFiles() at all and can use the simple static method:

            (openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")
            

            The static getOpenFileName() puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the Python getOpenFileName() works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]

            If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:

            dialog = QtWidgets.QFileDialog()
            dialog.setWindowTitle("Open Image")
            dialog.setDirectory("~")    # if "~" works, else path to home directory
            dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
            dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
            openedFiles = []
            if dialog.exec():
                openedFiles = dialog.selectedFiles()
            

            More complicated, and the sort of thing which the static getOpenFileName() does internally.

            I suspect you do not want multiple files selected and need only the static code per the first example.

            P.S.
            I just noticed that even if you do want multiple files selected there is actually a static function for that too:

            (openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
            

            Notice getOpenFileNames() plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.

            SGaistS S 2 Replies Last reply
            1
            • JonBJ JonB

              @SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

              getFile = QtWidgets.QFileDialog # Parenthesis are missing here
              

              Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

              I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)

              @SamuraiDestroy
              You were not necessarily missing parentheses on getFile = QtWidgets.QFileDialog --- it depends which subsequent line you look at. It is correct for the getFile.getOpenFileName() statement but wrong for the getFile.selectedFiles() one.

              You have a problem here with your attempted code. QtWidgets.QFileDialog.getOpenFileName() is a static method (see the docs) while QtWidgets.QFileDialog.selectedFiles() is an instance method (see the docs). This means you cannot use them together, they do not work on the same object. selectedFiles() will never look at or see what has happened in the getOpenFileName() call.

              Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need selectedFiles() at all and can use the simple static method:

              (openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")
              

              The static getOpenFileName() puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the Python getOpenFileName() works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]

              If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:

              dialog = QtWidgets.QFileDialog()
              dialog.setWindowTitle("Open Image")
              dialog.setDirectory("~")    # if "~" works, else path to home directory
              dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
              dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
              openedFiles = []
              if dialog.exec():
                  openedFiles = dialog.selectedFiles()
              

              More complicated, and the sort of thing which the static getOpenFileName() does internally.

              I suspect you do not want multiple files selected and need only the static code per the first example.

              P.S.
              I just noticed that even if you do want multiple files selected there is actually a static function for that too:

              (openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
              

              Notice getOpenFileNames() plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote last edited by
              #5

              @JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

              @SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

              getFile = QtWidgets.QFileDialog # Parenthesis are missing here
              

              Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

              I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)

              I did but was way too short on my answer :-)
              I somehow forgot to add the static function bit although I had it mind.

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

              1 Reply Last reply
              0
              • JonBJ JonB

                @SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

                getFile = QtWidgets.QFileDialog # Parenthesis are missing here
                

                Your getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.

                I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)

                @SamuraiDestroy
                You were not necessarily missing parentheses on getFile = QtWidgets.QFileDialog --- it depends which subsequent line you look at. It is correct for the getFile.getOpenFileName() statement but wrong for the getFile.selectedFiles() one.

                You have a problem here with your attempted code. QtWidgets.QFileDialog.getOpenFileName() is a static method (see the docs) while QtWidgets.QFileDialog.selectedFiles() is an instance method (see the docs). This means you cannot use them together, they do not work on the same object. selectedFiles() will never look at or see what has happened in the getOpenFileName() call.

                Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need selectedFiles() at all and can use the simple static method:

                (openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")
                

                The static getOpenFileName() puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the Python getOpenFileName() works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]

                If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:

                dialog = QtWidgets.QFileDialog()
                dialog.setWindowTitle("Open Image")
                dialog.setDirectory("~")    # if "~" works, else path to home directory
                dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
                dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
                openedFiles = []
                if dialog.exec():
                    openedFiles = dialog.selectedFiles()
                

                More complicated, and the sort of thing which the static getOpenFileName() does internally.

                I suspect you do not want multiple files selected and need only the static code per the first example.

                P.S.
                I just noticed that even if you do want multiple files selected there is actually a static function for that too:

                (openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")
                

                Notice getOpenFileNames() plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.

                S Offline
                S Offline
                SamuraiDestroy
                wrote last edited by
                #6

                @JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

                [Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]
                Ah, I somehow missed that before. Yep, adding the extra ) fixed it.

                @JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:

                I just noticed that even if you do want multiple files selected there is actually a static function for that too:

                This works perfectly and does exactly what I was trying to do, thank you

                1 Reply Last reply
                1
                • S SamuraiDestroy has marked this topic as solved

                • Login

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