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. Point getOpenFileName to parent of current working directory
QtWS25 Last Chance

Point getOpenFileName to parent of current working directory

Scheduled Pinned Locked Moved Unsolved Qt for Python
11 Posts 5 Posters 650 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.
  • E Offline
    E Offline
    Ed Schneider
    wrote on last edited by
    #1

    In getOpenFileName how do I get it to open the parent of the current working directory?

    jsulmJ 1 Reply Last reply
    0
    • E Ed Schneider

      In getOpenFileName how do I get it to open the parent of the current working directory?

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

      @Ed-Schneider Well, you pass that folder as const QString &dir = QString() parameter.
      Use

      QDir::current() + "/.."
      

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

      E 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Ed-Schneider Well, you pass that folder as const QString &dir = QString() parameter.
        Use

        QDir::current() + "/.."
        
        E Offline
        E Offline
        Ed Schneider
        wrote on last edited by
        #3

        @jsulm said in Point getOpenFileName to parent of current working directory:

        "/.."

        Can you create an example of calling GetOpenFile Name pointing to the parent directory? I tried several variations and could not get it to work. I'm working in Python with PySide6 and Windows. Thanks

        JonBJ M 2 Replies Last reply
        0
        • E Ed Schneider

          @jsulm said in Point getOpenFileName to parent of current working directory:

          "/.."

          Can you create an example of calling GetOpenFile Name pointing to the parent directory? I tried several variations and could not get it to work. I'm working in Python with PySide6 and Windows. Thanks

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Ed-Schneider
          He did. Use QDir.current() + "/.." or even just "..", and pass that as the directory.

          1 Reply Last reply
          1
          • E Ed Schneider

            @jsulm said in Point getOpenFileName to parent of current working directory:

            "/.."

            Can you create an example of calling GetOpenFile Name pointing to the parent directory? I tried several variations and could not get it to work. I'm working in Python with PySide6 and Windows. Thanks

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @Ed-Schneider
            try:

            QDir dir;
            dir.cdUp();
            qDebug()<<dir.path();
            
            JonBJ 1 Reply Last reply
            0
            • M mpergand

              @Ed-Schneider
              try:

              QDir dir;
              dir.cdUp();
              qDebug()<<dir.path();
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @mpergand
              That has the side-effect of permanently changing the application's current directory, just to open a file.
              (Or does it only change the directory of that QDir instance? In any case, in the politest way, I don't see this would help, I don't think QFileDialog::getOpenFileName takes a QDir.)
              Just passing the .. is all that is needed here.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Ed Schneider
                wrote on last edited by
                #7

                I got a working solution from ChatGPT -

                current_directory = os.getcwd()
                parent_directory = os.path.dirname(current_working_directory)

                and then use keyword dir=parent_directory in the call on getOpenFileName

                JonBJ 1 Reply Last reply
                0
                • E Ed Schneider

                  I got a working solution from ChatGPT -

                  current_directory = os.getcwd()
                  parent_directory = os.path.dirname(current_working_directory)

                  and then use keyword dir=parent_directory in the call on getOpenFileName

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @Ed-Schneider Did you try just dir=".."?

                  E 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Ed-Schneider Did you try just dir=".."?

                    E Offline
                    E Offline
                    Ed Schneider
                    wrote on last edited by
                    #9

                    @JonB That does indeed work as well. Thanks

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      friedemannkleint
                      wrote on last edited by
                      #10

                      These days, pathlib is recommended:

                      from pathlib import Path
                      dir = Path.cwd().parent

                      getOpenFileName(... , os.fspath(dir), ....

                      E 1 Reply Last reply
                      2
                      • F friedemannkleint

                        These days, pathlib is recommended:

                        from pathlib import Path
                        dir = Path.cwd().parent

                        getOpenFileName(... , os.fspath(dir), ....

                        E Offline
                        E Offline
                        Ed Schneider
                        wrote on last edited by
                        #11

                        @friedemannkleint That does work as well. Glad you added this. This is what I will be using. Surprised this wasn't offered sooner. Danke.

                        1 Reply Last reply
                        0

                        • Login

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