doc/src/snippets/settings/settings.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 QWidget *win;
       
    45 QWidget *panel;
       
    46 
       
    47 void snippet_ctor1()
       
    48 {
       
    49 //! [0]
       
    50     QSettings settings("MySoft", "Star Runner");
       
    51 //! [0]
       
    52 }
       
    53 
       
    54 void snippet_ctor2()
       
    55 {
       
    56 //! [1]
       
    57     QCoreApplication::setOrganizationName("MySoft");
       
    58 //! [1] //! [2]
       
    59     QCoreApplication::setOrganizationDomain("mysoft.com");
       
    60 //! [2] //! [3]
       
    61     QCoreApplication::setApplicationName("Star Runner");
       
    62 //! [3]
       
    63 
       
    64 //! [4]
       
    65     QSettings settings;
       
    66 //! [4]
       
    67 
       
    68 //! [5]
       
    69     settings.setValue("editor/wrapMargin", 68);
       
    70 //! [5] //! [6]
       
    71     int margin = settings.value("editor/wrapMargin").toInt();
       
    72 //! [6]
       
    73     {
       
    74 //! [7]
       
    75     int margin = settings.value("editor/wrapMargin", 80).toInt();
       
    76 //! [7]
       
    77     }
       
    78 
       
    79 //! [8]
       
    80     settings.setValue("mainwindow/size", win->size());
       
    81 //! [8] //! [9]
       
    82     settings.setValue("mainwindow/fullScreen", win->isFullScreen());
       
    83 //! [9] //! [10]
       
    84     settings.setValue("outputpanel/visible", panel->isVisible());
       
    85 //! [10]
       
    86 
       
    87 //! [11]
       
    88     settings.beginGroup("mainwindow");
       
    89     settings.setValue("size", win->size());
       
    90     settings.setValue("fullScreen", win->isFullScreen());
       
    91     settings.endGroup();
       
    92 //! [11]
       
    93 
       
    94 //! [12]
       
    95     settings.beginGroup("outputpanel");
       
    96     settings.setValue("visible", panel->isVisible());
       
    97     settings.endGroup();
       
    98 //! [12]
       
    99 }
       
   100 
       
   101 void snippet_locations()
       
   102 {
       
   103 //! [13]
       
   104     QSettings obj1("MySoft", "Star Runner");
       
   105 //! [13] //! [14]
       
   106     QSettings obj2("MySoft");
       
   107     QSettings obj3(QSettings::SystemScope, "MySoft", "Star Runner");
       
   108     QSettings obj4(QSettings::SystemScope, "MySoft");
       
   109 //! [14]
       
   110 
       
   111     {
       
   112 //! [15]
       
   113     QSettings settings(QSettings::IniFormat, QSettings::UserScope,
       
   114                        "MySoft", "Star Runner");
       
   115 //! [15]
       
   116     }
       
   117 
       
   118     {
       
   119     QSettings settings("starrunner.ini", QSettings::IniFormat);
       
   120     }
       
   121 
       
   122     {
       
   123     QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft",
       
   124                        QSettings::NativeFormat);
       
   125     }
       
   126 }
       
   127 
       
   128 class MainWindow : public QMainWindow
       
   129 {
       
   130 public:
       
   131     MainWindow();
       
   132 
       
   133     void writeSettings();
       
   134     void readSettings();
       
   135 
       
   136 protected:
       
   137     void closeEvent(QCloseEvent *event);
       
   138 };
       
   139 
       
   140 //! [16]
       
   141 void MainWindow::writeSettings()
       
   142 {
       
   143     QSettings settings("Moose Soft", "Clipper");
       
   144 
       
   145     settings.beginGroup("MainWindow");
       
   146     settings.setValue("size", size());
       
   147     settings.setValue("pos", pos());
       
   148     settings.endGroup();
       
   149 }
       
   150 //! [16]
       
   151 
       
   152 //! [17]
       
   153 void MainWindow::readSettings()
       
   154 {
       
   155     QSettings settings("Moose Soft", "Clipper");
       
   156 
       
   157     settings.beginGroup("MainWindow");
       
   158     resize(settings.value("size", QSize(400, 400)).toSize());
       
   159     move(settings.value("pos", QPoint(200, 200)).toPoint());
       
   160     settings.endGroup();
       
   161 }
       
   162 //! [17]
       
   163 
       
   164 //! [18]
       
   165 MainWindow::MainWindow()
       
   166 {
       
   167 //! [18] //! [19]
       
   168     readSettings();
       
   169 //! [19] //! [20]
       
   170 }
       
   171 //! [20]
       
   172 
       
   173 bool userReallyWantsToQuit() { return true; }
       
   174 
       
   175 //! [21]
       
   176 void MainWindow::closeEvent(QCloseEvent *event)
       
   177 {
       
   178     if (userReallyWantsToQuit()) {
       
   179         writeSettings();
       
   180         event->accept();
       
   181     } else {
       
   182         event->ignore();
       
   183     }
       
   184 }
       
   185 //! [21]