📄 qsettings.cpp
字号:
directly by specifying its full key, e.g. "/MyCompany/MyApplication/geometry/y". \sa subkeyList()*/QStringList QSettings::entryList(const QString &key) const{ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::entryList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return QStringList(); }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysEntryList( grp_key );#endif QString realkey; if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 1) {#ifdef QT_CHECK_STATE qWarning("QSettings::listEntries: invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE return QStringList(); } if (list.count() == 1) { d->heading = list[0]; d->group = "General"; } 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::Iterator it = grp.begin(); QStringList ret; QString itkey; while (it != grp.end()) { itkey = it.key(); ++it; if ( realkey.length() > 0 ) { if ( itkey.left( realkey.length() ) != realkey ) continue; else itkey.remove( 0, realkey.length() + 1 ); } if ( itkey.find( '/' ) != -1 ) continue; ret << itkey; } return ret;}/*! Returns a list of the keys which contain keys under \a key. Does \e not return any keys that contain entries. 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 /MyCompany/MyApplication/recent files/1 /MyCompany/MyApplication/recent files/2 /MyCompany/MyApplication/recent files/3 \endcode \code QStringList keys = subkeyList( "/MyCompany/MyApplication" ); \endcode \c keys contains 'geometry' and 'recent files'. It does not contain 'background color' or 'foreground color' because they are keys which contain entries not keys. To get a list of keys that have values rather than subkeys use entryList(). \sa entryList()*/QStringList QSettings::subkeyList(const QString &key) const{ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::subkeyList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return QStringList(); }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return d->sysSubkeyList( grp_key );#endif QString realkey; int subkeycount = 2; if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 1) {#ifdef QT_CHECK_STATE qWarning("QSettings::subkeyList: invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE return QStringList(); } subkeycount = list.count(); if (list.count() == 1) { d->heading = list[0]; d->group = "General"; } 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; QStringList ret; if ( subkeycount == 1 ) { QMap<QString,QSettingsHeading>::Iterator it = d->headings.begin(); while ( it != d->headings.end() ) { if ( it.key() != "General" && ! ret.contains( it.key() ) ) ret << it.key(); ++it; } return ret; } QSettingsGroup grp = d->readGroup(); QSettingsGroup::Iterator it = grp.begin(); QString itkey; while (it != grp.end()) { itkey = it.key(); ++it; if ( realkey.length() > 0 ) { if ( itkey.left( realkey.length() ) != realkey ) continue; else itkey.remove( 0, realkey.length() + 1 ); } int slash = itkey.find( '/' ); if ( slash == -1 ) continue; itkey.truncate( slash ); if ( ! ret.contains( itkey ) ) ret << itkey; } return ret;}/*! \internal This function returns the time of last modification for \a key.*/QDateTime QSettings::lastModificationTime( const QString &key ){ QString grp_key( groupKey( group(), key ) ); if ( !qt_verify_key( grp_key ) ) {#if defined(QT_CHECK_STATE) qWarning( "QSettings::lastModificationTime: Invalid key '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );#endif return QDateTime(); }#if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return QDateTime();#endif if (grp_key[0] == '/') { // parse our key QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) {#ifdef QT_CHECK_STATE qWarning("QSettings::lastModificationTime: Invalid key '%s'", grp_key.latin1());#endif // QT_CHECK_STATE return QDateTime(); } if (list.count() == 2) { d->heading = list[0]; d->group = "General"; } else { d->heading = list[0]; d->group = list[1]; } } return d->modificationTime();}/*! \overload \obsolete Writes the string list 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. The list is stored as a sequence of strings separated by \a separator (using QStringList::join()), so none of the strings in the list should contain the separator. If the list is empty or null the key's value will be an empty string. \warning The list should not contain empty or null strings, as readListEntry() will use QStringList::split() to recreate the list. As the documentation states, QStringList::split() will omit empty strings from the list. Because of this, it is impossible to retrieve identical list data that is stored with this function. We recommend using the writeEntry() and readListEntry() overloads that do not take a \a separator argument. If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry(), QStringList::join()*/bool QSettings::writeEntry(const QString &key, const QStringList &value, const QChar &separator){ QString s(value.join(separator)); return writeEntry(key, s);}/*! \overload Writes the string list 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 returns TRUE. \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()*/bool QSettings::writeEntry(const QString &key, const QStringList &value){ QString s; for (QStringList::ConstIterator it=value.begin(); it!=value.end(); ++it) { QString el = *it; if ( el.isNull() ) { el = "^0"; } else { el.replace("^", "^^"); } s+=el; s+="^e"; // end of element } return writeEntry(key, s);}/*! \overload QStringList QSettings::readListEntry(const QString &key, const QChar &separator, bool *ok ) const \obsolete Reads the entry specified by \a key as a string. The \a separator is used to create a QStringList by calling QStringList::split(\a separator, entry). If \a ok is not 0: \a *ok is set to TRUE if the key was read, otherwise \a *ok is set to FALSE. \warning As the documentation states, QStringList::split() will omit empty strings from the list. Because of this, it is impossible to retrieve identical list data with this function. We recommend using the readListEntry() and writeEntry() overloads that do not take a \a separator argument. Note that if you want to iterate over the list, you should iterate over a copy, e.g. \code QStringList list = mySettings.readListEntry( "size", " " ); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; } \endcode \sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), QStringList::split()*//*! \internal*/QStringList QSettings::readListEntry(const QString &key, const QChar &separator, bool *ok ){ QString value = readEntry( key, QString::null, ok ); if ( ok && !*ok ) return QStringList(); return QStringList::split(separator, value);}/*! \fn QStringList QSettings::readListEntry(const QString &key, bool *ok ) const Reads the entry specified by \a key as a string. If \a ok is not 0, \a *ok is set to TRUE if the key was read, otherwise \a *ok is set to FALSE. Note that if you want to iterate over the list, you should iterate over a copy, e.g. \code QStringList list = mySettings.readListEntry( "recentfiles" ); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; } \endcode \sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), QStringList::split()*//*! \internal*/QStringList QSettings::readListEntry(const QString &key, bool *ok ){ QString value = readEntry( key, QString::null, ok ); if ( ok && !*ok ) return QStringList(); QStringList l; QString s; bool esc=FALSE; for (int i=0; i<(int)value.length(); i++) { if ( esc ) { if ( value[i] == 'e' ) { // end-of-string l.append(s); s=""; } else if ( value[i] == '0' ) { // null string s=QString::null; } else { s.append(value[i]); } esc=FALSE; } else if ( value[i] == '^' ) { esc = TRUE; } else { s.append(value[i]); if ( i == (int)value.length()-1 ) l.append(s); } } return l;}#ifdef Q_OS_MACvoid qt_setSettingsBasePath(const QString &); //qsettings_mac.cpp#endif/*! Insert platform-dependent paths from platform-independent information. The \a domain should be an Internet domain name controlled by the producer of the software, eg. Trolltech products use "trolltech.com". The \a product should be the official name of the product. The \a scope should be QSettings::User for user-specific settings, or QSettings::Global for system-wide settings (generally these will be read-only to many users). Not all information is relevant on all systems (e.g. scoping is currently used only if QSettings accesses the Windows registry).*/void QSettings::setPath( const QString &domain, const QString &product, Scope scope ){// On Windows, any trailing ".com(\..*)" is stripped from the domain. The// Global scope corresponds to HKEY_LOCAL_MACHINE, and User corresponds to// HKEY_CURRENT_USER. Note that on some installations, not all users can// write to the Global scope. On UNIX, any trailing ".com(\..*)" is stripped// from the domain. The Global scope corresponds to "/opt" (this would be// configurable at library build time - eg. to "/usr/local" or "/usr"),// while the User scope corresponds to $HOME/.*rc.// Note that on most installations, not all users can write to the System// scope.//// On MacOS X, if there is no "." in domain, append ".com", then reverse the// order of the elements (Mac OS uses "com.apple.finder" as domain+product).// The Global scope corresponds to /Library/Preferences/*.plist, while the// User scope corresponds to ~/Library/Preferences/*.plist.// Note that on most installations, not all users can write to the System// scope. d->globalScope = scope == Global; QString actualSearchPath; int lastDot = domain.findRev( '.' );#if defined(Q_WS_WIN) actualSearchPath = "/" + domain.mid( 0, lastDot ) + "/" + product; insertSearchPath( Windows, actualSearchPath );#elif !defined(QWS) && defined(Q_OS_MAC) if(lastDot != -1) { QString topLevelDomain = domain.right( domain.length() - lastDot - 1 ) + "."; if ( !topLevelDomain.isEmpty() ) qt_setSettingsBasePath( topLevelDomain ); } actualSearchPath = "/" + domain.left( lastDot ) + "." + product; insertSearchPath( Mac, actualSearchPath );#else actualSearchPath = "/" + domain.mid( 0, lastDot ) + "/" + product; insertSearchPath( Unix, actualSearchPath );#endif}/*! Appends \a group to the current key prefix. \code QSettings settings; settings.beginGroup( "/MainWindow" ); // read values settings.endGroup(); \endcode*/void QSettings::beginGroup( const QString &group ){ d->groupStack.push( group ); d->groupDirty = TRUE;}/*! Undo previous calls to beginGroup(). Note that a single beginGroup("a/b/c") is undone by a single call to endGroup(). \code QSettings settings; settings.beginGroup( "/MainWindow/Geometry" ); // read values settings.endGroup(); \endcode*/void QSettings::endGroup(){ d->groupStack.pop(); d->groupDirty = TRUE;}/*! Set the current key prefix to the empty string.*/void QSettings::resetGroup(){ d->groupStack.clear(); d->groupDirty = FALSE; d->groupPrefix = QString::null;}/*! Returns the current key prefix, or a null string if there is no key prefix set. \sa beginGroup();*/QString QSettings::group() const{ if ( d->groupDirty ) { d->groupDirty = FALSE; d->groupPrefix = QString::null; QValueStack<QString>::Iterator it = d->groupStack.begin(); while ( it != d->groupStack.end() ) { QString group = *it; ++it; if ( group[0] != '/' ) group.prepend( "/" ); d->groupPrefix += group; } } return d->groupPrefix;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -