1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QtGui>
52
53 QWidget *win;
54 QWidget *panel;
55
snippet_ctor1()56 void snippet_ctor1()
57 {
58 //! [0]
59 QSettings settings("MySoft", "Star Runner");
60 //! [0]
61 }
62
snippet_ctor2()63 void snippet_ctor2()
64 {
65 //! [1]
66 QCoreApplication::setOrganizationName("MySoft");
67 //! [1] //! [2]
68 QCoreApplication::setOrganizationDomain("mysoft.com");
69 //! [2] //! [3]
70 QCoreApplication::setApplicationName("Star Runner");
71 //! [3]
72
73 //! [4]
74 QSettings settings;
75 //! [4]
76
77 //! [5]
78 settings.setValue("editor/wrapMargin", 68);
79 //! [5] //! [6]
80 int margin = settings.value("editor/wrapMargin").toInt();
81 //! [6]
82 {
83 //! [7]
84 int margin = settings.value("editor/wrapMargin", 80).toInt();
85 //! [7]
86 }
87
88 //! [8]
89 settings.setValue("mainwindow/size", win->size());
90 //! [8] //! [9]
91 settings.setValue("mainwindow/fullScreen", win->isFullScreen());
92 //! [9] //! [10]
93 settings.setValue("outputpanel/visible", panel->isVisible());
94 //! [10]
95
96 //! [11]
97 settings.beginGroup("mainwindow");
98 settings.setValue("size", win->size());
99 settings.setValue("fullScreen", win->isFullScreen());
100 settings.endGroup();
101 //! [11]
102
103 //! [12]
104 settings.beginGroup("outputpanel");
105 settings.setValue("visible", panel->isVisible());
106 settings.endGroup();
107 //! [12]
108 }
109
snippet_locations()110 void snippet_locations()
111 {
112 //! [13]
113 QSettings obj1("MySoft", "Star Runner");
114 //! [13] //! [14]
115 QSettings obj2("MySoft");
116 QSettings obj3(QSettings::SystemScope, "MySoft", "Star Runner");
117 QSettings obj4(QSettings::SystemScope, "MySoft");
118 //! [14]
119
120 {
121 //! [15]
122 QSettings settings(QSettings::IniFormat, QSettings::UserScope,
123 "MySoft", "Star Runner");
124 //! [15]
125 }
126
127 {
128 QSettings settings("starrunner.ini", QSettings::IniFormat);
129 }
130
131 {
132 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft",
133 QSettings::NativeFormat);
134 }
135 }
136
137 class MainWindow : public QMainWindow
138 {
139 public:
140 MainWindow();
141
142 void writeSettings();
143 void readSettings();
144
145 protected:
146 void closeEvent(QCloseEvent *event) override;
147 };
148
149 //! [16]
writeSettings()150 void MainWindow::writeSettings()
151 {
152 QSettings settings("Moose Soft", "Clipper");
153
154 settings.beginGroup("MainWindow");
155 settings.setValue("size", size());
156 settings.setValue("pos", pos());
157 settings.endGroup();
158 }
159 //! [16]
160
161 //! [17]
readSettings()162 void MainWindow::readSettings()
163 {
164 QSettings settings("Moose Soft", "Clipper");
165
166 settings.beginGroup("MainWindow");
167 resize(settings.value("size", QSize(400, 400)).toSize());
168 move(settings.value("pos", QPoint(200, 200)).toPoint());
169 settings.endGroup();
170 }
171 //! [17]
172
173 //! [18]
MainWindow()174 MainWindow::MainWindow()
175 {
176 //! [18] //! [19]
177 readSettings();
178 //! [19] //! [20]
179 }
180 //! [20]
181
userReallyWantsToQuit()182 bool userReallyWantsToQuit() { return true; }
183
184 //! [21]
closeEvent(QCloseEvent * event)185 void MainWindow::closeEvent(QCloseEvent *event)
186 {
187 if (userReallyWantsToQuit()) {
188 writeSettings();
189 event->accept();
190 } else {
191 event->ignore();
192 }
193 }
194 //! [21]
195