Does Qt have a way of storing configuration settings in a file? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
The community re开发者_运维技巧viewed whether to reopen this question 1 year ago and left it closed:
Improve this questionOriginal close reason(s) were not resolved
Is there a recomended way of saving my application's settings (like user selections, window size, postion etc.) in a file (ini or any other format) using Qt?
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
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:
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 )
Load the contents of a config file into memory using one of the following methods:
LoadConfigurationValues() LoadConfigurationValues(QString qstrConfigFilenameIn)
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 )
Set the values in a config file using:
setConfigValue( QString qstrConfigValueNameIn, QVariant variantNewValueIn )
Save the in memory config file variables to a config file using one of the following methods:
SaveConfigurationValues() SaveConfigurationValues(QString qstrConfigFilenameIn)
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()
, andclsConfigFileBase::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.
Take a look at the blog post QSettings mit XML-Format. I have used it and am completely happy with XML-based settings. My constructor and *Impl
methods look like this:
Config::Config() {
const QSettings::Format f=QSettings::registerFormat("xml", readImpl, writeImpl);
QSettings::setDefaultFormat(f);
s = new QSettings(f,QSettings::UserScope,QString("MyProject"),QString("settings"));
}
bool Config::readImpl(QIODevice& device, QSettings::SettingsMap& map) {
QXmlStreamReader xmlReader(&device);
QStringList elements;
while (!xmlReader.atEnd() && !xmlReader.hasError()) {
xmlReader.readNext();
if (xmlReader.isStartElement() && xmlReader.name() != "Settings") {
elements.append(xmlReader.name().toString());
} else if (xmlReader.isEndElement()) {
if (!elements.isEmpty()) {
elements.removeLast();
}
} else if (xmlReader.isCharacters() && !xmlReader.isWhitespace()) {
QString key;
for (int i = 0; i < elements.size(); i++) {
if (i != 0) {
key += "/";
}
key += elements.at(i);
}
map[key] = xmlReader.text().toString();
}
}
if (xmlReader.hasError()) {
sipDebug() << xmlReader.errorString();
return false;
}
return true;
}
bool Config::writeImpl(QIODevice& device, const QSettings::SettingsMap& map) {
QXmlStreamWriter xml(&device); xml.setAutoFormatting(true);
xml.writeStartDocument(); xml.writeStartElement("Settings");
QStringList prev; QSettings::SettingsMap::ConstIterator map_i;
for (map_i = map.begin(); map_i != map.end(); map_i++) {
QStringList elements = map_i.key().split("/"); int x = 0;
while (x < prev.size() && elements.at(x) == prev.at(x)) {
x++;
}
for (int i = prev.size() - 1; i >= x; i--) {
xml.writeEndElement();
}
for (int i = x; i < elements.size(); i++) {
xml.writeStartElement(elements.at(i));
}
xml.writeCharacters(map_i.value().toString()); prev = elements;
}
for (int i = 0; i < prev.size(); i++) {
xml.writeEndElement();
}
xml.writeEndElement(); xml.writeEndDocument();
return true;
}
精彩评论