⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsettings.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                iniEscapedString(variantToString(value), block);            }            block += eol;            if (device.write(block) == -1) {                writeError = true;                break;            }        }    }    return !writeError;}/*!    \class QSettings    \brief The QSettings class provides persistent platform-independent application settings.    \ingroup io    \ingroup misc    \mainclass    \reentrant    Users normally expect an application to remember its settings    (window sizes and positions, options, etc.) across sessions. This    information is often stored in the system registry on Windows,    and in XML preferences files on Mac OS X. On Unix systems, in the    absence of a standard, many applications (including the KDE    applications) use INI text files.    QSettings is an abstraction around these technologies, enabling    you to save and restore application settings in a portable    manner. It also supports \l{registerFormat()}{custom storage    formats}.    QSettings's API is based on QVariant, allowing you to save    most value-based types, such as QString, QRect, and QImage,    with the minimum of effort.    \tableofcontents section1    \section1 Basic Usage    When creating a QSettings object, you must pass the name of your    company or organization as well as the name of your application.    For example, if your product is called Star Runner and your    company is called MySoft, you would construct the QSettings    object as follows:    \quotefromfile snippets/settings/settings.cpp    \skipuntil snippet_ctor1    \skipline {    \printline QSettings settings    QSettings objects can be created either on the stack or on    the heap (i.e. using \c new). Constructing and destroying a    QSettings object is very fast.    If you use QSettings from many places in your application, you    might want to specify the organization name and the application    name using QCoreApplication::setOrganizationName() and    QCoreApplication::setApplicationName(), and then use the default    QSettings constructor:    \skipuntil snippet_ctor2    \skipline {    \printline setOrganizationName    \printline setOrganizationDomain    \printline setApplicationName    \dots    \printline QSettings settings;    (Here, we also specify the organization's Internet domain. When    the Internet domain is set, it is used on Mac OS X instead of the    organization name, since Mac OS X applications conventionally use    Internet domains to identify themselves. If no domain is set, a    fake domain is derived from the organization name. See the    \l{Platform-Specific Notes} below for details.)    QSettings stores settings. Each setting consists of a QString    that specifies the setting's name (the \e key) and a QVariant    that stores the data associated with the key. To write a setting,    use setValue(). For example:    \printline setValue(    If there already exists a setting with the same key, the existing    value is overwritten by the new value. For efficiency, the    changes may not be saved to permanent storage immediately. (You    can always call sync() to commit your changes.)    You can get a setting's value back using value():    \printline settings.value(    If there is no setting with the specified name, QSettings    returns a null QVariant (which can be converted to the integer 0).    You can specify another default value by passing a second    argument to value():    \skipline {    \printline /settings.value\(.*,.*\)/    \skipline }    To test whether a given key exists, call contains(). To remove    the setting associated with a key, call remove(). To obtain the    list of all keys, call allKeys(). To remove all keys, call    clear().    \section1 QVariant and GUI Types    Because QVariant is part of the \l QtCore library, it cannot provide    conversion functions to data types such as QColor, QImage, and    QPixmap, which are part of \l QtGui. In other words, there is no    \c toColor(), \c toImage(), or \c toPixmap() functions in QVariant.    Instead, you can use the QVariant::value() or the qVariantValue()    template function. For example:    \code        QSettings settings("MySoft", "Star Runner");        QColor color = settings.value("DataPump/bgcolor").value<QColor>();    \endcode    The inverse conversion (e.g., from QColor to QVariant) is    automatic for all data types supported by QVariant, including    GUI-related types:    \code        QSettings settings("MySoft", "Star Runner");        QColor color = palette().background().color();        settings.setValue("DataPump/bgcolor", color);    \endcode    Custom types registered using qRegisterMetaType() and    qRegisterMetaTypeStreamOperators() can be stored using QSettings.    \section1 Key Syntax    Setting keys can contain any Unicode characters. The Windows    registry and INI files use case-insensitive keys, whereas the    Carbon Preferences API on Mac OS X uses case-sensitive keys. To    avoid portability problems, follow these two simple rules:    \list 1    \o Always refer to the same key using the same case. For example,       if you refer to a key as "text fonts" in one place in your       code, don't refer to it as "Text Fonts" somewhere else.    \o Avoid key names that are identical except for the case. For       example, if you have a key called "MainWindow", don't try to       save another key as "mainwindow".    \endlist    You can form hierarchical keys using the '/' character as a    separator, similar to Unix file paths. For example:    \printline setValue    \printline setValue    \printline setValue    If you want to save or restore many settings with the same    prefix, you can specify the prefix using beginGroup() and call    endGroup() at the end. Here's the same example again, but this    time using the group mechanism:    \printline beginGroup    \printuntil endGroup    \printline beginGroup    \printuntil endGroup    If a group is set using beginGroup(), the behavior of most    functions changes consequently. Groups can be set recursively.    In addition to groups, QSettings also supports an "array"    concept. See beginReadArray() and beginWriteArray() for details.    \section1 Fallback Mechanism    Let's assume that you have created a QSettings object with the    organization name MySoft and the application name Star Runner.    When you look up a value, up to four locations are searched in    that order:    \list 1    \o a user-specific location for the Star Runner application    \o a user-specific location for all applications by MySoft    \o a system-wide location for the Star Runner application    \o a system-wide location for all applications by MySoft    \endlist    (See \l{Platform-Specific Notes} below for information on what    these locations are on the different platforms supported by Qt.)    If a key cannot be found in the first location, the search goes    on in the second location, and so on. This enables you to store    system-wide or organization-wide settings and to override them on    a per-user or per-application basis. To turn off this mechanism,    call setFallbacksEnabled(false).    Although keys from all four locations are available for reading,    only the first file (the user-specific location for the    application at hand) is accessible for writing. To write to any    of the other files, omit the application name and/or specify    QSettings::SystemScope (as opposed to QSettings::UserScope, the    default).    Let's see with an example:    \skipuntil snippet_locations    \skipline {    \printline obj1    \printuntil obj4    The table below summarizes which QSettings objects access    which location. "\bold{X}" means that the location is the main    location associated to the QSettings object and is used both    for reading and for writing; "o" means that the location is used    as a fallback when reading.    \table    \header \o Locations               \o \c{obj1} \o \c{obj2} \o \c{obj3} \o \c{obj4}    \row    \o 1. User, Application    \o \bold{X} \o          \o          \o    \row    \o 2. User, Organization   \o o        \o \bold{X} \o          \o    \row    \o 3. System, Application  \o o        \o          \o \bold{X} \o    \row    \o 4. System, Organization \o o        \o o        \o o        \o \bold{X}    \endtable    The beauty of this mechanism is that it works on all platforms    supported by Qt and that it still gives you a lot of flexibility,    without requiring you to specify any file names or registry    paths.    If you want to use INI files on all platforms instead of the    native API, you can pass QSettings::IniFormat as the first    argument to the QSettings constructor, followed by the scope, the    organization name, and the application name:    \skipline {    \printline /settings\(.*,$/    \printline );    Sometimes you do want to access settings stored in a specific    file or registry path. In that case, you can use a constructor    that takes a file name (or registry path) and a file format. For    example:    \skipline }    \skipline {    \printline /QSettings settings.*Ini/    The file format can either be QSettings::IniFormat or QSettings::NativeFormat.    On Mac OS X, the native format is an XML-based format called \e    plist. On Windows, the native format is the Windows registry, and    the first argument is a path in the registry rather than a file    name, for example:    \skipline }    \skipline {    \printline HKEY    \printline Native    On Unix systems, QSettings::IniFormat and QSettings::NativeFormat    have the same meaning.    The \l{tools/settingseditor}{Settings Editor} example lets you    experiment with different settings location and with fallbacks    turned on or off.    \section1 Restoring the State of a GUI Application    QSettings is often used to store the state of a GUI    application. The following example illustrates how to use we    will use QSettings to save and restore the geometry of an    application's main window.    \skipto ::writeSettings    \printuntil /^\}$/    \skipto ::readSettings    \printuntil /^\}$/    See \l{Window Geometry} for a discussion on why it is better to    call QWidget::resize() and QWidget::move() rather than QWidget::setGeometry()    to restore a window's geometry.    The \c readSettings() and \c writeSettings() functions must be    called from the main window's constructor and close event handler    as follows:    \skipto ::MainWindow    \printuntil {    \dots    \printline readSettings    \printline }    \skipto ::closeEvent    \printuntil /^\}/    See the \l{mainwindows/application}{Application} example for a    self-contained example that uses QSettings.    \section1 Accessing Settings from Multiple Threads or Processes Simultaneously    QSettings is \l{reentrant}. This means that you can use    distinct QSettings object in different threads    simultaneously. This guarantee stands even when the QSettings    objects refer to the same files on disk (or to the same entries    in the system registry). If a setting is modified through one    QSettings object, the change will immediately be visible in    any other QSettings objects that operate on the same location    and that live in the same process.    QSettings can safely be used from different processes (which can    be different instances of your application running at the same    time or different applications altogether) to read and write to    the same system locations. It uses advisory file locking and a    smart merging algorithm to ensure data integrity. Changes    performed by another process aren't visible in the current    process until sync() is called.    \section1 Platform-Specific Notes    As mentioned in the \l{Fallback Mechanism} section, QSettings    stores settings for an application in up to four locations,    depending on whether the settings are user-specific or    system-wide and whether the the settings are application-specific    or organization-wide. For simplicity, we're assuming the    organization is called MySoft and the application is called Star    Runner.    On Unix systems, if the file format is NativeFormat, the    following files are used by default:    \list 1    \o \c{$HOME/.config/MySoft/Star Runner.conf}    \o \c{$HOME/.config/MySoft.conf}    \o \c{/etc/xdg/MySoft/Star Runner.conf}    \o \c{/etc/xdg/MySoft.conf}    \endlist    On Mac OS X versions 10.2 and 10.3, these files are used by    default:    \list 1    \o \c{$HOME/Library/Preferences/com.MySoft.Star Runner.plist}    \o \c{$HOME/Library/Preferences/com.MySoft.plist}    \o \c{/Library/Preferences/com.MySoft.Star Runner.plist}    \o \c{/Library/Preferences/com.MySoft.plist}    \endlist    On Windows, NativeFormat settings are stored in the following    registry paths:    \list 1    \o \c{HKEY_CURRENT_USER\Software\MySoft\Star Runner}    \o \c{HKEY_CURRENT_USER\Software\MySoft}    \o \c{HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner}    \o \c{HKEY_LOCAL_MACHINE\Software\MySoft}    \endlist    If the file format is IniFormat, the following files are    used on Unix and Mac OS X:    \list 1    \o \c{$HOME/.config/MySoft/Star Runner.ini}    \o \c{$HOME/.config/MySoft.ini}    \o \c{/etc/xdg/MySoft/Star Runner.ini}    \o \c{/etc/xdg/MySoft.ini}    \endlist    On Windows, the following files are used:    \list 1    \o \c{%APPDATA%\MySoft\Star Runner.ini}    \o \c{%APPDATA%\MySoft.ini}    \o \c{%COMMON_APPDATA%\MySoft\Star Runner.ini}    \o \c{%COMMON_APPDATA%\MySoft.ini}    \endlist    The \c %APPDATA% path is usually \tt{C:\\Documents and    Settings\\\e{User Name}\\Application Data}; the \c    %COMMON_APPDATA% path is usually \tt{C:\\Documents and    Settings\\All Users\\Application Data}.    The paths for the \c .ini and \c .conf files can be changed using    setPath(). On Unix and Mac OS X, the user can override them by by    setting the \c XDG_CONFIG_HOME environment variable; see    setPath() for details.    While QSettings attempts to smooth over the differences between    the different supported platforms, there are still a few    differences that you should be aware of when porting your    application:    \list    \o  The Windows system registry has the following limitations: A        subkey may not exceed 255 characters, an entry's value may        not exceed 16,383 characters, and all the values of a key may        not exceed 65,535 characters. One way to work around these        limitations is to store the settings using the IniFormat        instead of the NativeFormat.    \o  On Mac OS X, allKeys() will return some extra keys for global        settings that apply to all applications. These keys can be        read using value() but cannot be changed, only shadowed.        Calling setFallbacksEnabled(false) will hide these global        settings.    \o  On Mac OS X, the CFPreferences API used by QSettings expects        Internet domain names rather than organization names. To        provide a uniform API, QSettings derives a fake domain name        from the organization name (unless the organization name        already is a domain name, e.g. OpenOffice.org). The algorithm        appends ".com" to the company name and replaces spaces and        other illegal characters with hyphens. If you want to specify    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -