Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog::getOpenFileName works differently on Windows and on MacOS

QFileDialog::getOpenFileName works differently on Windows and on MacOS

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 493 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.
  • A Offline
    A Offline
    AJ Moon
    wrote on 11 Apr 2025, 01:39 last edited by AJ Moon 4 Nov 2025, 01:44
    #1

    Hello,

    I am very new to Qt and Qt Creator and am exploring features. However, I found very weird behavior of QFileDialog::getOpenFileName between Windows and MacOS (Apple Silicon M1 Max).
    What I am building is a music player. When I select a file, it will open and there will be play, pause, and stop button with volume slider. The problem is when I try to open a file. Here's the problematic code:
    Screenshot 2025-04-11 at 10.28.45.jpg

    Following code is what I wrote to open a mp3 file. It works perfectly fine on Windows. When open button clicked, it opens a new window with homePath and can open mp3 files only. Nevertheless, when I try this code on MacOS, it pops up a window to select a file, but I cannot open ANY files.
    Without the QDir::homePath(), I can choose a file but like below, I can now select any files without the extension filter. I tried to put absolute path something like "/Users/ajmoon/" but it still works the same as QDir::homePath().

    Screenshot 2025-04-11 at 10.29.29.jpg

    What could be the possible solution for MacOS? and what makes the code behave different on MacOS and on Windows when Qt is meant for cross platform? Thank you so much for every hard work and passion about Qt!!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kevin Hoang
      wrote on 12 Apr 2025, 09:38 last edited by
      #6

      @AJ-Moon I tested the following code on my Mac to open an MP3 file with the Music app, and it worked without any issues, no special permissions needed.

             QString fileName = QFileDialog::getOpenFileName(this, tr("Open Music"), "", tr("Audio Files (*.mp3 *.wav *.aac)"));
             if (!fileName.isEmpty()) {
                 QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
             }
      
      A 1 Reply Last reply 30 days ago
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote on 11 Apr 2025, 12:50 last edited by
        #2

        I think the issue is related to file permissions on macOS (Catalina and later).
        Try copying a few MP3 files into your app’s build folder and test again.
        If it works, it’s likely because the original files are in protected folders that require extra permissions.

        A 1 Reply Last reply 11 Apr 2025, 14:50
        1
        • K Kevin Hoang
          11 Apr 2025, 12:50

          I think the issue is related to file permissions on macOS (Catalina and later).
          Try copying a few MP3 files into your app’s build folder and test again.
          If it works, it’s likely because the original files are in protected folders that require extra permissions.

          A Offline
          A Offline
          AJ Moon
          wrote on 11 Apr 2025, 14:50 last edited by
          #3

          @Kevin-Hoang I added Qt Creator to full disk access, copied a few mp3 files and put in the build folder. It still does not work. I am also assuming it is related MacOS' file permission but cannot find an answer to work it as is. Thanks for your replying though!

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kevin Hoang
            wrote on 11 Apr 2025, 15:39 last edited by
            #4

            It's not about giving permission to Qt Creator, you need to set the permissions for your own app.
            By the way, how did you play MP3 files on macOS?

            A 1 Reply Last reply 12 Apr 2025, 08:10
            0
            • K Kevin Hoang
              11 Apr 2025, 15:39

              It's not about giving permission to Qt Creator, you need to set the permissions for your own app.
              By the way, how did you play MP3 files on macOS?

              A Offline
              A Offline
              AJ Moon
              wrote on 12 Apr 2025, 08:10 last edited by
              #5

              @Kevin-Hoang I see, I am going to try to look up more about giving my own app permission! I played mp3 files with Music app.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kevin Hoang
                wrote on 12 Apr 2025, 09:38 last edited by
                #6

                @AJ-Moon I tested the following code on my Mac to open an MP3 file with the Music app, and it worked without any issues, no special permissions needed.

                       QString fileName = QFileDialog::getOpenFileName(this, tr("Open Music"), "", tr("Audio Files (*.mp3 *.wav *.aac)"));
                       if (!fileName.isEmpty()) {
                           QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
                       }
                
                A 1 Reply Last reply 30 days ago
                0
                • K Kevin Hoang
                  12 Apr 2025, 09:38

                  @AJ-Moon I tested the following code on my Mac to open an MP3 file with the Music app, and it worked without any issues, no special permissions needed.

                         QString fileName = QFileDialog::getOpenFileName(this, tr("Open Music"), "", tr("Audio Files (*.mp3 *.wav *.aac)"));
                         if (!fileName.isEmpty()) {
                             QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
                         }
                  
                  A Offline
                  A Offline
                  AJ Moon
                  wrote 30 days ago last edited by
                  #7

                  @Kevin-Hoang Oh yeah I just found the issue that I had ; in my filter string. Just a dump mistake made everything totally weird!! Sorry for bothering you with this stupid question and thank you for your answers!!!

                  S 1 Reply Last reply 30 days ago
                  0
                  • A AJ Moon has marked this topic as solved 30 days ago
                  • A AJ Moon
                    30 days ago

                    @Kevin-Hoang Oh yeah I just found the issue that I had ; in my filter string. Just a dump mistake made everything totally weird!! Sorry for bothering you with this stupid question and thank you for your answers!!!

                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote 30 days ago last edited by
                    #8

                    @AJ-Moon Hi,

                    What was the issue with the filter ? It might help someone else in the future :-)

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

                    S 1 Reply Last reply 28 days ago
                    0
                    • S SGaist
                      30 days ago

                      @AJ-Moon Hi,

                      What was the issue with the filter ? It might help someone else in the future :-)

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote 28 days ago last edited by
                      #9

                      @SGaist said in QFileDialog::getOpenFileName works differently on Windows and on MacOS:

                      What was the issue with the filter ?

                      Like he said it was the extra ; at the end of the filter string. Just to be clear: if you want to have several different filters those are separated by two semicolons ;;. In this case it might have played out differently.

                      S 1 Reply Last reply 28 days ago
                      0
                      • S SimonSchroeder
                        28 days ago

                        @SGaist said in QFileDialog::getOpenFileName works differently on Windows and on MacOS:

                        What was the issue with the filter ?

                        Like he said it was the extra ; at the end of the filter string. Just to be clear: if you want to have several different filters those are separated by two semicolons ;;. In this case it might have played out differently.

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote 28 days ago last edited by
                        #10

                        @SimonSchroeder I need to get my glasses fixed ! I missed the ; while reading 😅

                        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

                        1/10

                        11 Apr 2025, 01:39

                        • Login

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