QFileDialog::getOpenFileName works differently on Windows and on MacOS
-
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:
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().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!!
-
@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)); }
-
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. -
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.@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!
-
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? -
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?@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.
-
@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)); }
-
@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)); }
@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!!!
-
-
@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!!!
-
@AJ-Moon Hi,
What was the issue with the filter ? It might help someone else in the future :-)
@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. -
@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.@SimonSchroeder I need to get my glasses fixed ! I missed the
;
while reading 😅