Can not save config.ini on MacOS platform in .dmg file using qt6.1.2.
-
I compiled the Text2Pcap program and packaged it into Text2Pcap.dmg.
However, after installation, it was found that the program did not generate config.ini in a specific path as expected.
But the package in the Release and Debug directories developed by Qt contains config.ini (the path is inconsistent with the description in the help document) , Windows platform is also normal.
I did not explicitly specify the path of config.ini, I used the Qt default path.
May I ask if there is a usage error in my code? thank you very much.
The code snippet I wrote to the file is as follows:#define CONFIG_FILE_NAME "config.ini" #define WIRESHARK_PCAP_STORAGE_CONFIG "PcapStoragePath" void WiresharkUtils::writePcapStoragePathConfig(QString pcapStoragePath) { QSettings *configSaved = new QSettings(CONFIG_FILE_NAME, QSettings::IniFormat); configSaved->setValue(WIRESHARK_PCAP_STORAGE_CONFIG, pcapStoragePath); }
-
@angelyouyou I am not sure I follow... But if you run the app bundle from the installation dmg - that dmg is usually read-only. May I ask you to describe the broader picture here?
-
@angelyouyou I'd recommend specifying path, preferably using QStandardPaths::ConfigLocation.
Other than that, QSettings is fast and re-entrant, there is no need tonew
it, it can safely be used just by the means ofQSettings settings;
Since QSettings is reentrant it is sufficient to set the parameters for it only once, preferably in the main.cpp file.
You can see where the settings are being stored byqDebug() << settings.fileName();
- that should give you full path to the ini file. -
@artwaw
I think you are right.
Actually I found QSetting saved the ini file to the app's current directory on MacOS platform.
Below is my debug content, the description in documents of qt6.1.2 is wrong.Write pcap storage path config(/Users/constantine/Downloads) to path(/config.ini). Get pcap storage path config() from path(/config.ini).
But what I want to know is why QSetting can write the configuration of the current directory, but cannot read the configuration information?
My code for reading config.ini.#define WIRESHARK_PCAP_STORAGE_CONFIG "PcapStoragePath" #define CONFIG_FILE_NAME "config.ini" QString WiresharkUtils::getPcapStoragePath() { QSettings *configSaved = new QSettings(CONFIG_FILE_NAME, QSettings::IniFormat); QString pcapStoragePath = configSaved->value(WIRESHARK_PCAP_STORAGE_CONFIG).toString(); qDebug("Get pcap storage path config(%s) from path(%s).", qPrintable(pcapStoragePath), qPrintable(configSaved->fileName())); return pcapStoragePath; }
The content of config.ini file(/Applications/Text2Pcap.app/Contents/MacOS/config.ini).
[General] CustomedDisplayWiresharkPath=/Applications/Wireshark.app CustomedWiresharkPath=/Applications/Wireshark.app/Contents/MacOS/Wireshark PcapStoragePath=/Users/constantine/Downloads
-
Please notice that your program writes config into the app bundle (which is not what you want to do in macOS, but that's the default path returned when queried for current directory -> app location directory, that's in platform notes for macOS). Since you don't set, I assume, any parameters for QSettings (except for ini file name) I'd say it is just as expected with no scope set. Again, usual way is to edit your main.cpp so it has:
QCoreApplication::setApplicationVersion(your_version_string); QCoreApplication::setOrganizationName(your_org_name); QCoreApplication::setOrganizationDomain(your_org_domain); // please note that on macOS this one can be quite important QCoreApplication::setApplicationName(your_app_name); // I assume in here should be your ini settings QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setPath(QSettings::IniFormat,QSettings::UserScope,QStandardPaths::writeableLocation(QStandardPaths::DownloadLocation)+"/config.ini"); QApplication a(argc, argv);
and then in any place of your program just
QSettings settings
, like I mentioned above.The problem with your code is that for some reason the default path for QSettings is defaulting to application directory, which is inside the bundle.