Qt - How to save a configuration file on multiple platforms

前端 未结 4 937
一生所求
一生所求 2021-01-20 15:35

I\'m writing a Qt application that needs to save some settings to the user\'s configuration directory.

I\'ve come up with the following code to get this folder:

相关标签:
4条回答
  • 2021-01-20 16:14

    Depends on the settings you want to record, but I would suggest to use QSettings.

    0 讨论(0)
  • 2021-01-20 16:22

    I use QSettings for simple values that make sense to put in the Windows registry or an INI file, and SQLite for more complicated setups. In Qt5, put the SQLite database in QStandardPaths::writableLocation(QStandardPaths::ConfigLocation). In Qt4, put the SQLite database in QDesktopServices as gnud suggested (deprecated in Qt5).

    0 讨论(0)
  • 2021-01-20 16:27

    In addition to provided answers suggesting to use QSettings, I will shamelessly copy here Qt's own example from documentation (I slightly edited it, though).

    Here are the functions to read and write settings from the file/registry/whatever:

    void MainWindow::readSettings()
    {
        QSettings settings("Moose Soft", "Clipper");
    
        settings.beginGroup("MainWindow");
        resize(settings.value("size", QSize(400, 400)).toSize()); // note the 400x400 defaults if there is no saved settings yet
        move(settings.value("pos", QPoint(200, 200)).toPoint()); // here default pos is at 200,200
        settings.endGroup();
    }
    
    void MainWindow::writeSettings()
    {
        QSettings settings("Moose Soft", "Clipper");
    
        settings.beginGroup("MainWindow");
        settings.setValue("size", size());
        settings.setValue("pos", pos());
        settings.endGroup();
    }
    

    The readSettings() function may be called from the MainWindow constructor:

    MainWindow::MainWindow()
    {
        ...
        readSettings();
    }
    

    while writeSettings() from the close event handler:

    void MainWindow::closeEvent(QCloseEvent *event)
    {
            writeSettings();
            event->accept();
    }
    

    Last but not least, take a note, that settings format may be different. For example, if you want settings to be saved in the registry in Windows, you should pass QSettings::NativeFormat to the constructor, but if you'd like text config in the %appdata%, pass QSettings::IniFormat instead. In the example above format isn't passed, so it's native by default.

    My personal preference to set IniFormat for Windows (because registry data isn't easily transferable) and NativeFormat for Linux and macOS, like this:

    QSettings *settings;
    
    if ( (os == "Linux") | (os == "macOS") ) {
        settings = new QSettings(QSettings::NativeFormat, QSettings::UserScope, "Moose Soft", "Clipper");
    } else {
        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "Moose Soft", "Clipper");
    };
    
    0 讨论(0)
  • 2021-01-20 16:30

    Yes, this is supposed to be an easy task - but you're not supposed to hard-code paths. You should use the native APIs on win/mac, and an enviroment variable on most newer unix-like. Of course, Qt helps you do this cross-platform.

    Follow Tibur's suggestion and use QSettings if it's configuration data for your Qt app. You can also get the config directory by using the appropriate QT api.

    QT4: QDesktopServices::storageLocation(QDesktopServices::DataLocation)
    QT5: QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)

    0 讨论(0)
提交回复
热议问题