📄 qsettings.cpp
字号:
} if (file.status() != IO_Ok) {#ifdef QT_CHECK_STATE qWarning("QSettings::sync: error at end of write");#endif // QT_CHECK_STATE success = FALSE; } file.close(); if ( success ) { QDir dir( QFileInfo( file ).dir( TRUE ) ); if ( dir.exists( filename ) && !dir.remove( filename ) || !dir.rename( file.name(), filename, TRUE ) ) {#ifdef QT_CHECK_STATE qWarning( "QSettings::sync: error writing file '%s'", QFile::encodeName( filename ).data() );#endif // QT_CHECK_STATE success = FALSE; } } // remove temporary file file.remove(); closelock( lockfd ); } d->modified = FALSE; return success;}/*! \fn bool QSettings::readBoolEntry(const QString &key, bool def, bool *ok ) const Reads the entry specified by \a key, and returns a bool, or the default value, \a def, if the entry couldn't be read. If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. \sa readEntry(), readNumEntry(), readDoubleEntry(), writeEntry(), removeEntry()*//*! \internal*/bool QSettings::readBoolEntry(const QString &key, bool def, bool *ok ){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::readBoolEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif if ( ok ) *ok = FALSE; return def; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysReadBoolEntry( grp_key, def, ok );#endif QString value = readEntry( key, ( def ? "true" : "false" ), ok ); if (value.lower() == "true") return TRUE; else if (value.lower() == "false") return FALSE; else if (value == "1") return TRUE; else if (value == "0") return FALSE; if (! value.isEmpty()) qWarning("QSettings::readBoolEntry: '%s' is not 'true' or 'false'", value.latin1()); if ( ok ) *ok = FALSE; return def;}/*! \fn double QSettings::readDoubleEntry(const QString &key, double def, bool *ok ) const Reads the entry specified by \a key, and returns a double, or the default value, \a def, if the entry couldn't be read. If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. \sa readEntry(), readNumEntry(), readBoolEntry(), writeEntry(), removeEntry()*//*! \internal*/double QSettings::readDoubleEntry(const QString &key, double def, bool *ok ){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::readDoubleEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif if ( ok ) *ok = FALSE; return def; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysReadDoubleEntry( grp_key, def, ok );#endif QString value = readEntry( key, QString::number(def), ok ); bool conv_ok; double retval = value.toDouble( &conv_ok ); if ( conv_ok ) return retval; if ( ! value.isEmpty() ) qWarning( "QSettings::readDoubleEntry: '%s' is not a number", value.latin1() ); if ( ok ) *ok = FALSE; return def;}/*! \fn int QSettings::readNumEntry(const QString &key, int def, bool *ok ) const Reads the entry specified by \a key, and returns an integer, or the default value, \a def, if the entry couldn't be read. If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. \sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry()*//*! \internal*/int QSettings::readNumEntry(const QString &key, int def, bool *ok ){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::readNumEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif if ( ok ) *ok = FALSE; return def; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysReadNumEntry( grp_key, def, ok );#endif QString value = readEntry( key, QString::number( def ), ok ); bool conv_ok; int retval = value.toInt( &conv_ok ); if ( conv_ok ) return retval; if ( ! value.isEmpty() ) qWarning( "QSettings::readNumEntry: '%s' is not a number", value.latin1() ); if ( ok ) *ok = FALSE; return def;}/*! \fn QString QSettings::readEntry(const QString &key, const QString &def, bool *ok ) const Reads the entry specified by \a key, and returns a QString, or the default value, \a def, if the entry couldn't be read. If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry()*//*! \internal*/QString QSettings::readEntry(const QString &key, const QString &def, bool *ok ){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::readEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif if ( ok ) *ok = FALSE; return def; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysReadEntry( grp_key, def, ok );#endif if ( ok ) // no, everything is not ok *ok = FALSE; QString realkey; if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) {#ifdef QT_CHECK_STATE qWarning("QSettings::readEntry: invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE if ( ok ) *ok = FALSE; return def; } if (list.count() == 2) { d->heading = list[0]; d->group = "General"; realkey = list[1]; } else { d->heading = list[0]; d->group = list[1]; // remove the group from the list list.remove(list.at(1)); // remove the heading from the list list.remove(list.at(0)); realkey = list.join("/"); } } else { realkey = grp_key; } QSettingsGroup grp = d->readGroup(); QSettingsGroup::const_iterator it = grp.find( realkey ), end = grp.end(); QString retval = def; if ( it != end ) { // found the value we needed retval = *it; if ( ok ) *ok = TRUE; } return retval;}#if !defined(Q_NO_BOOL_TYPE)/*! Writes the boolean entry \a value into key \a key. The \a key is created if it doesn't exist. Any previous value is overwritten by \a value. If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, bool value){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return FALSE; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysWriteEntry( grp_key, value );#endif QString s(value ? "true" : "false"); return writeEntry(key, s);}#endif/*! \overload Writes the double entry \a value into key \a key. The \a key is created if it doesn't exist. Any previous value is overwritten by \a value. If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, double value){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return FALSE; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysWriteEntry( grp_key, value );#endif QString s(QString::number(value)); return writeEntry(key, s);}/*! \overload Writes the integer entry \a value into key \a key. The \a key is created if it doesn't exist. Any previous value is overwritten by \a value. If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, int value){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return FALSE; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysWriteEntry( grp_key, value );#endif QString s(QString::number(value)); return writeEntry(key, s);}/*! \internal Writes the entry specified by \a key with the string-literal \a value, replacing any previous setting. If \a value is zero-length or null, the entry is replaced by an empty setting. \e NOTE: This function is provided because some compilers use the writeEntry (const QString &, bool) overload for this code: writeEntry ("/foo/bar", "baz") If an error occurs, this functions returns FALSE and the object is left unchanged. \sa readEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, const char *value){ return writeEntry(key, QString(value));}/*! \overload Writes the string entry \a value into key \a key. The \a key is created if it doesn't exist. Any previous value is overwritten by \a value. If \a value is an empty string or a null string the key's value will be an empty string. If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, const QString &value){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return FALSE; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysWriteEntry( grp_key, value );#endif // NOTE: we *do* allow value to be a null/empty string QString realkey; if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) {#ifdef QT_CHECK_STATE qWarning("QSettings::writeEntry: invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE return FALSE; } if (list.count() == 2) { d->heading = list[0]; d->group = "General"; realkey = list[1]; } else { d->heading = list[0]; d->group = list[1]; // remove the group from the list list.remove(list.at(1)); // remove the heading from the list list.remove(list.at(0)); realkey = list.join("/"); } } else { realkey = grp_key; } d->writeGroup(realkey, value); return TRUE;}/*! Removes the entry specified by \a key. Returns TRUE if the entry existed and was removed; otherwise returns FALSE. \sa readEntry(), writeEntry()*/bool QSettings::removeEntry(const QString &key){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::removeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return FALSE; }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysRemoveEntry( grp_key );#endif QString realkey; if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) {#ifdef QT_CHECK_STATE qWarning("QSettings::removeEntry: invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE return FALSE; } if (list.count() == 2) { d->heading = list[0]; d->group = "General"; realkey = list[1]; } else { d->heading = list[0]; d->group = list[1]; // remove the group from the list list.remove(list.at(1)); // remove the heading from the list list.remove(list.at(0)); realkey = list.join("/"); } } else { realkey = grp_key; } d->removeGroup(realkey); return TRUE;}/*! Returns a list of the keys which contain entries under \a key. Does \e not return any keys that contain keys. Example settings: \code /MyCompany/MyApplication/background color /MyCompany/MyApplication/foreground color /MyCompany/MyApplication/geometry/x /MyCompany/MyApplication/geometry/y /MyCompany/MyApplication/geometry/width /MyCompany/MyApplication/geometry/height \endcode \code QStringList keys = entryList( "/MyCompany/MyApplication" ); \endcode \c keys contains 'background color' and 'foreground color'. It does not contain 'geometry' because this key contains keys not entries. To access the geometry values could either use subkeyList() to read the keys and then read each entry, or simply read each entry
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -