Does Qt have a way of storing configuration settings in a file?

If you mean the config file for compiling, then it's project (pro) file. If you wanna store some settings for you own program, you can try QSettings. Of course, you can write a class to read/write config file organized by yourself.


To address this exact issue, I have a config file library I've been working on for several years now. I have incorporated it into several programs and it seems pretty stable. If anyone is interested I can post the doxygen docs and/or the source code.

Here is a section of the docs with an overview:


clsConfigFileBase is the base class for a config file access object

Description:

clsConfigFileBase is the primary engine for config file processing. To use the config file system you need to derive a class from clsConfigFileBase and use your derived class to:

  1. Define the contents of a config file using one or more of the following methods:

    ConfigValue RegisterConfigValue( QString qstrConfigValueNameIn, 
        QVariant::Type VariantTypeIn )
    
    ConfigValue RegisterConfigValue( QString qstrConfigValueNameIn,
        QVariant::Type VariantTypeIn, QString qstrWhatsThisTextIn )
    
    ConfigValue RegisterConfigValue( clsConfigValueData::ConfigValueSource 
        ConfigValueSourceIn, QString qstrConfigValueNameIn, QVariant::Type 
        VariantTypeIn )
    
    ConfigValue RegisterConfigValue( clsConfigValueData::ConfigValueSource 
        ConfigValueSourceIn, QString qstrConfigValueNameIn, QVariant::Type 
        VariantTypeIn, QString qstrWhatsThisTextIn )
    
    void RegisterConfigValue( ConfigValue ConfigValueIn, QVariant::Type 
        VariantTypeIn )
    
    void RegisterConfigValue( ConfigValue ConfigValueIn, QVariant::Type 
        VariantTypeIn, QString qstrWhatsThisTextIn )
    
    void RegisterConfigValue( const ConfigValue ConfigValueIn, const QString 
        qstrVariantTypeNameIn, const QString qstrWhatsThisTextIn )
    
    DeclareListToLoadAndSave( QString qstrPathConfigValueNameIn, QString 
        qstrConfigValueNameIn )
    
  2. Load the contents of a config file into memory using one of the following methods:

    LoadConfigurationValues()
    
    LoadConfigurationValues(QString qstrConfigFilenameIn)
    
  3. Access the contents of a config file using one of the following methods:

    getConfigValue( QString qstrConfigValueNameIn )
    
    getBoolConfigValue( QString qstrNameOfConfigValueIn )
    
    getBrushConfigValue( QString qstrNameOfConfigValueIn )
    
    getIntConfigValue( QString qstrNameOfConfigValueIn )
    
    getPaletteConfigValue( QString qstrNameOfConfigValueIn )
    
    getRectConfigValue( QString qstrNameOfConfigValueIn )
    
    getStringConfigValue( QString qstrNameOfConfigValueIn )
    
    getStringListConfigValue( QString qstrNameOfConfigValueIn )
    
  4. Set the values in a config file using:

    setConfigValue( QString qstrConfigValueNameIn, QVariant variantNewValueIn )
    
  5. Save the in memory config file variables to a config file using one of the following methods:

    SaveConfigurationValues()
    
    SaveConfigurationValues(QString qstrConfigFilenameIn)
    
  6. Create widgets which can be used to change the contents of a config value using one of the following methods:

    CreateCheckBox( QString qstrNameOfConfigValueIn )
    
    CreateComboBox(QString qstrNameOfConfigValueIn, QStringList 
        stringlistComboBoxItemsIn, QLabel * & labelComboBoxOut )
    
    CreateComboBox(QString qstrNameOfConfigValueIn, QStringList 
        stringlistComboBoxItemsIn )
    
    CreateLineEdit( QString qstrNameOfConfigValueIn )
    
    CreateLineEdit( QString qstrNameOfConfigValueIn, QLabel * & labelOut )
    
    CreateLineEdit( QString qstrNameOfConfigValueIn, QHBoxLayout * & layoutOut, 
        QLabel * & labelLineEditLabelOut )
    

clsConfigFileBase provides other methods to manage the config file access object such as:

  • clsConfigFileBase::AddItemToStringList(),
  • clsConfigFileBase::getDebugModeIsEnabled(),
  • clsConfigFileBase::getStringConfigValue(), and
  • clsConfigFileBase::getTotalConfigValues()

I also have a config file editor program that makes the above functionality much easier to access. The config file editor program also has doxygen documentation.

If you have any questions, post a comment.


If you're trying to store settings for your own application in a config file, I've used QSettings like this before:

QSettings settings(QString("configs/config.ini"), QSettings::IniFormat);
QString someValue = settings.value("some/config/key", "default value if unset").toString(); // settings.value() returns QVariant

And exmaple configs/config.ini file:

[some]
config/key=whatever string here

Tags:

File

Qt

Config