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 //! [0]
createApplication(int & argc,char * argv[])52 QCoreApplication* createApplication(int &argc, char *argv[])
53 {
54     for (int i = 1; i < argc; ++i) {
55         if (!qstrcmp(argv[i], "-no-gui"))
56             return new QCoreApplication(argc, argv);
57     }
58     return new QApplication(argc, argv);
59 }
60 
main(int argc,char * argv[])61 int main(int argc, char* argv[])
62 {
63     QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
64 
65     if (qobject_cast<QApplication *>(app.data())) {
66        // start GUI version...
67     } else {
68        // start non-GUI version...
69     }
70 
71     return app->exec();
72 }
73 //! [0]
74 
75 
76 //! [1]
77 QApplication::setStyle(QStyleFactory::create("Fusion"));
78 //! [1]
79 
80 
81 // ### fixme: Qt 6: Remove [2]
82 //! [2]
main(int argc,char * argv[])83 int main(int argc, char *argv[])
84 {
85     QApplication::setColorSpec(QApplication::ManyColor);
86     QApplication app(argc, argv);
87     ...
88     return app.exec();
89 }
90 //! [2]
91 
92 
93 //! [3]
sizeHint() const94 QSize MyWidget::sizeHint() const
95 {
96     return QSize(80, 25);
97 }
98 //! [3]
99 
100 
101 //! [4]
showAllHiddenTopLevelWidgets()102 void showAllHiddenTopLevelWidgets()
103 {
104     const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
105     for (QWidget *widget : topLevelWidgets) {
106         if (widget->isHidden())
107             widget->show();
108     }
109 }
110 //! [4]
111 
112 
113 //! [5]
updateAllWidgets()114 void updateAllWidgets()
115 {
116     const QWidgetList allWidgets = QApplication::allWidgets();
117     for (QWidget *widget : allWidgets)
118         widget->update();
119 }
120 //! [5]
121 
122 
123 //! [6]
main(int argc,char * argv[])124 int main(int argc, char *argv[])
125 {
126     QApplication::setDesktopSettingsAware(false);
127     QApplication app(argc, argv);
128     ...
129     return app.exec();
130 }
131 //! [6]
132 
133 
134 //! [7]
135 if ((startPos - currentPos).manhattanLength() >=
136         QApplication::startDragDistance())
137     startTheDrag();
138 //! [7]
139 
140 
141 //! [8]
commitData(QSessionManager & manager)142 void MyApplication::commitData(QSessionManager& manager)
143 {
144     if (manager.allowsInteraction()) {
145         int ret = QMessageBox::warning(
146                     mainWindow,
147                     tr("My Application"),
148                     tr("Save changes to document?"),
149                     QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
150 
151         switch (ret) {
152         case QMessageBox::Save:
153             manager.release();
154             if (!saveDocument())
155                 manager.cancel();
156             break;
157         case QMessageBox::Discard:
158             break;
159         case QMessageBox::Cancel:
160         default:
161             manager.cancel();
162         }
163     } else {
164         // we did not get permission to interact, then
165         // do something reasonable instead
166     }
167 }
168 //! [8]
169 
170 
171 //! [9]
172 appname -session id
173 //! [9]
174 
175 
176 //! [10]
177 const QStringList commands = mySession.restartCommand();
178 for (const QString &command : commands)
179     do_something(command);
180 //! [10]
181 
182 
183 //! [11]
184 const QStringList commands = mySession.discardCommand();
185 for (const QString &command : commands)
186     do_something(command);
187 //! [11]
188 
189 
190 //! [12]
191 QWidget *widget = QApplication::widgetAt(x, y);
192 if (widget)
193     widget = widget->window();
194 //! [12]
195 
196 
197 //! [13]
198 QWidget *widget = QApplication::widgetAt(point);
199 if (widget)
200     widget = widget->window();
201 //! [13]
202