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. Put a button in a GUI in order to open a folder not a file
Forum Updated to NodeBB v4.3 + New Features

Put a button in a GUI in order to open a folder not a file

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 5.3k Views
  • 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.
  • J Offline
    J Offline
    john_hobbyist
    wrote on 17 Jul 2022, 20:11 last edited by john_hobbyist
    #1

    Hello I am trying to put a button icon on a GUI Widget, so that when I press it to let me select a folder not a file. After google searching I found this: https://stackoverflow.com/questions/41587541/pyqt-assign-to-a-button-the-ability-to-choose-a-directory So, my try is this:

    dialog = QFileDialog()
    file = dialog.getExistingDirectory(None, "Select Folder")
    self.tb.addWidget(dialog)
    

    The problem is that when I execute the code, the first thing I see is a select directory menu, not the GUI on which I need to place the icon button and when I choose folder the code collapse. When I remove the above code, everything works fine.... Do you see something strange on my code?

    J 1 Reply Last reply 17 Jul 2022, 20:24
    0
    • J john_hobbyist
      17 Jul 2022, 20:11

      Hello I am trying to put a button icon on a GUI Widget, so that when I press it to let me select a folder not a file. After google searching I found this: https://stackoverflow.com/questions/41587541/pyqt-assign-to-a-button-the-ability-to-choose-a-directory So, my try is this:

      dialog = QFileDialog()
      file = dialog.getExistingDirectory(None, "Select Folder")
      self.tb.addWidget(dialog)
      

      The problem is that when I execute the code, the first thing I see is a select directory menu, not the GUI on which I need to place the icon button and when I choose folder the code collapse. When I remove the above code, everything works fine.... Do you see something strange on my code?

      J Offline
      J Offline
      JonB
      wrote on 17 Jul 2022, 20:24 last edited by
      #2

      @john_hobbyist
      You need to write this in the slot attached to the button's clicked() signal.

      1 Reply Last reply
      2
      • J Offline
        J Offline
        john_hobbyist
        wrote on 18 Jul 2022, 08:22 last edited by john_hobbyist
        #3

        @john_hobbyist said in Put a button in a GUI in order to open a folder not a file:

        file = dialog.getExistingDirectory(None, "Select Folder")

        Ok, I have build a QPushButton that when pressed it calls do_something() method. What do I put inside the method so that I can choose a directory and not a file...??

        Because when I tried file = dialog.getExistingDirectory(None, "Select Folder") with QPushButton I got this error:
        AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory'

        J J 2 Replies Last reply 18 Jul 2022, 08:27
        0
        • J john_hobbyist
          18 Jul 2022, 08:22

          @john_hobbyist said in Put a button in a GUI in order to open a folder not a file:

          file = dialog.getExistingDirectory(None, "Select Folder")

          Ok, I have build a QPushButton that when pressed it calls do_something() method. What do I put inside the method so that I can choose a directory and not a file...??

          Because when I tried file = dialog.getExistingDirectory(None, "Select Folder") with QPushButton I got this error:
          AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory'

          J Offline
          J Offline
          JonB
          wrote on 18 Jul 2022, 08:27 last edited by
          #4

          @john_hobbyist said in Put a button in a GUI in order to open a folder not a file:

          file = dialog.getExistingDirectory(None, "Select Folder")
          AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory'

          Then your dialog variable must have been a QPushButton.... And that is not what you show with your earlier dialog = QFileDialog().

          1 Reply Last reply
          1
          • J john_hobbyist
            18 Jul 2022, 08:22

            @john_hobbyist said in Put a button in a GUI in order to open a folder not a file:

            file = dialog.getExistingDirectory(None, "Select Folder")

            Ok, I have build a QPushButton that when pressed it calls do_something() method. What do I put inside the method so that I can choose a directory and not a file...??

            Because when I tried file = dialog.getExistingDirectory(None, "Select Folder") with QPushButton I got this error:
            AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory'

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 18 Jul 2022, 08:28 last edited by
            #5

            @john_hobbyist apparently, you named your QPushButton dialog.

            getExistingDirectory is a static function, you can simply call it this way:

            QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                            "/home",
                                                            QFileDialog::ShowDirsOnly
                                                            | QFileDialog::DontResolveSymlinks);
            

            https://doc.qt.io/qt-6/qfiledialog.html#getExistingDirectory


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • J Offline
              J Offline
              john_hobbyist
              wrote on 18 Jul 2022, 08:34 last edited by
              #6

              @JonB Yes, I deleted the code I showed on first post and I wrote new code as you told me to use different rationale...What should I do now with the method do_something() ??

              @J-Hilk Thanks but I do not know how to transform C/C++ to python...I use PyQt5...

              J 1 Reply Last reply 18 Jul 2022, 08:39
              0
              • J john_hobbyist
                18 Jul 2022, 08:34

                @JonB Yes, I deleted the code I showed on first post and I wrote new code as you told me to use different rationale...What should I do now with the method do_something() ??

                @J-Hilk Thanks but I do not know how to transform C/C++ to python...I use PyQt5...

                J Offline
                J Offline
                JonB
                wrote on 18 Jul 2022, 08:39 last edited by
                #7

                @john_hobbyist
                But you had something which worked for this originally: file = dialog.getExistingDirectory(None, "Select Folder"). Or you might change it to:

                dirName = QFileDialog.getExistingDirectory(self, "Select Folder")
                

                You don't need or use the dialog = QFileDialog() for calling getExistingDirectory() because it is a static method of QFileDialog.

                1 Reply Last reply
                3
                • J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 18 Jul 2022, 08:49 last edited by
                  #8

                  @john_hobbyist moved this to Qt for Python subform, since it is the python part you're having problems with :D


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1

                  1/8

                  17 Jul 2022, 20:11

                  • Login

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