From 0bc02fd0d61d1e4aed9b39890d28975dff30e822 Mon Sep 17 00:00:00 2001 From: Casper van Donderen Date: Tue, 20 Mar 2012 19:37:07 +0100 Subject: Doc: Prepare for building modular QtCore docs. This change fixes most qdoc errors in QtCore. There are about 900 left. The main thing this change does is moving documentation from qtcore from /doc/src to /src/corelib/doc. Other issues resolved are mis-use of qdoc commands. Change-Id: I002d01edfb13575e8bf27ce91596a577a92562d1 Reviewed-by: Lars Knoll Reviewed-by: Jason McDonald Reviewed-by: Jerome Pasion --- .../doc/snippets/code/doc_src_containers.cpp | 275 ++++++++++ src/corelib/doc/snippets/code/doc_src_groups.cpp | 66 +++ .../doc/snippets/code/doc_src_objecttrees.cpp | 60 +++ .../doc/snippets/code/doc_src_properties.cpp | 132 +++++ .../doc/snippets/code/doc_src_qalgorithms.cpp | 354 +++++++++++++ src/corelib/doc/snippets/code/doc_src_qcache.cpp | 57 ++ .../doc/snippets/code/doc_src_qiterator.cpp | 420 +++++++++++++++ .../doc/snippets/code/doc_src_qnamespace.cpp | 59 +++ .../doc/snippets/code/doc_src_qnamespace.qdoc | 43 ++ src/corelib/doc/snippets/code/doc_src_qpair.cpp | 55 ++ src/corelib/doc/snippets/code/doc_src_qplugin.cpp | 64 +++ src/corelib/doc/snippets/code/doc_src_qplugin.pro | 44 ++ src/corelib/doc/snippets/code/doc_src_qset.cpp | 166 ++++++ src/corelib/doc/snippets/code/doc_src_qtcore.cpp | 43 ++ .../doc/snippets/code/doc_src_qvarlengtharray.cpp | 78 +++ .../doc/snippets/code/qlogging/qlogging.cpp | 54 ++ .../code/src_corelib_codecs_qtextcodec.cpp | 75 +++ .../code/src_corelib_concurrent_qthreadpool.cpp | 53 ++ .../snippets/code/src_corelib_global_qglobal.cpp | 585 +++++++++++++++++++++ .../code/src_corelib_io_qabstractfileengine.cpp | 120 +++++ .../snippets/code/src_corelib_io_qdatastream.cpp | 121 +++++ .../doc/snippets/code/src_corelib_io_qdir.cpp | 175 ++++++ .../snippets/code/src_corelib_io_qdiriterator.cpp | 52 ++ .../doc/snippets/code/src_corelib_io_qfile.cpp | 75 +++ .../doc/snippets/code/src_corelib_io_qfileinfo.cpp | 146 +++++ .../doc/snippets/code/src_corelib_io_qiodevice.cpp | 99 ++++ .../doc/snippets/code/src_corelib_io_qprocess.cpp | 134 +++++ .../doc/snippets/code/src_corelib_io_qsettings.cpp | 316 +++++++++++ .../snippets/code/src_corelib_io_qtemporarydir.cpp | 53 ++ .../code/src_corelib_io_qtemporaryfile.cpp | 53 ++ .../snippets/code/src_corelib_io_qtextstream.cpp | 129 +++++ .../doc/snippets/code/src_corelib_io_qurl.cpp | 87 +++ ...src_corelib_kernel_qabstracteventdispatcher.cpp | 43 ++ .../code/src_corelib_kernel_qabstractitemmodel.cpp | 99 ++++ .../code/src_corelib_kernel_qcoreapplication.cpp | 126 +++++ .../code/src_corelib_kernel_qmetaobject.cpp | 136 +++++ .../snippets/code/src_corelib_kernel_qmetatype.cpp | 114 ++++ .../snippets/code/src_corelib_kernel_qmimedata.cpp | 108 ++++ .../snippets/code/src_corelib_kernel_qobject.cpp | 493 +++++++++++++++++ .../code/src_corelib_kernel_qsystemsemaphore.cpp | 61 +++ .../snippets/code/src_corelib_kernel_qtimer.cpp | 52 ++ .../snippets/code/src_corelib_kernel_qvariant.cpp | 136 +++++ .../code/src_corelib_mimetype_qmimedatabase.cpp | 47 ++ .../snippets/code/src_corelib_plugin_qlibrary.cpp | 84 +++ .../doc/snippets/code/src_corelib_plugin_quuid.cpp | 44 ++ .../src_corelib_statemachine_qstatemachine.cpp | 55 ++ .../snippets/code/src_corelib_thread_qatomic.cpp | 98 ++++ .../snippets/code/src_corelib_thread_qmutex.cpp | 193 +++++++ .../code/src_corelib_thread_qmutexpool.cpp | 61 +++ .../code/src_corelib_thread_qreadwritelock.cpp | 109 ++++ .../code/src_corelib_thread_qsemaphore.cpp | 73 +++ .../snippets/code/src_corelib_thread_qthread.cpp | 56 ++ .../src_corelib_thread_qwaitcondition_unix.cpp | 89 ++++ .../snippets/code/src_corelib_tools_qbitarray.cpp | 176 +++++++ .../snippets/code/src_corelib_tools_qbytearray.cpp | 421 +++++++++++++++ .../snippets/code/src_corelib_tools_qdatetime.cpp | 146 +++++ .../code/src_corelib_tools_qeasingcurve.cpp | 44 ++ .../doc/snippets/code/src_corelib_tools_qhash.cpp | 299 +++++++++++ .../code/src_corelib_tools_qlinkedlist.cpp | 204 +++++++ .../snippets/code/src_corelib_tools_qlistdata.cpp | 267 ++++++++++ .../snippets/code/src_corelib_tools_qlocale.cpp | 95 ++++ .../doc/snippets/code/src_corelib_tools_qmap.cpp | 313 +++++++++++ .../doc/snippets/code/src_corelib_tools_qpoint.cpp | 149 ++++++ .../doc/snippets/code/src_corelib_tools_qqueue.cpp | 48 ++ .../doc/snippets/code/src_corelib_tools_qrect.cpp | 50 ++ .../snippets/code/src_corelib_tools_qregexp.cpp | 224 ++++++++ .../code/src_corelib_tools_qregularexpression.cpp | 289 ++++++++++ .../code/src_corelib_tools_qscopedpointer.cpp | 138 +++++ .../doc/snippets/code/src_corelib_tools_qsize.cpp | 136 +++++ .../snippets/code/src_corelib_tools_qstring.cpp | 94 ++++ .../snippets/code/src_corelib_tools_qtimeline.cpp | 55 ++ .../snippets/code/src_corelib_tools_qvector.cpp | 183 +++++++ .../snippets/code/src_corelib_xml_qxmlstream.cpp | 67 +++ .../snippets/code/src_gui_dialogs_qmessagebox.cpp | 148 ++++++ .../code/src_gui_itemviews_qidentityproxymodel.cpp | 64 +++ .../code/src_gui_itemviews_qitemselectionmodel.cpp | 50 ++ 76 files changed, 10180 insertions(+) create mode 100644 src/corelib/doc/snippets/code/doc_src_containers.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_groups.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_objecttrees.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_properties.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qcache.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qiterator.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qnamespace.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qnamespace.qdoc create mode 100644 src/corelib/doc/snippets/code/doc_src_qpair.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qplugin.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qplugin.pro create mode 100644 src/corelib/doc/snippets/code/doc_src_qset.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qtcore.cpp create mode 100644 src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp create mode 100644 src/corelib/doc/snippets/code/qlogging/qlogging.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_codecs_qtextcodec.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qabstractfileengine.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qdatastream.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qdir.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qdiriterator.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qfile.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qfileinfo.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qiodevice.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qsettings.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qtemporarydir.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qtemporaryfile.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qtextstream.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qabstracteventdispatcher.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qmetaobject.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qmetatype.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qmimedata.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qsystemsemaphore.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qtimer.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_mimetype_qmimedatabase.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_plugin_qlibrary.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_plugin_quuid.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qatomic.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qmutex.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qmutexpool.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qreadwritelock.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qsemaphore.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qstring.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_xml_qxmlstream.cpp create mode 100644 src/corelib/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp create mode 100644 src/corelib/doc/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp create mode 100644 src/corelib/doc/snippets/code/src_gui_itemviews_qitemselectionmodel.cpp (limited to 'src/corelib/doc/snippets/code') diff --git a/src/corelib/doc/snippets/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp new file mode 100644 index 0000000000..c74b0f79dc --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp @@ -0,0 +1,275 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class Employee +{ +public: + Employee() {} + Employee(const Employee &other); + + Employee &operator=(const Employee &other); + +private: + QString myName; + QDate myDateOfBirth; +}; +//! [0] + + +//! [1] +QList list; +list << "A" << "B" << "C" << "D"; + +QListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [1] + + +//! [2] +QListIterator i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [2] + + +//! [3] +QMutableListIterator i(list); +while (i.hasNext()) { + if (i.next() % 2 != 0) + i.remove(); +} +//! [3] + + +//! [4] +QMutableListIterator i(list); +i.toBack(); +while (i.hasPrevious()) { + if (i.previous() % 2 != 0) + i.remove(); +} +//! [4] + + +//! [5] +QMutableListIterator i(list); +while (i.hasNext()) { + if (i.next() > 128) + i.setValue(128); +} +//! [5] + + +//! [6] +QMutableListIterator i(list); +while (i.hasNext()) + i.next() *= 2; +//! [6] + + +//! [7] +QMap map; +map.insert("Paris", "France"); +map.insert("Guatemala City", "Guatemala"); +map.insert("Mexico City", "Mexico"); +map.insert("Moscow", "Russia"); +... + +QMutableMapIterator i(map); +while (i.hasNext()) { + if (i.next().key().endsWith("City")) + i.remove(); +} +//! [7] + + +//! [8] +QMap map; +QHash hash; + +QMapIterator i(map); +while (i.hasNext()) { + i.next(); + hash.insert(i.key(), i.value()); +} +//! [8] + + +//! [9] +QMutableMapIterator i(map); +while (i.findNext(widget)) + i.remove(); +//! [9] + + +//! [10] +QList list; +list << "A" << "B" << "C" << "D"; + +QList::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i = (*i).toLower(); +//! [10] + + +//! [11] +QList list; +list << "A" << "B" << "C" << "D"; + +QList::iterator i = list.end(); +while (i != list.begin()) { + --i; + *i = (*i).toLower(); +} +//! [11] + + +//! [12] +QList::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + qDebug() << *i; +//! [12] + + +//! [13] +QMap map; +... +QMap::const_iterator i; +for (i = map.constBegin(); i != map.constEnd(); ++i) + qDebug() << i.key() << ":" << i.value(); +//! [13] + + +//! [14] +// RIGHT +const QList sizes = splitter->sizes(); +QList::const_iterator i; +for (i = sizes.begin(); i != sizes.end(); ++i) + ... + +// WRONG +QList::const_iterator i; +for (i = splitter->sizes().begin(); + i != splitter->sizes().end(); ++i) + ... +//! [14] + + +//! [15] +QLinkedList list; +... +QString str; +foreach (str, list) + qDebug() << str; +//! [15] + + +//! [16] +QLinkedList list; +... +QLinkedListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [16] + + +//! [17] +QLinkedList list; +... +foreach (const QString &str, list) + qDebug() << str; +//! [17] + + +//! [18] +QLinkedList list; +... +foreach (const QString &str, list) { + if (str.isEmpty()) + break; + qDebug() << str; +} +//! [18] + + +//! [19] +QMap map; +... +foreach (const QString &str, map.keys()) + qDebug() << str << ":" << map.value(str); +//! [19] + + +//! [20] +QMultiMap map; +... +foreach (const QString &str, map.uniqueKeys()) { + foreach (int i, map.values(str)) + qDebug() << str << ":" << i; +} +//! [20] + + +//! [21] +forever { + ... +} +//! [21] + + +//! [22] +CONFIG += no_keywords +//! [22] + + +//! [23] +QString onlyLetters(const QString &in) +{ + QString out; + for (int j = 0; j < in.size(); ++j) { + if (in[j].isLetter()) + out += in[j]; + } + return out; +} +//! [23] diff --git a/src/corelib/doc/snippets/code/doc_src_groups.cpp b/src/corelib/doc/snippets/code/doc_src_groups.cpp new file mode 100644 index 0000000000..abfbfc59c0 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_groups.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +void QPen::setStyle(Qt::PenStyle style) +{ + detach(); // detach from common data + d->style = style; // set the style member +} + +void QPen::detach() +{ + if (d->ref != 1) { + ... // perform a deep copy + } +} +//! [0] + + +//! [1] +QPixmap p1, p2; +p1.load("image.bmp"); +p2 = p1; // p1 and p2 share data + +QPainter paint; +paint.begin(&p2); // cuts p2 loose from p1 +paint.drawText(0,50, "Hi"); +paint.end(); +//! [1] diff --git a/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp b/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp new file mode 100644 index 0000000000..62d556057b --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//![0] +int main() +{ + QWidget window; + QPushButton quit("Quit", &window); + ... +} +//![0] + + +//![1] +int main() +{ + QPushButton quit("Quit"); + QWidget window; + + quit.setParent(&window); + ... +} +//![1] diff --git a/src/corelib/doc/snippets/code/doc_src_properties.cpp b/src/corelib/doc/snippets/code/doc_src_properties.cpp new file mode 100644 index 0000000000..b88fc22084 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_properties.cpp @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +Q_PROPERTY(type name + READ getFunction + [WRITE setFunction] + [RESET resetFunction] + [NOTIFY notifySignal] + [REVISION int] + [DESIGNABLE bool] + [SCRIPTABLE bool] + [STORED bool] + [USER bool] + [CONSTANT] + [FINAL]) +//! [0] + + +//! [1] +Q_PROPERTY(bool focus READ hasFocus) +Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) +Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor) +//! [1] + + +//! [2] +Q_PROPERTY(QDate date READ getDate WRITE setDate) +//! [2] + + +//! [3] +QPushButton *button = new QPushButton; +QObject *object = button; + +button->setDown(true); +object->setProperty("down", true); +//! [3] + + +//! [4] +QObject *object = ... +const QMetaObject *metaobject = object->metaObject(); +int count = metaobject->propertyCount(); +for (int i=0; iproperty(i); + const char *name = metaproperty.name(); + QVariant value = object->property(name); + ... +} +//! [4] + + +//! [5] +class MyClass : public QObject +{ + Q_OBJECT + Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged) + Q_ENUMS(Priority) + +public: + MyClass(QObject *parent = 0); + ~MyClass(); + + enum Priority { High, Low, VeryHigh, VeryLow }; + + void setPriority(Priority priority) + { + m_priority = priority; + emit priorityChanged(priority); + } + Priority priority() const + { return m_priority; } + +signals: + void priorityChanged(Priority); + +private: + Priority m_priority; +}; +//! [5] + + +//! [6] +MyClass *myinstance = new MyClass; +QObject *object = myinstance; + +myinstance->setPriority(MyClass::VeryHigh); +object->setProperty("priority", "VeryHigh"); +//! [6] + + +//! [7] +Q_CLASSINFO("Version", "3.0.0") +//! [7] diff --git a/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp new file mode 100644 index 0000000000..20e64b61fa --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp @@ -0,0 +1,354 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QStringList list; +list << "one" << "two" << "three"; + +qFill(list.begin(), list.end(), "eleven"); +// list: [ "eleven", "eleven", "eleven" ] +//! [0] + + +//! [1] +qFill(list.begin() + 1, list.end(), "six"); +// list: [ "eleven", "six", "six" ] +//! [1] + + +//! [2] +QChar resolveEntity(const QString &entity) +{ + static const QLatin1String name_table[] = { + "AElig", "Aacute", ..., "zwnj" + }; + static const ushort value_table[] = { + 0x0061, 0x00c1, ..., 0x200c + }; + int N = sizeof(name_table) / sizeof(name_table[0]); + + const QLatin1String *name = qBinaryFind(name_table, name_table + N, + entity); + int index = name - name_table; + if (index == N) + return QChar(); + + return QChar(value_table[index]); +} +//! [2] + + +//! [3] +QChar resolveEntity(const QString &entity) +{ + static QMap entityMap; + + if (!entityMap) { + entityMap.insert("AElig", 0x0061); + entityMap.insert("Aacute", 0x00c1); + ... + entityMap.insert("zwnj", 0x200c); + } + return QChar(entityMap.value(entity)); +} +//! [3] + + +//! [4] +QStringList list; +list << "one" << "two" << "three"; + +QVector vect1(3); +qCopy(list.begin(), list.end(), vect1.begin()); +// vect: [ "one", "two", "three" ] + +QVector vect2(8); +qCopy(list.begin(), list.end(), vect2.begin() + 2); +// vect: [ "", "", "one", "two", "three", "", "", "" ] +//! [4] + + +//! [5] +QStringList list; +list << "one" << "two" << "three"; + +QVector vect(5); +qCopyBackward(list.begin(), list.end(), vect.end()); +// vect: [ "", "", "one", "two", "three" ] +//! [5] + + +//! [6] +QStringList list; +list << "one" << "two" << "three"; + +QVector vect(3); +vect[0] = "one"; +vect[1] = "two"; +vect[2] = "three"; + +bool ret1 = qEqual(list.begin(), list.end(), vect.begin()); +// ret1 == true + +vect[2] = "seven"; +bool ret2 = qEqual(list.begin(), list.end(), vect.begin()); +// ret2 == false +//! [6] + + +//! [7] +QStringList list; +list << "one" << "two" << "three"; + +qFill(list.begin(), list.end(), "eleven"); +// list: [ "eleven", "eleven", "eleven" ] + +qFill(list.begin() + 1, list.end(), "six"); +// list: [ "eleven", "six", "six" ] +//! [7] + + +//! [8] +QStringList list; +list << "one" << "two" << "three"; + +QStringList::iterator i1 = qFind(list.begin(), list.end(), "two"); +// i1 == list.begin() + 1 + +QStringList::iterator i2 = qFind(list.begin(), list.end(), "seventy"); +// i2 == list.end() +//! [8] + + +//! [9] +QList list; +list << 3 << 3 << 6 << 6 << 6 << 8; + +int countOf6 = 0; +qCount(list.begin(), list.end(), 6, countOf6); +// countOf6 == 3 + +int countOf7 = 0; +qCount(list.begin(), list.end(), 7, countOf7); +// countOf7 == 0 +//! [9] + + +//! [10] +double pi = 3.14; +double e = 2.71; + +qSwap(pi, e); +// pi == 2.71, e == 3.14 +//! [10] + + +//! [11] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end()); +// list: [ 6, 12, 12, 33, 68 ] +//! [11] + + +//! [12] +bool caseInsensitiveLessThan(const QString &s1, const QString &s2) +{ + return s1.toLower() < s2.toLower(); +} + +int doSomething() +{ + QStringList list; + list << "AlPha" << "beTA" << "gamma" << "DELTA"; + qSort(list.begin(), list.end(), caseInsensitiveLessThan); + // list: [ "AlPha", "beTA", "DELTA", "gamma" ] +} +//! [12] + + +//! [13] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qGreater()); +// list: [ 68, 33, 12, 12, 6 ] +//! [13] + + +//! [14] +QStringList list; +list << "AlPha" << "beTA" << "gamma" << "DELTA"; + +QMap map; +foreach (const QString &str, list) + map.insert(str.toLower(), str); + +list = map.values(); +//! [14] + + +//! [15] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qStableSort(list.begin(), list.end()); +// list: [ 6, 12, 12, 33, 68 ] +//! [15] + + +//! [16] +bool caseInsensitiveLessThan(const QString &s1, const QString &s2) +{ + return s1.toLower() < s2.toLower(); +} + +int doSomething() +{ + QStringList list; + list << "AlPha" << "beTA" << "gamma" << "DELTA"; + qStableSort(list.begin(), list.end(), caseInsensitiveLessThan); + // list: [ "AlPha", "beTA", "DELTA", "gamma" ] +} +//! [16] + + +//! [17] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qStableSort(list.begin(), list.end(), qGreater()); +// list: [ 68, 33, 12, 12, 6 ] +//! [17] + + +//! [18] +QList list; +list << 3 << 3 << 6 << 6 << 6 << 8; + +QList::iterator i = qLowerBound(list.begin(), list.end(), 5); +list.insert(i, 5); +// list: [ 3, 3, 5, 6, 6, 6, 8 ] + +i = qLowerBound(list.begin(), list.end(), 12); +list.insert(i, 12); +// list: [ 3, 3, 5, 6, 6, 6, 8, 12 ] +//! [18] + + +//! [19] +QVector vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; +QVector::iterator begin6 = + qLowerBound(vect.begin(), vect.end(), 6); +QVector::iterator end6 = + qUpperBound(begin6, vect.end(), 6); + +QVector::iterator i = begin6; +while (i != end6) { + *i = 7; + ++i; +} +// vect: [ 3, 3, 7, 7, 7, 8 ] +//! [19] + + +//! [20] +QList list; +list << 3 << 3 << 6 << 6 << 6 << 8; + +QList::iterator i = qUpperBound(list.begin(), list.end(), 5); +list.insert(i, 5); +// list: [ 3, 3, 5, 6, 6, 6, 8 ] + +i = qUpperBound(list.begin(), list.end(), 12); +list.insert(i, 12); +// list: [ 3, 3, 5, 6, 6, 6, 8, 12 ] +//! [20] + + +//! [21] +QVector vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; +QVector::iterator begin6 = + qLowerBound(vect.begin(), vect.end(), 6); +QVector::iterator end6 = + qUpperBound(vect.begin(), vect.end(), 6); + +QVector::iterator i = begin6; +while (i != end6) { + *i = 7; + ++i; +} +// vect: [ 3, 3, 7, 7, 7, 8 ] +//! [21] + + +//! [22] +QVector vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; + +QVector::iterator i = + qBinaryFind(vect.begin(), vect.end(), 6); +// i == vect.begin() + 2 (or 3 or 4) +//! [22] + + +//! [23] +QList list; +list.append(new Employee("Blackpool", "Stephen")); +list.append(new Employee("Twist", "Oliver")); + +qDeleteAll(list.begin(), list.end()); +list.clear(); +//! [23] + + +//! [24] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qLess()); +// list: [ 6, 12, 12, 33, 68 ] +//! [24] + + +//! [25] +QList list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qGreater()); +// list: [ 68, 33, 12, 12, 6 ] +//! [25] diff --git a/src/corelib/doc/snippets/code/doc_src_qcache.cpp b/src/corelib/doc/snippets/code/doc_src_qcache.cpp new file mode 100644 index 0000000000..0ba9e65531 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qcache.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QCache cache; +//! [0] + + +//! [1] +Employee *employee = new Employee; +employee->setId(37); +employee->setName("Richard Schmit"); +... +cache.insert(employee->id(), employee); +//! [1] + + +//! [2] +QCache cache(5000); +//! [2] diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp new file mode 100644 index 0000000000..b96f9065cb --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp @@ -0,0 +1,420 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QList list; +... +QListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [0] + + +//! [1] +QListIterator i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [1] + + +//! [2] +QLinkedList list; +... +QLinkedListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [2] + + +//! [3] +QLinkedListIterator i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [3] + + +//! [4] +QVector vector; +... +QVectorIterator i(vector); +while (i.hasNext()) + qDebug() << i.next(); +//! [4] + + +//! [5] +QVectorIterator i(vector); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [5] + + +//! [6] +QSet set; +... +QSetIterator i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [6] + + +//! [7] +QSetIterator i(set); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [7] + + +//! [8] +QList list; +... +QMutableListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [8] + + +//! [9] +QMutableListIterator i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [9] + + +//! [10] +QMutableListIterator i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [10] + + +//! [11] +QLinkedList list; +... +QMutableLinkedListIterator i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [11] + + +//! [12] +QMutableLinkedListIterator i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [12] + + +//! [13] +QMutableLinkedListIterator i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [13] + + +//! [14] +QVector vector; +... +QMutableVectorIterator i(vector); +while (i.hasNext()) + qDebug() << i.next(); +//! [14] + + +//! [15] +QMutableVectorIterator i(vector); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [15] + + +//! [16] +QMutableVectorIterator i(vector); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [16] + + +//! [17] +QSet set; +... +QMutableSetIterator i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [17] + + +//! [18] +QMutableSetIterator i(set); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [18] + + +//! [19] +QMutableListIterator i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [19] + + +//! [20] +QMutableLinkedListIterator i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [20] + + +//! [21] +QMutableVectorIterator i(vector); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [21] + + +//! [22] +QMutableSetIterator i(set); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [22] + + +//! [23] +QMutableListIterator i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [23] + + +//! [24] +QMutableLinkedListIterator i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [24] + + +//! [25] +QMutableVectorIterator i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [25] + + +//! [26] +QMap map; +... +QMapIterator i(map); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [26] + + +//! [27] +QMapIterator i(map); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [27] + + +//! [28] +QMapIterator i(map); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [28] + + +//! [29] +QHash hash; +... +QHashIterator i(hash); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [29] + + +//! [30] +QHashIterator i(hash); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [30] + + +//! [31] +QHashIterator i(hash); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [31] + + +//! [32] +QMap map; +... +QMutableMapIterator i(map); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [32] + + +//! [33] +QMutableMapIterator i(map); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [33] + + +//! [34] +QMutableMapIterator i(map); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [34] + + +//! [35] +QMutableMapIterator i(map); +while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); +} +//! [35] + + +//! [36] +QHash hash; +... +QMutableHashIterator i(hash); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [36] + + +//! [37] +QMutableHashIterator i(hash); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [37] + + +//! [38] +QMutableHashIterator i(hash); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [38] + + +//! [39] +QMutableHashIterator i(hash); +while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); +} +//! [39] diff --git a/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp b/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp new file mode 100644 index 0000000000..ec80d011c9 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [1] +enum CustomEventPriority +{ + // An important event + ImportantEventPriority = Qt::HighEventPriority, + + // A more important event + MoreImportantEventPriority = ImportantEventPriority + 1, + + // A critical event + CriticalEventPriority = 100 * MoreImportantEventPriority, + + // Not that important + StatusEventPriority = Qt::LowEventPriority, + + // These are less important than Status events + IdleProcessingDoneEventPriority = StatusEventPriority - 1 +}; +//! [1] diff --git a/src/corelib/doc/snippets/code/doc_src_qnamespace.qdoc b/src/corelib/doc/snippets/code/doc_src_qnamespace.qdoc new file mode 100644 index 0000000000..e67b83a292 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qnamespace.qdoc @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QObject::connect: Cannot queue arguments of type 'MyType' +//! [0] diff --git a/src/corelib/doc/snippets/code/doc_src_qpair.cpp b/src/corelib/doc/snippets/code/doc_src_qpair.cpp new file mode 100644 index 0000000000..a29b6d02b2 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qpair.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QPair pair; +//! [0] + + +//! [1] +pair.first = "pi"; +pair.second = 3.14159265358979323846; +//! [1] + + +//! [2] +QList > list; +list.append(qMakePair(66, 3.14159)); +//! [2] diff --git a/src/corelib/doc/snippets/code/doc_src_qplugin.cpp b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp new file mode 100644 index 0000000000..1577b2fa61 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +namespace Foo +{ + struct MyInterface { ... }; +} + +Q_DECLARE_INTERFACE(Foo::MyInterface, "org.examples.MyInterface") +//! [0] + + +//! [1] +Q_EXPORT_PLUGIN2(pnp_extrafilters, ExtraFiltersPlugin) +//! [1] + + +//! [2] +Q_IMPORT_PLUGIN(qjpeg) +//! [2] + + +//! [3] +TEMPLATE = app +QTPLUGIN += qjpeg qgif # image formats +//! [3] diff --git a/src/corelib/doc/snippets/code/doc_src_qplugin.pro b/src/corelib/doc/snippets/code/doc_src_qplugin.pro new file mode 100644 index 0000000000..e5e5311e5d --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qplugin.pro @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#! [3] +TEMPLATE = app +QTPLUGIN += qjpeg qgif # image formats +#! [3] diff --git a/src/corelib/doc/snippets/code/doc_src_qset.cpp b/src/corelib/doc/snippets/code/doc_src_qset.cpp new file mode 100644 index 0000000000..cfd366f639 --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qset.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QSet set; +//! [0] + + +//! [1] +set.insert("one"); +set.insert("three"); +set.insert("seven"); +//! [1] + + +//! [2] +set << "twelve" << "fifteen" << "nineteen"; +//! [2] + + +//! [3] +if (!set.contains("ninety-nine")) + ... +//! [3] + + +//! [4] +QSetIterator i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [4] + + +//! [5] +QSet::const_iterator i = set.constBegin(); +while (i != set.constEnd()) { + qDebug() << *i; + ++i; +} +//! [5] + + +//! [6] +QSet set; +... +foreach (const QString &value, set) + qDebug() << value; +//! [6] + + +//! [7] +QSet set; +set.reserve(20000); +for (int i = 0; i < 20000; ++i) + set.insert(values[i]); +//! [7] + + +//! [8] +QSet set; +set << "January" << "February" << ... << "December"; + +QSet::iterator i; +for (i = set.begin(); i != set.end(); ++i) + qDebug() << *i; +//! [8] + + +//! [9] +QSet set; +set << "January" << "February" << ... << "December"; + +QSet::iterator i = set.begin(); +while (i != set.end()) { + if ((*i).startsWith('J')) { + i = set.erase(i); + } else { + ++i; + } +} +//! [9] + + +//! [10] +QSet set; +... +QSet::iterator it = qFind(set.begin(), set.end(), "Jeanette"); +if (it != set.end()) + cout << "Found Jeanette" << endl; +//! [10] + + +//! [11] +QSet set; +set << "January" << "February" << ... << "December"; + +QSet::const_iterator i; +for (i = set.begin(); i != set.end(); ++i) + qDebug() << *i; +//! [11] + + +//! [12] +QSet set; +... +QSet::iterator it = qFind(set.begin(), set.end(), "Jeanette"); +if (it != set.constEnd()) + cout << "Found Jeanette" << endl; +//! [12] + + +//! [13] +QSet set; +set << "red" << "green" << "blue" << ... << "black"; + +QList list = set.toList(); +qSort(list); +//! [13] + + +//! [14] +QStringList list; +list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; + +QSet set = QSet::fromList(list); +set.contains("Julia"); // returns true +set.contains("Mike"); // returns true +set.size(); // returns 2 +//! [14] diff --git a/src/corelib/doc/snippets/code/doc_src_qtcore.cpp b/src/corelib/doc/snippets/code/doc_src_qtcore.cpp new file mode 100644 index 0000000000..f00c96c6df --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qtcore.cpp @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include +//! [0] diff --git a/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp b/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp new file mode 100644 index 0000000000..027faa41dc --- /dev/null +++ b/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +int myfunc(int n) +{ + int table[n + 1]; // WRONG + ... + return table[n]; +} +//! [0] + + +//! [1] +int myfunc(int n) +{ + int *table = new int[n + 1]; + ... + int ret = table[n]; + delete[] table; + return ret; +} +//! [1] + + +//! [2] +int myfunc(int n) +{ + QVarLengthArray array(n + 1); + ... + return array[n]; +} +//! [2] + + +//! [3] +QVarLengthArray array(10); +int *data = array.data(); +for (int i = 0; i < 10; ++i) + data[i] = 2 * i; +//! [3] diff --git a/src/corelib/doc/snippets/code/qlogging/qlogging.cpp b/src/corelib/doc/snippets/code/qlogging/qlogging.cpp new file mode 100644 index 0000000000..74892cdfba --- /dev/null +++ b/src/corelib/doc/snippets/code/qlogging/qlogging.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +//! [1] + void statusChanged(QDeclarativeComponent::Status status) { + if (status == QDeclarativeComponent::Error) { + foreach (const QDeclarativeError &error, component->errors()) { + const QByteArray file = error.url().toEncoded(); + QMessageLogger(file.constData(), error.line(), 0).debug() << error.description(); + } + } + } +//! [1] diff --git a/src/corelib/doc/snippets/code/src_corelib_codecs_qtextcodec.cpp b/src/corelib/doc/snippets/code/src_corelib_codecs_qtextcodec.cpp new file mode 100644 index 0000000000..78abd5ba28 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_codecs_qtextcodec.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QByteArray encodedString = "..."; +QTextCodec *codec = QTextCodec::codecForName("KOI8-R"); +QString string = codec->toUnicode(encodedString); +//! [0] + + +//! [1] +QString string = "..."; +QTextCodec *codec = QTextCodec::codecForName("KOI8-R"); +QByteArray encodedString = codec->fromUnicode(string); +//! [1] + + +//! [2] +QTextCodec *codec = QTextCodec::codecForName("Shift-JIS"); +QTextDecoder *decoder = codec->makeDecoder(); + +QString string; +while (new_data_available()) { + QByteArray chunk = get_new_data(); + string += decoder->toUnicode(chunk); +} +delete decoder; +//! [2] + + +//! [3] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QTextCodec::setCodecForTr(QTextCodec::codecForName("eucKR")); + ... +} +//! [3] diff --git a/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp b/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp new file mode 100644 index 0000000000..a8f9da3a7c --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class HelloWorldTask : public QRunnable +{ + void run() + { + qDebug() << "Hello world from thread" << QThread::currentThread(); + } +} + +HelloWorldTask *hello = new HelloWorldTask(); +// QThreadPool takes ownership and deletes 'hello' automatically +QThreadPool::globalInstance()->start(hello); +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp new file mode 100644 index 0000000000..16f6783a52 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -0,0 +1,585 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +label->setAlignment(Qt::AlignLeft | Qt::AlignTop); +//! [0] + + +//! [1] +class MyClass +{ +public: + enum Option { + NoOptions = 0x0, + ShowTabs = 0x1, + ShowAll = 0x2, + SqueezeBlank = 0x4 + }; + Q_DECLARE_FLAGS(Options, Option) + ... +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options) +//! [1] + +//! [meta-object flags] +Q_FLAGS(Options) +//! [meta-object flags] + +//! [2] +typedef QFlags Flags; +//! [2] + + +//! [3] +int myValue = 10; +int minValue = 2; +int maxValue = 6; + +int boundedValue = qBound(minValue, myValue, maxValue); +// boundedValue == 6 +//! [3] + + +//! [4] +if (!driver()->isOpen() || driver()->isOpenError()) { + qWarning("QSqlQuery::exec: database not open"); + return false; +} +//! [4] + + +//! [5] +qint64 value = Q_INT64_C(932838457459459); +//! [5] + + +//! [6] +quint64 value = Q_UINT64_C(932838457459459); +//! [6] + + +//! [7] +void myMsgHandler(QtMsgType, const char *); +//! [7] + + +//! [8] +qint64 value = Q_INT64_C(932838457459459); +//! [8] + + +//! [9] +quint64 value = Q_UINT64_C(932838457459459); +//! [9] + + +//! [10] +int absoluteValue; +int myValue = -4; + +absoluteValue = qAbs(myValue); +// absoluteValue == 4 +//! [10] + + +//! [11] +qreal valueA = 2.3; +qreal valueB = 2.7; + +int roundedValueA = qRound(valueA); +// roundedValueA = 2 +int roundedValueB = qRound(valueB); +// roundedValueB = 3 +//! [11] + + +//! [12] +qreal valueA = 42949672960.3; +qreal valueB = 42949672960.7; + +int roundedValueA = qRound(valueA); +// roundedValueA = 42949672960 +int roundedValueB = qRound(valueB); +// roundedValueB = 42949672961 +//! [12] + + +//! [13] +int myValue = 6; +int yourValue = 4; + +int minValue = qMin(myValue, yourValue); +// minValue == yourValue +//! [13] + + +//! [14] +int myValue = 6; +int yourValue = 4; + +int maxValue = qMax(myValue, yourValue); +// maxValue == myValue +//! [14] + + +//! [15] +int myValue = 10; +int minValue = 2; +int maxValue = 6; + +int boundedValue = qBound(minValue, myValue, maxValue); +// boundedValue == 6 +//! [15] + + +//! [16] +#if QT_VERSION >= 0x040100 + QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon); +#else + QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon); + QIcon icon(pixmap); +#endif +//! [16] + + +//! [17] +// File: div.cpp + +#include + +int divide(int a, int b) +{ + Q_ASSERT(b != 0); + return a / b; +} +//! [17] + + +//! [18] +ASSERT: "b == 0" in file div.cpp, line 7 +//! [18] + + +//! [19] +// File: div.cpp + +#include + +int divide(int a, int b) +{ + Q_ASSERT_X(b != 0, "divide", "division by zero"); + return a / b; +} +//! [19] + + +//! [20] +ASSERT failure in divide: "division by zero", file div.cpp, line 7 +//! [20] + + +//! [21] +int *a; + +Q_CHECK_PTR(a = new int[80]); // WRONG! + +a = new (nothrow) int[80]; // Right +Q_CHECK_PTR(a); +//! [21] + + +//! [22] +template +const TInputType &myMin(const TInputType &value1, const TInputType &value2) +{ + qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2; + + if(value1 < value2) + return value1; + else + return value2; +} +//! [22] + + +//! [23] +#include +#include +#include + +void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const char *msg) +{ + switch (type) { + case QtDebugMsg: + fprintf(stderr, "Debug: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); + break; + case QtWarningMsg: + fprintf(stderr, "Warning: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); + break; + case QtCriticalMsg: + fprintf(stderr, "Critical: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); + break; + case QtFatalMsg: + fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); + abort(); + } +} + +int main(int argc, char **argv) +{ + qInstallMessageHandler(myMessageOutput); + QApplication app(argc, argv); + ... + return app.exec(); +} +//! [23] + + +//! [24] +qDebug("Items in list: %d", myList.size()); +//! [24] + + +//! [25] +qDebug() << "Brush:" << myQBrush << "Other value:" << i; +//! [25] + + +//! [26] +void f(int c) +{ + if (c > 200) + qWarning("f: bad argument, c == %d", c); +} +//! [26] + + +//! [27] +qWarning() << "Brush:" << myQBrush << "Other value:" +<< i; +//! [27] + + +//! [28] +void load(const QString &fileName) +{ + QFile file(fileName); + if (!file.exists()) + qCritical("File '%s' does not exist!", qPrintable(fileName)); +} +//! [28] + + +//! [29] +qCritical() << "Brush:" << myQBrush << "Other +value:" << i; +//! [29] + + +//! [30] +int divide(int a, int b) +{ + if (b == 0) // program error + qFatal("divide: cannot divide by zero"); + return a / b; +} +//! [30] + + +//! [31] +forever { + ... +} +//! [31] + + +//! [32] +CONFIG += no_keywords +//! [32] + + +//! [33] +CONFIG += no_keywords +//! [33] + + +//! [34] +QString FriendlyConversation::greeting(int type) +{ +static const char *greeting_strings[] = { + QT_TR_NOOP("Hello"), + QT_TR_NOOP("Goodbye") +}; +return tr(greeting_strings[type]); +} +//! [34] + + +//! [35] +static const char *greeting_strings[] = { + QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"), + QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye") +}; + +QString FriendlyConversation::greeting(int type) +{ + return tr(greeting_strings[type]); +} + +QString global_greeting(int type) +{ + return qApp->translate("FriendlyConversation", + greeting_strings[type]); +} +//! [35] + + +//! [36] + +static { const char *source; const char *comment; } greeting_strings[] = +{ + QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello", + "A really friendly hello"), + QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye", + "A really friendly goodbye") +}; + +QString FriendlyConversation::greeting(int type) +{ + return tr(greeting_strings[type].source, + greeting_strings[type].comment); +} + +QString global_greeting(int type) +{ + return qApp->translate("FriendlyConversation", + greeting_strings[type].source, + greeting_strings[type].comment); +} +//! [36] + + +//! [qttrid] + //% "%n fooish bar(s) found.\n" + //% "Do you want to continue?" + QString text = qtTrId("qtn_foo_bar", n); +//! [qttrid] + + +//! [qttrid_noop] +static const char * const ids[] = { + //% "This is the first text." + QT_TRID_NOOP("qtn_1st_text"), + //% "This is the second text." + QT_TRID_NOOP("qtn_2nd_text"), + 0 +}; + +void TheClass::addLabels() +{ + for (int i = 0; ids[i]; ++i) + new QLabel(qtTrId(ids[i]), this); +} +//! [qttrid_noop] + + +//! [37] +qWarning("%s: %s", qPrintable(key), qPrintable(value)); +//! [37] + + +//! [38] +struct Point2D +{ + int x; + int y; +}; + +Q_DECLARE_TYPEINFO(Point2D, Q_PRIMITIVE_TYPE); +//! [38] + + +//! [39] +class Point2D +{ +public: + Point2D() { data = new int[2]; } + Point2D(const Point2D &other) { ... } + ~Point2D() { delete[] data; } + + Point2D &operator=(const Point2D &other) { ... } + + int x() const { return data[0]; } + int y() const { return data[1]; } + +private: + int *data; +}; + +Q_DECLARE_TYPEINFO(Point2D, Q_MOVABLE_TYPE); +//! [39] + + +//! [40] +#if Q_BYTE_ORDER == Q_BIG_ENDIAN +... +#endif + +or + +#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN +... +#endif + +//! [40] + + +//! [41] + +#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN +... +#endif + +//! [41] + + +//! [42] +#if Q_BYTE_ORDER == Q_BIG_ENDIAN +... +#endif + +//! [42] + +//! [begin namespace macro] +namespace QT_NAMESPACE { +//! [begin namespace macro] + +//! [end namespace macro] +} +//! [end namespace macro] + +//! [43] +class MyClass : public QObject +{ + + private: + Q_DISABLE_COPY(MyClass) +}; + +//! [43] + +//! [44] +class MyClass : public QObject +{ + + private: + MyClass(const MyClass &); + MyClass &operator=(const MyClass &); +}; +//! [44] + +//! [45] + QWidget w = QWidget(); +//! [45] + +//! [46] + // Instead of comparing with 0.0 + qFuzzyCompare(0.0,1.0e-200); // This will return false + // Compare adding 1 to both values will fix the problem + qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true +//! [46] + +//! [47] +CApaApplication *myApplicationFactory(); +//! [47] + + +//! [49] +void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *); +//! [49] + +//! [qlikely] + // the condition inside the "if" will be successful most of the times + for (int i = 1; i <= 365; i++) { + if (Q_LIKELY(isWorkingDay(i))) { + ... + } + ... + } +//! [qlikely] + +//! [qunlikely] +bool readConfiguration(const QFile &file) +{ + // We expect to be asked to read an existing file + if (Q_UNLIKELY(!file.exists())) { + qWarning() << "File not found"; + return false; + } + + ... + return true; +} +//! [qunlikely] + +//! [qunreachable-enum] + enum Shapes { + Rectangle, + Triangle, + Circle, + NumShapes + }; +//! [qunreachable-enum] + +//! [qunreachable-switch] + switch (shape) { + case Rectangle: + return rectangle(); + case Triangle: + return triangle(); + case Circle: + return circle(); + case NumShapes: + Q_UNREACHABLE(); + break; + } +//! [qunreachable-switch] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qabstractfileengine.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qabstractfileengine.cpp new file mode 100644 index 0000000000..8893fbd08c --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qabstractfileengine.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class ZipEngineHandler : public QAbstractFileEngineHandler +{ +public: + QAbstractFileEngine *create(const QString &fileName) const; +}; + +QAbstractFileEngine *ZipEngineHandler::create(const QString &fileName) const +{ + // ZipEngineHandler returns a ZipEngine for all .zip files + return fileName.toLower().endsWith(".zip") ? new ZipEngine(fileName) : 0; +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + ZipEngineHandler engine; + + MainWindow window; + window.show(); + + return app.exec(); +} +//! [0] + + +//! [1] +QAbstractSocketEngine *ZipEngineHandler::create(const QString &fileName) const +{ + // ZipEngineHandler returns a ZipEngine for all .zip files + return fileName.toLower().endsWith(".zip") ? new ZipEngine(fileName) : 0; +} +//! [1] + + +//! [2] +QAbstractFileEngineIterator * +CustomFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames) +{ + return new CustomFileEngineIterator(filters, filterNames); +} +//! [2] + + +//! [3] +class CustomIterator : public QAbstractFileEngineIterator +{ +public: + CustomIterator(const QStringList &nameFilters, QDir::Filters filters) + : QAbstractFileEngineIterator(nameFilters, filters), index(0) + { + // In a real iterator, these entries are fetched from the + // file system based on the value of path(). + entries << "entry1" << "entry2" << "entry3"; + } + + bool hasNext() const + { + return index < entries.size() - 1; + } + + QString next() + { + if (!hasNext()) + return QString(); + ++index; + return currentFilePath(); + } + + QString currentFileName() + { + return entries.at(index); + } + +private: + QStringList entries; + int index; +}; +//! [3] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qdatastream.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qdatastream.cpp new file mode 100644 index 0000000000..af2d7d24bf --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qdatastream.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +void wrapInFunction() +{ + +//! [0] +QFile file("file.dat"); +file.open(QIODevice::WriteOnly); +QDataStream out(&file); // we will serialize the data into the file +out << QString("the answer is"); // serialize a string +out << (qint32)42; // serialize an integer +//! [0] + + +//! [1] +QFile file("file.dat"); +file.open(QIODevice::ReadOnly); +QDataStream in(&file); // read the data serialized from the file +QString str; +qint32 a; +in >> str >> a; // extract "the answer is" and 42 +//! [1] + + +//! [2] +stream.setVersion(QDataStream::Qt_4_0); +//! [2] + + +//! [3] +QFile file("file.xxx"); +file.open(QIODevice::WriteOnly); +QDataStream out(&file); + +// Write a header with a "magic number" and a version +out << (quint32)0xA0B0C0D0; +out << (qint32)123; + +out.setVersion(QDataStream::Qt_4_0); + +// Write the data +out << lots_of_interesting_data; +//! [3] + + +//! [4] +QFile file("file.xxx"); +file.open(QIODevice::ReadOnly); +QDataStream in(&file); + +// Read and check the header +quint32 magic; +in >> magic; +if (magic != 0xA0B0C0D0) + return XXX_BAD_FILE_FORMAT; + +// Read the version +qint32 version; +in >> version; +if (version < 100) + return XXX_BAD_FILE_TOO_OLD; +if (version > 123) + return XXX_BAD_FILE_TOO_NEW; + +if (version <= 110) + in.setVersion(QDataStream::Qt_3_2); +else + in.setVersion(QDataStream::Qt_4_0); + +// Read the data +in >> lots_of_interesting_data; +if (version >= 120) + in >> data_new_in_XXX_version_1_2; +in >> other_interesting_data; +//! [4] + + +//! [5] +QDataStream out(file); +out.setVersion(QDataStream::Qt_4_0); +//! [5] + +} diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qdir.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qdir.cpp new file mode 100644 index 0000000000..3c333e5642 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qdir.cpp @@ -0,0 +1,175 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +void wrapInFunction() +{ + +//! [0] +QDir("/home/user/Documents") +QDir("C:/Documents and Settings") +//! [0] + + +//! [1] +QDir("images/landscape.png") +//! [1] + + +//! [2] +QDir("Documents/Letters/Applications").dirName() // "Applications" +QDir().dirName() // "." +//! [2] + + +//! [3] +QDir directory("Documents/Letters"); +QString path = directory.filePath("contents.txt"); +QString absolutePath = directory.absoluteFilePath("contents.txt"); +//! [3] + + +//! [4] +QDir dir("example"); +if (!dir.exists()) + qWarning("Cannot find the example directory"); +//! [4] + + +//! [5] +QDir dir = QDir::root(); // "/" +if (!dir.cd("tmp")) { // "/tmp" + qWarning("Cannot find the \"/tmp\" directory"); +} else { + QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt" + if (!file.open(QIODevice::ReadWrite)) + qWarning("Cannot create the file %s", file.name()); +} +//! [5] + + +//! [6] +QString bin = "/local/bin"; // where /local/bin is a symlink to /usr/bin +QDir binDir(bin); +QString canonicalBin = binDir.canonicalPath(); +// canonicalBin now equals "/usr/bin" + +QString ls = "/local/bin/ls"; // where ls is the executable "ls" +QDir lsDir(ls); +QString canonicalLs = lsDir.canonicalPath(); +// canonicalLS now equals "/usr/bin/ls". +//! [6] + + +//! [7] +QDir dir("/home/bob"); +QString s; + +s = dir.relativeFilePath("images/file.jpg"); // s is "images/file.jpg" +s = dir.relativeFilePath("/home/mary/file.txt"); // s is "../mary/file.txt" +//! [7] + + +//! [8] +QDir::setSearchPaths("icons", QStringList(QDir::homePath() + "/images")); +QDir::setSearchPaths("docs", QStringList(":/embeddedDocuments")); +... +QPixmap pixmap("icons:undo.png"); // will look for undo.png in QDir::homePath() + "/images" +QFile file("docs:design.odf"); // will look in the :/embeddedDocuments resource path +//! [8] + + +//! [9] +QDir dir("/tmp/root_link"); +dir = dir.canonicalPath(); +if (dir.isRoot()) + qWarning("It is a root link"); +//! [9] + + +//! [10] +// The current directory is "/usr/local" +QDir d1("/usr/local/bin"); +QDir d2("bin"); +if (d1 == d2) + qDebug("They're the same"); +//! [10] + + +//! [11] +// The current directory is "/usr/local" +QDir d1("/usr/local/bin"); +d1.setFilter(QDir::Executable); +QDir d2("bin"); +if (d1 != d2) + qDebug("They differ"); +//! [11] + + +//! [12] +C:/Documents and Settings/Username +//! [12] + + +//! [13] +Q_INIT_RESOURCE(myapp); +//! [13] + + +//! [14] +inline void initMyResource() { Q_INIT_RESOURCE(myapp); } + +namespace MyNamespace +{ + ... + + void myFunction() + { + initMyResource(); + } +} +//! [14] + + +//! [15] +Q_CLEANUP_RESOURCE(myapp); +//! [15] + +} diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qdiriterator.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qdiriterator.cpp new file mode 100644 index 0000000000..bf38bb1627 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qdiriterator.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QDirIterator it("/etc", QDirIterator::Subdirectories); +while (it.hasNext()) { + qDebug() << it.next(); + + // /etc/. + // /etc/.. + // /etc/X11 + // /etc/X11/fs + // ... +} +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qfile.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qfile.cpp new file mode 100644 index 0000000000..7ad24b30f1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qfile.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QFile file; +QDir::setCurrent("/tmp"); +file.setFileName("readme.txt"); +QDir::setCurrent("/home"); +file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix +//! [0] + + +//! [1] +QByteArray myEncoderFunc(const QString &fileName); +//! [1] + + +//! [2] +QString myDecoderFunc(const QByteArray &localFileName); +//! [2] + + +//! [3] +#include + +void printError(const char* msg) +{ + QFile file; + file.open(stderr, QIODevice::WriteOnly); + file.write(msg, qstrlen(msg)); // write to stderr + file.close(); +} +//! [3] + + +//! [4] +CONFIG += console +//! [4] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qfileinfo.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qfileinfo.cpp new file mode 100644 index 0000000000..f565786934 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qfileinfo.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +//![newstuff] + QFileInfo fi("c:/temp/foo"); => fi.absoluteFilePath() => "C:/temp/foo" +//![newstuff] + +//! [0] +#ifdef Q_OS_UNIX + +QFileInfo info1("/home/bob/bin/untabify"); +info1.isSymLink(); // returns true +info1.absoluteFilePath(); // returns "/home/bob/bin/untabify" +info1.size(); // returns 56201 +info1.symLinkTarget(); // returns "/opt/pretty++/bin/untabify" + +QFileInfo info2(info1.symLinkTarget()); +info2.isSymLink(); // returns false +info2.absoluteFilePath(); // returns "/opt/pretty++/bin/untabify" +info2.size(); // returns 56201 + +#endif +//! [0] + + +//! [1] +#ifdef Q_OS_WIN + +QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk"); +info1.isSymLink(); // returns true +info1.absoluteFilePath(); // returns "C:/Documents and Settings/Bob/untabify.lnk" +info1.size(); // returns 743 +info1.symLinkTarget(); // returns "C:/Pretty++/untabify" + +QFileInfo info2(info1.symLinkTarget()); +info2.isSymLink(); // returns false +info2.absoluteFilePath(); // returns "C:/Pretty++/untabify" +info2.size(); // returns 63942 + +#endif +//! [1] + + +//! [2] +QString absolute = "/local/bin"; +QString relative = "local/bin"; +QFileInfo absFile(absolute); +QFileInfo relFile(relative); + +QDir::setCurrent(QDir::rootPath()); +// absFile and relFile now point to the same file + +QDir::setCurrent("/tmp"); +// absFile now points to "/local/bin", +// while relFile points to "/tmp/local/bin" +//! [2] + + +//! [3] +QFileInfo fi("/tmp/archive.tar.gz"); +QString name = fi.fileName(); // name = "archive.tar.gz" +//! [3] + + +//! [4] +QFileInfo fi("/Applications/Safari.app"); +QString bundle = fi.bundleName(); // name = "Safari" +//! [4] + + +//! [5] +QFileInfo fi("/tmp/archive.tar.gz"); +QString base = fi.baseName(); // base = "archive" +//! [5] + + +//! [6] +QFileInfo fi("/tmp/archive.tar.gz"); +QString base = fi.completeBaseName(); // base = "archive.tar" +//! [6] + + +//! [7] +QFileInfo fi("/tmp/archive.tar.gz"); +QString ext = fi.completeSuffix(); // ext = "tar.gz" +//! [7] + + +//! [8] +QFileInfo fi("/tmp/archive.tar.gz"); +QString ext = fi.suffix(); // ext = "gz" +//! [8] + + +//! [9] +QFileInfo info(fileName); +if (info.isSymLink()) + fileName = info.symLinkTarget(); +//! [9] + + +//! [10] +QFileInfo fi("/tmp/archive.tar.gz"); +if (fi.permission(QFile::WriteUser | QFile::ReadGroup)) + qWarning("I can change the file; my group can read the file"); +if (fi.permission(QFile::WriteGroup | QFile::WriteOther)) + qWarning("The group or others can change the file"); +//! [10] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qiodevice.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qiodevice.cpp new file mode 100644 index 0000000000..6018a21f89 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qiodevice.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QProcess gzip; +gzip.start("gzip", QStringList() << "-c"); +if (!gzip.waitForStarted()) + return false; + +gzip.write("uncompressed data"); + +QByteArray compressed; +while (gzip.waitForReadyRead()) + compressed += gzip.readAll(); +//! [0] + + +//! [1] +qint64 CustomDevice::bytesAvailable() const +{ + return buffer.size() + QIODevice::bytesAvailable(); +} +//! [1] + + +//! [2] +QFile file("box.txt"); +if (file.open(QFile::ReadOnly)) { + char buf[1024]; + qint64 lineLength = file.readLine(buf, sizeof(buf)); + if (lineLength != -1) { + // the line is available in buf + } +} +//! [2] + + +//! [3] +bool CustomDevice::canReadLine() const +{ + return buffer.contains('\n') || QIODevice::canReadLine(); +} +//! [3] + + +//! [4] +bool isExeFile(QFile *file) +{ + char buf[2]; + if (file->peek(buf, sizeof(buf)) == sizeof(buf)) + return (buf[0] == 'M' && buf[1] == 'Z'); + return false; +} +//! [4] + + +//! [5] +bool isExeFile(QFile *file) +{ + return file->peek(2) == "MZ"; +} +//! [5] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp new file mode 100644 index 0000000000..16878f41e1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +void wrapInFunction() +{ + +//! [0] +QProcess builder; +builder.setProcessChannelMode(QProcess::MergedChannels); +builder.start("make", QStringList() << "-j2"); + +if (!builder.waitForFinished()) + qDebug() << "Make failed:" << builder.errorString(); +else + qDebug() << "Make output:" << builder.readAll(); +//! [0] + + +//! [1] +QProcess more; +more.start("more"); +more.write("Text to display"); +more.closeWriteChannel(); +// QProcess will emit readyRead() once "more" starts printing +//! [1] + + +//! [2] +command1 | command2 +//! [2] + + +//! [3] +QProcess process1; +QProcess process2; + +process1.setStandardOutputProcess(&process2); + +process1.start("command1"); +process2.start("command2"); +//! [3] + + +//! [4] +class SandboxProcess : public QProcess +{ + ... + protected: + void setupChildProcess(); + ... +}; + +void SandboxProcess::setupChildProcess() +{ + // Drop all privileges in the child process, and enter + // a chroot jail. +#if defined Q_OS_UNIX + ::setgroups(0, 0); + ::chroot("/etc/safe"); + ::chdir("/"); + ::setgid(safeGid); + ::setuid(safeUid); + ::umask(0); +#endif +} + +//! [4] + + +//! [5] +QProcess process; +process.start("del /s *.txt"); +// same as process.start("del", QStringList() << "/s" << "*.txt"); +... +//! [5] + + +//! [6] +QProcess process; +process.start("dir \"My Documents\""); +//! [6] + + +//! [7] +QProcess process; +process.start("dir \"\"\"My Documents\"\"\""); +//! [7] + + +//! [8] +QStringList environment = QProcess::systemEnvironment(); +// environment = {"PATH=/usr/bin:/usr/local/bin", +// "USER=greg", "HOME=/home/greg"} +//! [8] + +} diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qsettings.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qsettings.cpp new file mode 100644 index 0000000000..c9ce7561ff --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qsettings.cpp @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QSettings settings("MySoft", "Star Runner"); +QColor color = settings.value("DataPump/bgcolor").value(); +//! [0] + + +//! [1] +QSettings settings("MySoft", "Star Runner"); +QColor color = palette().background().color(); +settings.setValue("DataPump/bgcolor", color); +//! [1] + + +//! [2] +QSettings settings("/home/petra/misc/myapp.ini", + QSettings::IniFormat); +//! [2] + + +//! [3] +QSettings settings("/Users/petra/misc/myapp.plist", + QSettings::NativeFormat); +//! [3] + + +//! [4] +QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office", + QSettings::NativeFormat); +//! [4] + + +//! [5] +settings.setValue("11.0/Outlook/Security/DontTrustInstalledFiles", 0); +//! [5] + + +//! [6] +settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway"); +settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar"); +settings.value("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default"); // returns "Milkyway" +//! [6] + + +//! [7] +#ifdef Q_WS_MAC + QSettings settings("grenoullelogique.fr", "Squash"); +#else + QSettings settings("Grenoulle Logique", "Squash"); +#endif +//! [7] + + +//! [8] +pos = @Point(100 100) +//! [8] + + +//! [9] +windir = C:\Windows +//! [9] + + +//! [10] +QSettings settings("Moose Tech", "Facturo-Pro"); +//! [10] + + +//! [11] +QSettings settings("Moose Soft", "Facturo-Pro"); +//! [11] + + +//! [12] +QCoreApplication::setOrganizationName("Moose Soft"); +QCoreApplication::setApplicationName("Facturo-Pro"); +QSettings settings; +//! [12] + + +//! [13] +settings.beginGroup("mainwindow"); +settings.setValue("size", win->size()); +settings.setValue("fullScreen", win->isFullScreen()); +settings.endGroup(); + +settings.beginGroup("outputpanel"); +settings.setValue("visible", panel->isVisible()); +settings.endGroup(); +//! [13] + + +//! [14] +settings.beginGroup("alpha"); +// settings.group() == "alpha" + +settings.beginGroup("beta"); +// settings.group() == "alpha/beta" + +settings.endGroup(); +// settings.group() == "alpha" + +settings.endGroup(); +// settings.group() == "" +//! [14] + + +//! [15] +struct Login { + QString userName; + QString password; +}; +QList logins; +... + +QSettings settings; +int size = settings.beginReadArray("logins"); +for (int i = 0; i < size; ++i) { + settings.setArrayIndex(i); + Login login; + login.userName = settings.value("userName").toString(); + login.password = settings.value("password").toString(); + logins.append(login); +} +settings.endArray(); +//! [15] + + +//! [16] +struct Login { + QString userName; + QString password; +}; +QList logins; +... + +QSettings settings; +settings.beginWriteArray("logins"); +for (int i = 0; i < logins.size(); ++i) { + settings.setArrayIndex(i); + settings.setValue("userName", list.at(i).userName); + settings.setValue("password", list.at(i).password); +} +settings.endArray(); +//! [16] + + +//! [17] +QSettings settings; +settings.setValue("fridge/color", Qt::white); +settings.setValue("fridge/size", QSize(32, 96)); +settings.setValue("sofa", true); +settings.setValue("tv", false); + +QStringList keys = settings.allKeys(); +// keys: ["fridge/color", "fridge/size", "sofa", "tv"] +//! [17] + + +//! [18] +settings.beginGroup("fridge"); +keys = settings.allKeys(); +// keys: ["color", "size"] +//! [18] + + +//! [19] +QSettings settings; +settings.setValue("fridge/color", Qt::white); +settings.setValue("fridge/size", QSize(32, 96)); +settings.setValue("sofa", true); +settings.setValue("tv", false); + +QStringList keys = settings.childKeys(); +// keys: ["sofa", "tv"] +//! [19] + + +//! [20] +settings.beginGroup("fridge"); +keys = settings.childKeys(); +// keys: ["color", "size"] +//! [20] + + +//! [21] +QSettings settings; +settings.setValue("fridge/color", Qt::white); +settings.setValue("fridge/size", QSize(32, 96)); +settings.setValue("sofa", true); +settings.setValue("tv", false); + +QStringList groups = settings.childGroups(); +// groups: ["fridge"] +//! [21] + + +//! [22] +settings.beginGroup("fridge"); +groups = settings.childGroups(); +// groups: [] +//! [22] + + +//! [23] +QSettings settings; +settings.setValue("interval", 30); +settings.value("interval").toInt(); // returns 30 + +settings.setValue("interval", 6.55); +settings.value("interval").toDouble(); // returns 6.55 +//! [23] + + +//! [24] +QSettings settings; +settings.setValue("ape"); +settings.setValue("monkey", 1); +settings.setValue("monkey/sea", 2); +settings.setValue("monkey/doe", 4); + +settings.remove("monkey"); +QStringList keys = settings.allKeys(); +// keys: ["ape"] +//! [24] + + +//! [25] +QSettings settings; +settings.setValue("ape"); +settings.setValue("monkey", 1); +settings.setValue("monkey/sea", 2); +settings.setValue("monkey/doe", 4); + +settings.beginGroup("monkey"); +settings.remove(""); +settings.endGroup(); + +QStringList keys = settings.allKeys(); +// keys: ["ape"] +//! [25] + + +//! [26] +QSettings settings; +settings.setValue("animal/snake", 58); +settings.value("animal/snake", 1024).toInt(); // returns 58 +settings.value("animal/zebra", 1024).toInt(); // returns 1024 +settings.value("animal/zebra").toInt(); // returns 0 +//! [26] + + +//! [27] +bool myReadFunc(QIODevice &device, QSettings::SettingsMap &map); +//! [27] + + +//! [28] +bool myWriteFunc(QIODevice &device, const QSettings::SettingsMap &map); +//! [28] + + +//! [29] +bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map); +bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map); + +int main(int argc, char *argv[]) +{ + const QSettings::Format XmlFormat = + QSettings::registerFormat("xml", readXmlFile, writeXmlFile); + + QSettings settings(XmlFormat, QSettings::UserScope, "MySoft", + "Star Runner"); + + ... +} +//! [29] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qtemporarydir.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qtemporarydir.cpp new file mode 100644 index 0000000000..4e855f72f0 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qtemporarydir.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +{ +//! [0] + // Within a function/method... + + QTemporaryDir dir; + if (dir.isValid()) { + // dir.path() returns the unique directory path + } + + // The QTemporaryDir destructor removes the temporary directory + // as it goes out of scope. +//! [0] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qtemporaryfile.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qtemporaryfile.cpp new file mode 100644 index 0000000000..2db3aace56 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qtemporaryfile.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +{ +//! [0] + // Within a function/method... + + QTemporaryFile file; + if (file.open()) { + // file.fileName() returns the unique file name + } + + // The QTemporaryFile destructor removes the temporary file + // as it goes out of scope. +//! [0] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qtextstream.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qtextstream.cpp new file mode 100644 index 0000000000..c46cbb23d3 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qtextstream.cpp @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QFile data("output.txt"); +if (data.open(QFile::WriteOnly | QFile::Truncate)) { + QTextStream out(&data); + out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7; + // writes "Result: 3.14 2.7 " +} +//! [0] + + +//! [1] +QTextStream stream(stdin); +QString line; +do { + line = stream.readLine(); +} while (!line.isNull()); +//! [1] + + +//! [2] +QTextStream in("0x50 0x20"); +int firstNumber, secondNumber; + +in >> firstNumber; // firstNumber == 80 +in >> dec >> secondNumber; // secondNumber == 0 + +char ch; +in >> ch; // ch == 'x' +//! [2] + + +//! [3] +int main(int argc, char *argv[]) +{ + // read numeric arguments (123, 0x20, 4.5...) + for (int i = 1; i < argc; ++i) { + int number; + QTextStream in(argv[i]); + in >> number; + ... + } +} +//! [3] + + +//! [4] +QString str; +QTextStream in(stdin); +in >> str; +//! [4] + + +//! [5] +QString s; +QTextStream out(&s); +out.setFieldWidth(10); +out.setFieldAlignment(QTextStream::AlignCenter); +out.setPadChar('-'); +out << "Qt" << "rocks!"; +//! [5] + + +//! [6] +----Qt------rocks!-- +//! [6] + + +//! [7] +QTextStream in(file); +QChar ch1, ch2, ch3; +in >> ch1 >> ch2 >> ch3; +//! [7] + + +//! [8] +QTextStream out(stdout); +out << "Qt rocks!" << endl; +//! [8] + + +//! [9] +stream << '\n' << flush; +//! [9] + + +//! [10] +QTextStream out(&file); +out.setCodec("UTF-8"); +//! [10] diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp new file mode 100644 index 0000000000..8fd4b8ee9f --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QUrl url("http://www.example.com/List of holidays.xml"); +// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml" +//! [0] + + +//! [1] +QUrl url = QUrl::fromEncoded("http://qt.nokia.com/List%20of%20holidays.xml"); +//! [1] + + +//! [2] +bool checkUrl(const QUrl &url) { + if (!url.isValid()) { + qDebug(QString("Invalid URL: %1").arg(url.toString())); + return false; + } + + return true; +} +//! [2] + + +//! [3] +QTcpSocket sock; +sock.connectToHost(url.host(), url.port(80)); +//! [3] + + +//! [4] +http://www.example.com/cgi-bin/drawgraph.cgi?type(pie)color(green) +//! [4] + + +//! [5] +QUrl baseUrl("http://qt.nokia.com/support"); +QUrl relativeUrl("../products/solutions"); +qDebug(baseUrl.resolved(relativeUrl).toString()); +// prints "http://qt.nokia.com/products/solutions" +//! [5] + + +//! [6] +QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s"); +qDebug(ba.constData()); +// prints "{a fi%73hy %73tring%3F}" +//! [6] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qabstracteventdispatcher.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qabstracteventdispatcher.cpp new file mode 100644 index 0000000000..eba007e947 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qabstracteventdispatcher.cpp @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +bool myEventFilter(void *message); +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp new file mode 100644 index 0000000000..aa5b23265e --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +beginInsertRows(parent, 2, 4); +//! [0] + + +//! [1] +beginInsertRows(parent, 4, 5); +//! [1] + + +//! [2] +beginRemoveRows(parent, 2, 3); +//! [2] + + +//! [3] +beginInsertColumns(parent, 4, 6); +//! [3] + + +//! [4] +beginInsertColumns(parent, 6, 8); +//! [4] + + +//! [5] +beginRemoveColumns(parent, 4, 6); +//! [5] + + +//! [6] +beginMoveRows(sourceParent, 2, 4, destinationParent, 2); +//! [6] + + +//! [7] +beginMoveRows(sourceParent, 2, 4, destinationParent, 6); +//! [7] + + +//! [8] +beginMoveRows(parent, 2, 2, parent, 0); +//! [8] + + +//! [9] +beginMoveRows(parent, 2, 2, parent, 4); +//! [9] + +//! [10] +myData.clear(); +reset(); +//! [10] + +//! [11] +beginResetModel(); +myData.clear(); +endResetModel(); +//! [11] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp new file mode 100644 index 0000000000..8023e6a218 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0); +QApplication::sendEvent(mainWindow, &event); +//! [0] + + +//! [1] +QPushButton *quitButton = new QPushButton("Quit"); +connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit())); +//! [1] + + +//! [2] +foreach (const QString &path, app.libraryPaths()) + do_something(path); +//! [2] + + +//! [3] +bool myEventFilter(void *message, long *result); +//! [3] + + +//! [4] +static int *global_ptr = 0; + +static void cleanup_ptr() +{ + delete [] global_ptr; + global_ptr = 0; +} + +void init_ptr() +{ + global_ptr = new int[100]; // allocate data + qAddPostRoutine(cleanup_ptr); // delete later +} +//! [4] + + +//! [5] +class MyPrivateInitStuff : public QObject +{ +public: + static MyPrivateInitStuff *initStuff(QObject *parent) + { + if (!p) + p = new MyPrivateInitStuff(parent); + return p; + } + + ~MyPrivateInitStuff() + { + // cleanup goes here + } + +private: + MyPrivateInitStuff(QObject *parent) + : QObject(parent) + { + // initialization goes here + } + + MyPrivateInitStuff *p; +}; +//! [5] + + +//! [6] +static inline QString tr(const char *sourceText, + const char *comment = 0); +static inline QString trUtf8(const char *sourceText, + const char *comment = 0); +//! [6] + + +//! [7] +class MyMfcView : public CView +{ + Q_DECLARE_TR_FUNCTIONS(MyMfcView) + +public: + MyMfcView(); + ... +}; +//! [7] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qmetaobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qmetaobject.cpp new file mode 100644 index 0000000000..7c0c2c2122 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qmetaobject.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +void wrapInFunction() +{ + +//! [0] +class MyClass : public QObject +{ + Q_OBJECT + Q_CLASSINFO("author", "Sabrina Schweinsteiger") + Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/") + +public: + ... +}; +//! [0] + + +//! [1] +QByteArray normType = QMetaObject::normalizedType(" int const *"); +// normType is now "const int*" +//! [1] + + +//! [2] +QMetaObject::invokeMethod(pushButton, "animateClick", + Qt::QueuedConnection); +//! [2] + + +//! [3] +QMetaObject::invokeMethod: Unable to handle unregistered datatype 'MyType' +//! [3] + + +//! [4] +QString retVal; +QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, + Q_RETURN_ARG(QString, retVal), + Q_ARG(QString, "sqrt"), + Q_ARG(int, 42), + Q_ARG(double, 9.7)); +//! [4] + + +//! [5] +class MyClass +{ + Q_OBJECT + Q_CLASSINFO("author", "Sabrina Schweinsteiger") + Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/") + +public: + ... +}; +//! [5] + + +//! [propertyCount] +const QMetaObject* metaObject = obj->metaObject(); +QStringList properties; +for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) + properties << QString::fromLatin1(metaObject->property(i).name()); +//! [propertyCount] + + +//! [methodCount] +const QMetaObject* metaObject = obj->metaObject(); +QStringList methods; +for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i) + methods << QString::fromLatin1(metaObject->method(i).methodSignature()); +//! [methodCount] + +//! [6] +int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()"); +QMetaMethod method = metaObject->method(methodIndex); +method.invoke(pushButton, Qt::QueuedConnection); +//! [6] + +//! [7] +QMetaMethod::invoke: Unable to handle unregistered datatype 'MyType' +//! [7] + +//! [8] +QString retVal; +QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)"); +int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature); +QMetaMethod method = metaObject->method(methodIndex); +method.invoke(obj, + Qt::DirectConnection, + Q_RETURN_ARG(QString, retVal), + Q_ARG(QString, "sqrt"), + Q_ARG(int, 42), + Q_ARG(double, 9.7)); +//! [8] + +} diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qmetatype.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qmetatype.cpp new file mode 100644 index 0000000000..d0a7a69884 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qmetatype.cpp @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +struct MyStruct +{ + int i; + ... +}; + +Q_DECLARE_METATYPE(MyStruct) +//! [0] + + +//! [1] +namespace MyNamespace +{ + ... +} + +Q_DECLARE_METATYPE(MyNamespace::MyStruct) +//! [1] + + +//! [2] +MyStruct s; +QVariant var; +var.setValue(s); // copy s into the variant + +... + +// retrieve the value +MyStruct s2 = var.value(); +//! [2] + + +//! [3] +int id = QMetaType::type("MyClass"); +if (id != QMetaType::UnknownType) { + void *myClassPtr = QMetaType::create(id); + ... + QMetaType::destroy(id, myClassPtr); + myClassPtr = 0; +} +//! [3] + + +//! [4] +qRegisterMetaType("MyClass"); +//! [4] + + +//! [5] +qRegisterMetaTypeStreamOperators("MyClass"); +//! [5] + + +//! [6] +QDataStream &operator<<(QDataStream &out, const MyClass &myObj); +QDataStream &operator>>(QDataStream &in, MyClass &myObj); +//! [6] + + +//! [7] +int id = qRegisterMetaType(); +//! [7] + + +//! [8] +int id = qMetaTypeId(); // id is now QMetaType::QString +id = qMetaTypeId(); // compile error if MyStruct not declared +//! [8] + +//! [9] +typedef QString CustomString; +qRegisterMetaType("CustomString"); +//! [9] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qmimedata.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qmimedata.cpp new file mode 100644 index 0000000000..de683cf463 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qmimedata.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +void MyWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasUrls()) + event->acceptProposedAction(); +} + +void MyWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasUrls()) { + foreach (QUrl url, event->mimeData()->urls()) { + ... + } + } +} +//! [0] + + +//! [1] +QByteArray csvData = ...; + +QMimeData *mimeData = new QMimeData; +mimeData->setData("text/csv", csvData); +//! [1] + + +//! [2] +void MyWidget::dropEvent(QDropEvent *event) +{ + const MyMimeData *myData = + qobject_cast(event->mimeData()); + if (myData) { + // access myData's data directly (not through QMimeData's API) + } +} +//! [2] + + +//! [3] +application/x-qt-windows-mime;value="" +//! [3] + + +//! [4] +application/x-qt-windows-mime;value="FileGroupDescriptor" +application/x-qt-windows-mime;value="FileContents" +//! [4] + + +//! [5] +if (event->mimeData()->hasImage()) { + QImage image = qvariant_cast(event->mimeData()->imageData()); + ... +} +//! [5] + + +//! [6] +mimeData->setImageData(QImage("beautifulfjord.png")); +//! [6] + + +//! [7] +if (event->mimeData()->hasColor()) { + QColor color = qvariant_cast(event->mimeData()->colorData()); + ... +} +//! [7] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp new file mode 100644 index 0000000000..5b27775322 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp @@ -0,0 +1,493 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QLineEdit *lineEdit = static_cast( + qt_find_obj_child(myWidget, "QLineEdit", "my line edit")); +if (lineEdit) + lineEdit->setText("Default"); +//! [0] + + +//! [1] +QObject *obj = new QPushButton; +obj->metaObject()->className(); // returns "QPushButton" + +QPushButton::staticMetaObject.className(); // returns "QPushButton" +//! [1] + + +//! [2] +QPushButton::staticMetaObject.className(); // returns "QPushButton" + +QObject *obj = new QPushButton; +obj->metaObject()->className(); // returns "QPushButton" +//! [2] + + +//! [3] +QObject *obj = new QTimer; // QTimer inherits QObject + +QTimer *timer = qobject_cast(obj); +// timer == (QObject *)obj + +QAbstractButton *button = qobject_cast(obj); +// button == 0 +//! [3] + + +//! [4] +QTimer *timer = new QTimer; // QTimer inherits QObject +timer->inherits("QTimer"); // returns true +timer->inherits("QObject"); // returns true +timer->inherits("QAbstractButton"); // returns false + +// QVBoxLayout inherits QObject and QLayoutItem +QVBoxLayout *layout = new QVBoxLayout; +layout->inherits("QObject"); // returns true +layout->inherits("QLayoutItem"); // returns true (even though QLayoutItem is not a QObject) +//! [4] + + +//! [5] +qDebug("MyClass::setPrecision(): (%s) invalid precision %f", + qPrintable(objectName()), newPrecision); +//! [5] + + +//! [6] +class MainWindow : public QMainWindow +{ +public: + MainWindow(); + +protected: + bool eventFilter(QObject *obj, QEvent *ev); + +private: + QTextEdit *textEdit; +}; + +MainWindow::MainWindow() +{ + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + textEdit->installEventFilter(this); +} + +bool MainWindow::eventFilter(QObject *obj, QEvent *event) +{ + if (obj == textEdit) { + if (event->type() == QEvent::KeyPress) { + QKeyEvent *keyEvent = static_cast(event); + qDebug() << "Ate key press" << keyEvent->key(); + return true; + } else { + return false; + } + } else { + // pass the event on to the parent class + return QMainWindow::eventFilter(obj, event); + } +} +//! [6] + + +//! [7] +myObject->moveToThread(QApplication::instance()->thread()); +//! [7] + + +//! [8] +class MyObject : public QObject +{ + Q_OBJECT + +public: + MyObject(QObject *parent = 0); + +protected: + void timerEvent(QTimerEvent *event); +}; + +MyObject::MyObject(QObject *parent) + : QObject(parent) +{ + startTimer(50); // 50-millisecond timer + startTimer(1000); // 1-second timer + startTimer(60000); // 1-minute timer +} + +void MyObject::timerEvent(QTimerEvent *event) +{ + qDebug() << "Timer ID:" << event->timerId(); +} +//! [8] + + +//! [9] +QList list = window()->queryList("QAbstractButton")); +foreach (QObject *obj, list) + static_cast(obj)->setEnabled(false); +//! [9] + + +//! [10] +QPushButton *button = parentWidget->findChild("button1"); +//! [10] + + +//! [11] +QListWidget *list = parentWidget->findChild(); +//! [11] + + +//! [12] +QList widgets = parentWidget.findChildren("widgetname"); +//! [12] + + +//! [13] +QList allPButtons = parentWidget.findChildren(); +//! [13] + + +//! [14] +monitoredObj->installEventFilter(filterObj); +//! [14] + + +//! [15] +class KeyPressEater : public QObject +{ + Q_OBJECT + ... + +protected: + bool eventFilter(QObject *obj, QEvent *event); +}; + +bool KeyPressEater::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::KeyPress) { + QKeyEvent *keyEvent = static_cast(event); + qDebug("Ate key press %d", keyEvent->key()); + return true; + } else { + // standard event processing + return QObject::eventFilter(obj, event); + } +} +//! [15] + + +//! [16] +KeyPressEater *keyPressEater = new KeyPressEater(this); +QPushButton *pushButton = new QPushButton(this); +QListView *listView = new QListView(this); + +pushButton->installEventFilter(keyPressEater); +listView->installEventFilter(keyPressEater); +//! [16] + + +//! [17] +MyWindow::MyWindow() +{ + QLabel *senderLabel = new QLabel(tr("Name:")); + QLabel *recipientLabel = new QLabel(tr("Name:", "recipient")); +//! [17] +} + + +//! [18] +int n = messages.count(); +showMessage(tr("%n message(s) saved", "", n)); +//! [18] + + +//! [19] +n == 1 ? tr("%n message saved") : tr("%n messages saved") +//! [19] + + +//! [20] +label->setText(tr("F\374r \310lise")); +//! [20] + + +//! [21] +if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) { + QByteArray data; + get_the_value(&data); // expensive operation + emit valueChanged(data); +} +//! [21] + + +//! [22] +QLabel *label = new QLabel; +QScrollBar *scrollBar = new QScrollBar; +QObject::connect(scrollBar, SIGNAL(valueChanged(int)), + label, SLOT(setNum(int))); +//! [22] + + +//! [23] +// WRONG +QObject::connect(scrollBar, SIGNAL(valueChanged(int value)), + label, SLOT(setNum(int value))); +//! [23] + + +//! [24] +class MyWidget : public QWidget +{ + Q_OBJECT + +public: + MyWidget(); + +signals: + void buttonClicked(); + +private: + QPushButton *myButton; +}; + +MyWidget::MyWidget() +{ + myButton = new QPushButton(this); + connect(myButton, SIGNAL(clicked()), + this, SIGNAL(buttonClicked())); +} +//! [24] + + +//! [25] +QObject::connect: Cannot queue arguments of type 'MyType' +(Make sure 'MyType' is registered using qRegisterMetaType().) +//! [25] + + +//! [26] +disconnect(myObject, 0, 0, 0); +//! [26] + + +//! [27] +myObject->disconnect(); +//! [27] + + +//! [28] +disconnect(myObject, SIGNAL(mySignal()), 0, 0); +//! [28] + + +//! [29] +myObject->disconnect(SIGNAL(mySignal())); +//! [29] + + +//! [30] +disconnect(myObject, 0, myReceiver, 0); +//! [30] + + +//! [31] +myObject->disconnect(myReceiver); +//! [31] + + +//! [32] +if (QLatin1String(signal) == SIGNAL(valueChanged(int))) { + // signal is valueChanged(int) +} +//! [32] + + +//! [33] +void on__(); +//! [33] + + +//! [34] +void on_button1_clicked(); +//! [34] + + +//! [35] +class MyClass : public QObject +{ + Q_OBJECT + Q_CLASSINFO("Author", "Pierre Gendron") + Q_CLASSINFO("URL", "http://www.my-organization.qc.ca") + +public: + ... +}; +//! [35] + + +//! [36] +Q_PROPERTY(type name + READ getFunction + [WRITE setFunction] + [RESET resetFunction] + [NOTIFY notifySignal] + [DESIGNABLE bool] + [SCRIPTABLE bool] + [STORED bool] + [USER bool] + [CONSTANT] + [FINAL]) +//! [36] + + +//! [37] +Q_PROPERTY(QString title READ title WRITE setTitle USER true) +//! [37] + + +//! [38] +class MyClass : public QObject +{ + Q_OBJECT + Q_ENUMS(Priority) + +public: + MyClass(QObject *parent = 0); + ~MyClass(); + + enum Priority { High, Low, VeryHigh, VeryLow }; + void setPriority(Priority priority); + Priority priority() const; +}; +//! [38] + + +//! [39a] +class QLibrary : public QObject +{ + ... + Q_FLAGS(LoadHint LoadHints) + ... +//! [39a] + +//! [39b] + ... +public: + enum LoadHint { + ResolveAllSymbolsHint = 0x01, + ExportExternalSymbolsHint = 0x02, + LoadArchiveMemberHint = 0x04 + }; + Q_DECLARE_FLAGS(LoadHints, LoadHint) + ... +//! [39b] + + +//! [40] +//: This name refers to a host name. +hostNameLabel->setText(tr("Name:")); + +/*: This text refers to a C++ code example. */ +QString example = tr("Example"); +//! [40] + +//! [41] +QPushButton *button = parentWidget->findChild("button1", Qt::FindDirectChildOnly); +//! [41] + + +//! [42] +QListWidget *list = parentWidget->findChild(QString(), Qt::FindDirectChildOnly); +//! [42] + + +//! [43] +QList childButtons = parentWidget.findChildren(QString(), Qt::FindDirectChildOnly); +//! [43] + +//! [44] +QLabel *label = new QLabel; +QLineEdit *lineEdit = new QLineEdit; +QObject::connect(lineEdit, &QLineEdit::textChanged, + label, &QLabel::setText); +//! [44] + +//! [45] +void someFunction(); +QPushButton *button = new QPushButton; +QObject::connect(button, &QPushButton::clicked, someFunction); +//! [45] + +//! [46] +QByteArray page = ...; +QTcpSocket *socket = new QTcpSocket; +socket->connectToHost("qt-project.org", 80); +QObject::connect(socket, &QTcpSocket::connected, [=] () { + socket->write("GET " + page + "\r\n"); + }); +//! [46] + +//! [47] +disconnect(myObject, &MyObject::mySignal(), 0, 0); +//! [47] + +//! [48] +QObject::disconnect(lineEdit, &QLineEdit::textChanged, + label, &QLabel::setText); +//! [48] + +//! [meta data] +//: This is a comment for the translator. +//= qtn_foo_bar +//~ loc-layout_id foo_dialog +//~ loc-blank False +//~ magic-stuff This might mean something magic. +QString text = MyMagicClass::tr("Sim sala bim."); +//! [meta data] + +//! [explicit tr context] +QString text = QScrollBar::tr("Page up"); +//! [explicit tr context] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qsystemsemaphore.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qsystemsemaphore.cpp new file mode 100644 index 0000000000..da6e9fca8e --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qsystemsemaphore.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QSystemSemaphore sem("market", 3, QSystemSemaphore::Create); + // resources available == 3 +sem.acquire(); // resources available == 2 +sem.acquire(); // resources available == 1 +sem.acquire(); // resources available == 0 +sem.release(); // resources available == 1 +sem.release(2); // resources available == 3 +//! [0] + + +//! [1] +QSystemSemaphore sem("market", 5, QSystemSemaphore::Create); +sem.acquire(5); // acquire all 5 resources +sem.release(5); // release the 5 resources +//! [1] + + +//! [2] +sem.release(10); // "create" 10 new resources +//! [2] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qtimer.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qtimer.cpp new file mode 100644 index 0000000000..dfdef73e75 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qtimer.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QTimer::singleShot(600000, &app, SLOT(quit())); + ... + return app.exec(); +} +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp new file mode 100644 index 0000000000..ae38c8372f --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QDataStream out(...); +QVariant v(123); // The variant now contains an int +int x = v.toInt(); // x = 123 +out << v; // Writes a type tag and an int to out +v = QVariant("hello"); // The variant now contains a QByteArray +v = QVariant(tr("hello")); // The variant now contains a QString +int y = v.toInt(); // y = 0 since v cannot be converted to an int +QString s = v.toString(); // s = tr("hello") (see QObject::tr()) +out << v; // Writes a type tag and a QString to out +... +QDataStream in(...); // (opening the previously written stream) +in >> v; // Reads an Int variant +int z = v.toInt(); // z = 123 +qDebug("Type is %s", // prints "Type is int" + v.typeName()); +v = v.toInt() + 100; // The variant now hold the value 223 +v = QVariant(QStringList()); +//! [0] + + +//! [1] +QVariant x, y(QString()), z(QString("")); +x.convert(QVariant::Int); +// x.isNull() == true +// y.isNull() == true, z.isNull() == false +//! [1] + + +//! [2] +QVariant variant; +... +QColor color = variant.value(); +//! [2] + + +//! [3] +QColor color = palette().background().color(); +QVariant variant = color; +//! [3] + + +//! [4] +QVariant v; + +v.setValue(5); +int i = v.toInt(); // i is now 5 +QString s = v.toString() // s is now "5" + +MyCustomStruct c; +v.setValue(c); + +... + +MyCustomStruct c2 = v.value(); +//! [4] + + +//! [5] +QVariant v; + +MyCustomStruct c; +if (v.canConvert()) + c = v.value(); + +v = 7; +int i = v.value(); // same as v.toInt() +QString s = v.value(); // same as v.toString(), s is now "7" +MyCustomStruct c2 = v.value(); // conversion failed, c2 is empty +//! [5] + + +//! [6] +QVariant v = 42; + +v.canConvert(); // returns true +v.canConvert(); // returns true + +MyCustomStruct s; +v.setValue(s); + +v.canConvert(); // returns false +v.canConvert(); // returns true +//! [6] + + +//! [7] +MyCustomStruct s; +return QVariant::fromValue(s); +//! [7] + + +//! [8] +QObject *object = getObjectFromSomewhere(); +QVariant data = QVariant::fromValue(object); +//! [8] diff --git a/src/corelib/doc/snippets/code/src_corelib_mimetype_qmimedatabase.cpp b/src/corelib/doc/snippets/code/src_corelib_mimetype_qmimedatabase.cpp new file mode 100644 index 0000000000..e59a96b87a --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_mimetype_qmimedatabase.cpp @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QMimeDatabase db; +QMimeType mime = db.mimeTypeForFile(fileName); +if (mime.inherits("text/plain")) { + // The file is plain text, we can display it in a QTextEdit +} +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_plugin_qlibrary.cpp b/src/corelib/doc/snippets/code/src_corelib_plugin_qlibrary.cpp new file mode 100644 index 0000000000..9de685a7b5 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_plugin_qlibrary.cpp @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QLibrary myLib("mylib"); +typedef void (*MyPrototype)(); +MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol"); +if (myFunction) + myFunction(); +//! [0] + + +//! [1] +typedef void (*MyPrototype)(); +MyPrototype myFunction = + (MyPrototype) QLibrary::resolve("mylib", "mysymbol"); +if (myFunction) + myFunction(); +//! [1] + + +//! [2] +typedef int (*AvgFunction)(int, int); + +AvgFunction avg = (AvgFunction) library->resolve("avg"); +if (avg) + return avg(5, 8); +else + return -1; +//! [2] + + +//! [3] +extern "C" MY_EXPORT int avg(int a, int b) +{ + return (a + b) / 2; +} +//! [3] + + +//! [4] +#ifdef Q_OS_WIN +#define MY_EXPORT __declspec(dllexport) +#else +#define MY_EXPORT +#endif +//! [4] diff --git a/src/corelib/doc/snippets/code/src_corelib_plugin_quuid.cpp b/src/corelib/doc/snippets/code/src_corelib_plugin_quuid.cpp new file mode 100644 index 0000000000..5c8f6ae091 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_plugin_quuid.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +// {67C8770B-44F1-410A-AB9A-F9B5446F13EE} +QUuid IID_MyInterface(0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee) +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp b/src/corelib/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp new file mode 100644 index 0000000000..e212470b75 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [simple state machine] +QPushButton button; + +QStateMachine machine; +QState *s1 = new QState(); +s1->assignProperty(&button, "text", "Click me"); + +QFinalState *s2 = new QFinalState(); +s1->addTransition(&button, SIGNAL(clicked()), s2); + +machine.addState(s1); +machine.addState(s2); +machine.setInitialState(s1); +machine.start(); +//! [simple state machine] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qatomic.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qatomic.cpp new file mode 100644 index 0000000000..7e3d7dc7e4 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qatomic.cpp @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +MySharedType &MySharedType::operator=(const MySharedType &other) +{ + (void) other.data->atomicInt.ref(); + if (!data->atomicInt.deref()) { + // The last reference has been released + delete d; + } + d = other.d; + return *this; +} +//! [0] + + +//! [1] +if (currentValue == expectedValue) { + currentValue = newValue; + return true; +} +return false; +//! [1] + + +//! [2] +int originalValue = currentValue; +currentValue = newValue; +return originalValue; +//! [2] + + +//! [3] +int originalValue = currentValue; +currentValue += valueToAdd; +return originalValue; +//! [3] + + +//! [4] +if (currentValue == expectedValue) { + currentValue = newValue; + return true; +} +return false; +//! [4] + + +//! [5] +T *originalValue = currentValue; +currentValue = newValue; +return originalValue; +//! [5] + + +//! [6] +T *originalValue = currentValue; +currentValue += valueToAdd; +return originalValue; +//! [6] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qmutex.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qmutex.cpp new file mode 100644 index 0000000000..f5fbeb8419 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qmutex.cpp @@ -0,0 +1,193 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +int number = 6; + +void method1() +{ + number *= 5; + number /= 4; +} + +void method2() +{ + number *= 3; + number /= 2; +} +//! [0] + + +//! [1] +// method1() +number *= 5; // number is now 30 +number /= 4; // number is now 7 + +// method2() +number *= 3; // number is now 21 +number /= 2; // number is now 10 +//! [1] + + +//! [2] +// Thread 1 calls method1() +number *= 5; // number is now 30 + +// Thread 2 calls method2(). +// +// Most likely Thread 1 has been put to sleep by the operating +// system to allow Thread 2 to run. +number *= 3; // number is now 90 +number /= 2; // number is now 45 + +// Thread 1 finishes executing. +number /= 4; // number is now 11, instead of 10 +//! [2] + + +//! [3] +QMutex mutex; +int number = 6; + +void method1() +{ + mutex.lock(); + number *= 5; + number /= 4; + mutex.unlock(); +} + +void method2() +{ + mutex.lock(); + number *= 3; + number /= 2; + mutex.unlock(); +} +//! [3] + + +//! [4] +int complexFunction(int flag) +{ + mutex.lock(); + + int retVal = 0; + + switch (flag) { + case 0: + case 1: + retVal = moreComplexFunction(flag); + break; + case 2: + { + int status = anotherFunction(); + if (status < 0) { + mutex.unlock(); + return -2; + } + retVal = status + flag; + } + break; + default: + if (flag > 10) { + mutex.unlock(); + return -1; + } + break; + } + + mutex.unlock(); + return retVal; +} +//! [4] + + +//! [5] +int complexFunction(int flag) +{ + QMutexLocker locker(&mutex); + + int retVal = 0; + + switch (flag) { + case 0: + case 1: + return moreComplexFunction(flag); + case 2: + { + int status = anotherFunction(); + if (status < 0) + return -2; + retVal = status + flag; + } + break; + default: + if (flag > 10) + return -1; + break; + } + + return retVal; +} +//! [5] + + +//! [6] +class SignalWaiter +{ +private: + QMutexLocker locker; + +public: + SignalWaiter(QMutex *mutex) + : locker(mutex) + { + } + + void waitForSignal() + { + ... + while (!signalled) + waitCondition.wait(locker.mutex()); + ... + } +}; +//! [6] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qmutexpool.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qmutexpool.cpp new file mode 100644 index 0000000000..29076db91c --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qmutexpool.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class Number { +public: + Number(double n) : num (n) { } + + void setNumber(double n) { num = n; } + double number() const { return num; } + +private: + double num; +}; +//! [0] + + +//! [1] +void calcSquare(Number *num) +{ + QMutexLocker locker(mutexpool.get(num)); + num.setNumber(num.number() * num.number()); +} +//! [1] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qreadwritelock.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qreadwritelock.cpp new file mode 100644 index 0000000000..bd42c01905 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qreadwritelock.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QReadWriteLock lock; + +void ReaderThread::run() +{ + ... + lock.lockForRead(); + read_file(); + lock.unlock(); + ... +} + +void WriterThread::run() +{ + ... + lock.lockForWrite(); + write_file(); + lock.unlock(); + ... +} +//! [0] + + +//! [1] +QReadWriteLock lock; + +QByteArray readData() +{ + QReadLocker locker(&lock); + ... + return data; +} +//! [1] + + +//! [2] +QReadWriteLock lock; + +QByteArray readData() +{ + lock.lockForRead(); + ... + lock.unlock(); + return data; +} +//! [2] + + +//! [3] +QReadWriteLock lock; + +void writeData(const QByteArray &data) +{ + QWriteLocker locker(&lock); + ... +} +//! [3] + + +//! [4] +QReadWriteLock lock; + +void writeData(const QByteArray &data) +{ + lock.lockForWrite(); + ... + lock.unlock(); +} +//! [4] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qsemaphore.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qsemaphore.cpp new file mode 100644 index 0000000000..fd4ff98314 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qsemaphore.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QSemaphore sem(5); // sem.available() == 5 + +sem.acquire(3); // sem.available() == 2 +sem.acquire(2); // sem.available() == 0 +sem.release(5); // sem.available() == 5 +sem.release(5); // sem.available() == 10 + +sem.tryAcquire(1); // sem.available() == 9, returns true +sem.tryAcquire(250); // sem.available() == 9, returns false +//! [0] + + +//! [1] +QSemaphore sem(5); // a semaphore that guards 5 resources +sem.acquire(5); // acquire all 5 resources +sem.release(5); // release the 5 resources +sem.release(10); // "create" 10 new resources +//! [1] + + +//! [2] +QSemaphore sem(5); // sem.available() == 5 +sem.tryAcquire(250); // sem.available() == 5, returns false +sem.tryAcquire(3); // sem.available() == 2, returns true +//! [2] + + +//! [3] +QSemaphore sem(5); // sem.available() == 5 +sem.tryAcquire(250, 1000); // sem.available() == 5, waits 1000 milliseconds and returns false +sem.tryAcquire(3, 30000); // sem.available() == 2, returns true without waiting +//! [3] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp new file mode 100644 index 0000000000..681023c91b --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class MyThread : public QThread +{ +public: + void run(); +}; + +void MyThread::run() +{ + QTcpSocket socket; + // connect QTcpSocket's signals somewhere meaningful + ... + socket.connectToHost(hostName, portNumber); + exec(); +} +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp new file mode 100644 index 0000000000..149cae5bf6 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +forever { + mutex.lock(); + keyPressed.wait(&mutex); + do_something(); + mutex.unlock(); +} +//! [0] + + +//! [1] +forever { + getchar(); + keyPressed.wakeAll(); +} +//! [1] + + +//! [2] +forever { + mutex.lock(); + keyPressed.wait(&mutex); + ++count; + mutex.unlock(); + + do_something(); + + mutex.lock(); + --count; + mutex.unlock(); +} +//! [2] + + +//! [3] +forever { + getchar(); + + mutex.lock(); + // Sleep until there are no busy worker threads + while (count > 0) { + mutex.unlock(); + sleep(1); + mutex.lock(); + } + keyPressed.wakeAll(); + mutex.unlock(); +} +//! [3] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp new file mode 100644 index 0000000000..cfaa6ef485 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QBitArray ba(200); +//! [0] + + +//! [1] +QBitArray ba; +ba.resize(3); +ba[0] = true; +ba[1] = false; +ba[2] = true; +//! [1] + + +//! [2] +QBitArray ba(3); +ba.setBit(0, true); +ba.setBit(1, false); +ba.setBit(2, true); +//! [2] + + +//! [3] +QBitArray x(5); +x.setBit(3, true); +// x: [ 0, 0, 0, 1, 0 ] + +QBitArray y(5); +y.setBit(4, true); +// y: [ 0, 0, 0, 0, 1 ] + +x |= y; +// x: [ 0, 0, 0, 1, 1 ] +//! [3] + + +//! [4] +QBitArray().isNull(); // returns true +QBitArray().isEmpty(); // returns true + +QBitArray(0).isNull(); // returns false +QBitArray(0).isEmpty(); // returns true + +QBitArray(3).isNull(); // returns false +QBitArray(3).isEmpty(); // returns false +//! [4] + + +//! [5] +QBitArray().isNull(); // returns true +QBitArray(0).isNull(); // returns false +QBitArray(3).isNull(); // returns false +//! [5] + + +//! [6] +QBitArray ba(8); +ba.fill(true); +// ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] + +ba.fill(false, 2); +// ba: [ 0, 0 ] +//! [6] + + +//! [7] +QBitArray a(3); +a[0] = false; +a[1] = true; +a[2] = a[0] ^ a[1]; +//! [7] + + +//! [8] +QBitArray a(3); +QBitArray b(2); +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +a &= b; // a: [ 1, 0, 0 ] +//! [8] + + +//! [9] +QBitArray a(3); +QBitArray b(2); +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +a |= b; // a: [ 1, 1, 1 ] +//! [9] + + +//! [10] +QBitArray a(3); +QBitArray b(2); +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +a ^= b; // a: [ 0, 1, 1 ] +//! [10] + + +//! [11] +QBitArray a(3); +QBitArray b; +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b = ~a; // b: [ 0, 1, 0 ] +//! [11] + + +//! [12] +QBitArray a(3); +QBitArray b(2); +QBitArray c; +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +c = a & b; // c: [ 1, 0, 0 ] +//! [12] + + +//! [13] +QBitArray a(3); +QBitArray b(2); +QBitArray c; +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +c = a | b; // c: [ 1, 1, 1 ] +//! [13] + + +//! [14] +QBitArray a(3); +QBitArray b(2); +QBitArray c; +a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] +b[0] = 1; b[1] = 0; // b: [ 1, 1 ] +c = a ^ b; // c: [ 0, 1, 1 ] +//! [14] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp new file mode 100644 index 0000000000..a232f490b2 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp @@ -0,0 +1,421 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +void wrapInFunction() +{ + +//! [0] +QByteArray ba("Hello"); +//! [0] + + +//! [1] +QByteArray ba; +ba.resize(5); +ba[0] = 0x3c; +ba[1] = 0xb8; +ba[2] = 0x64; +ba[3] = 0x18; +ba[4] = 0xca; +//! [1] + + +//! [2] +for (int i = 0; i < ba.size(); ++i) { + if (ba.at(i) >= 'a' && ba.at(i) <= 'f') + cout << "Found character in range [a-f]" << endl; +} +//! [2] + + +//! [3] +QByteArray x("and"); +x.prepend("rock "); // x == "rock and" +x.append(" roll"); // x == "rock and roll" +x.replace(5, 3, "&"); // x == "rock & roll" +//! [3] + + +//! [4] +QByteArray ba("We must be bold, very bold"); +int j = 0; +while ((j = ba.indexOf("", j)) != -1) { + cout << "Found tag at index position " << j << endl; + ++j; +} +//! [4] + + +//! [5] +QByteArray().isNull(); // returns true +QByteArray().isEmpty(); // returns true + +QByteArray("").isNull(); // returns false +QByteArray("").isEmpty(); // returns true + +QByteArray("abc").isNull(); // returns false +QByteArray("abc").isEmpty(); // returns false +//! [5] + + +//! [6] +QByteArray ba("Hello"); +int n = ba.size(); // n == 5 +ba.data()[0]; // returns 'H' +ba.data()[4]; // returns 'o' +ba.data()[5]; // returns '\0' +//! [6] + + +//! [7] +QByteArray().isEmpty(); // returns true +QByteArray("").isEmpty(); // returns true +QByteArray("abc").isEmpty(); // returns false +//! [7] + + +//! [8] +QByteArray ba("Hello world"); +char *data = ba.data(); +while (*data) { + cout << "[" << *data << "]" << endl; + ++data; +} +//! [8] + + +//! [9] +QByteArray ba; +for (int i = 0; i < 10; ++i) + ba[i] = 'A' + i; +// ba == "ABCDEFGHIJ" +//! [9] + + +//! [10] +QByteArray ba("Stockholm"); +ba.truncate(5); // ba == "Stock" +//! [10] + + +//! [11] +QByteArray ba("STARTTLS\r\n"); +ba.chop(2); // ba == "STARTTLS" +//! [11] + + +//! [12] +QByteArray x("free"); +QByteArray y("dom"); +x += y; +// x == "freedom" +//! [12] + + +//! [13] +QByteArray().isNull(); // returns true +QByteArray("").isNull(); // returns false +QByteArray("abc").isNull(); // returns false +//! [13] + + +//! [14] +QByteArray ba("Istambul"); +ba.fill('o'); +// ba == "oooooooo" + +ba.fill('X', 2); +// ba == "XX" +//! [14] + + +//! [15] +QByteArray x("ship"); +QByteArray y("air"); +x.prepend(y); +// x == "airship" +//! [15] + + +//! [16] +QByteArray x("free"); +QByteArray y("dom"); +x.append(y); +// x == "freedom" +//! [16] + + +//! [17] +QByteArray ba("Meal"); +ba.insert(1, QByteArray("ontr")); +// ba == "Montreal" +//! [17] + + +//! [18] +QByteArray ba("Montreal"); +ba.remove(1, 4); +// ba == "Meal" +//! [18] + + +//! [19] +QByteArray x("Say yes!"); +QByteArray y("no"); +x.replace(4, 3, y); +// x == "Say no!" +//! [19] + + +//! [20] +QByteArray ba("colour behaviour flavour neighbour"); +ba.replace(QByteArray("ou"), QByteArray("o")); +// ba == "color behavior flavor neighbor" +//! [20] + + +//! [21] +QByteArray x("sticky question"); +QByteArray y("sti"); +x.indexOf(y); // returns 0 +x.indexOf(y, 1); // returns 10 +x.indexOf(y, 10); // returns 10 +x.indexOf(y, 11); // returns -1 +//! [21] + + +//! [22] +QByteArray ba("ABCBA"); +ba.indexOf("B"); // returns 1 +ba.indexOf("B", 1); // returns 1 +ba.indexOf("B", 2); // returns 3 +ba.indexOf("X"); // returns -1 +//! [22] + + +//! [23] +QByteArray x("crazy azimuths"); +QByteArray y("az"); +x.lastIndexOf(y); // returns 6 +x.lastIndexOf(y, 6); // returns 6 +x.lastIndexOf(y, 5); // returns 2 +x.lastIndexOf(y, 1); // returns -1 +//! [23] + + +//! [24] +QByteArray ba("ABCBA"); +ba.lastIndexOf("B"); // returns 3 +ba.lastIndexOf("B", 3); // returns 3 +ba.lastIndexOf("B", 2); // returns 1 +ba.lastIndexOf("X"); // returns -1 +//! [24] + + +//! [25] +QByteArray url("ftp://ftp.qt.nokia.com/"); +if (url.startsWith("ftp:")) + ... +//! [25] + + +//! [26] +QByteArray url("http://qt.nokia.com/index.html"); +if (url.endsWith(".html")) + ... +//! [26] + + +//! [27] +QByteArray x("Pineapple"); +QByteArray y = x.left(4); +// y == "Pine" +//! [27] + + +//! [28] +QByteArray x("Pineapple"); +QByteArray y = x.right(5); +// y == "apple" +//! [28] + + +//! [29] +QByteArray x("Five pineapples"); +QByteArray y = x.mid(5, 4); // y == "pine" +QByteArray z = x.mid(5); // z == "pineapples" +//! [29] + + +//! [30] +QByteArray x("Qt by NOKIA"); +QByteArray y = x.toLower(); +// y == "qt by nokia" +//! [30] + + +//! [31] +QByteArray x("Qt by NOKIA"); +QByteArray y = x.toUpper(); +// y == "QT BY NOKIA" +//! [31] + + +//! [32] +QByteArray ba(" lots\t of\nwhitespace\r\n "); +ba = ba.simplified(); +// ba == "lots of whitespace"; +//! [32] + + +//! [33] +QByteArray ba(" lots\t of\nwhitespace\r\n "); +ba = ba.trimmed(); +// ba == "lots\t of\nwhitespace"; +//! [33] + + +//! [34] +QByteArray x("apple"); +QByteArray y = x.leftJustified(8, '.'); // y == "apple..." +//! [34] + + +//! [35] +QByteArray x("apple"); +QByteArray y = x.rightJustified(8, '.'); // y == "...apple" +//! [35] + + +//! [36] +QByteArray str("FF"); +bool ok; +int hex = str.toInt(&ok, 16); // hex == 255, ok == true +int dec = str.toInt(&ok, 10); // dec == 0, ok == false +//! [36] + + +//! [37] +QByteArray str("FF"); +bool ok; +long hex = str.toLong(&ok, 16); // hex == 255, ok == true +long dec = str.toLong(&ok, 10); // dec == 0, ok == false +//! [37] + + +//! [38] +QByteArray string("1234.56"); +double a = string.toDouble(); // a == 1234.56 +//! [38] + + +//! [39] +QByteArray text("Qt is great!"); +text.toBase64(); // returns "UXQgaXMgZ3JlYXQh" +//! [39] + + +//! [40] +QByteArray ba; +int n = 63; +ba.setNum(n); // ba == "63" +ba.setNum(n, 16); // ba == "3f" +//! [40] + + +//! [41] +int n = 63; +QByteArray::number(n); // returns "63" +QByteArray::number(n, 16); // returns "3f" +QByteArray::number(n, 16).toUpper(); // returns "3F" +//! [41] + + +//! [42] +QByteArray ba = QByteArray::number(12.3456, 'E', 3); +// ba == 1.235E+01 +//! [42] + + +//! [43] + static const char mydata[] = { + 0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76, + 0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99, + ... + 0x6d, 0x5b +}; + +QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata)); +QDataStream in(&data, QIODevice::ReadOnly); +... +//! [43] + + +//! [44] +QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh"); +text.data(); // returns "Qt is great!" +//! [44] + + +//! [45] +QByteArray text = QByteArray::fromHex("517420697320677265617421"); +text.data(); // returns "Qt is great!" +//! [45] + +//! [46] +QString tmp = "test"; +QByteArray text = tmp.toLocal8Bit(); +char *data = new char[text.size()] +strcpy(data, text.data()); +delete [] data; +//! [46] + +//! [47] +QString tmp = "test"; +QByteArray text = tmp.toLocal8Bit(); +char *data = new char[text.size() + 1] +strcpy(data, text.data()); +delete [] data; +//! [47] + +} + + diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp new file mode 100644 index 0000000000..59cd67fe6d --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QDate d1(1995, 5, 17); // May 17, 1995 +QDate d2(1995, 5, 20); // May 20, 1995 +d1.daysTo(d2); // returns 3 +d2.daysTo(d1); // returns -3 +//! [0] + + +//! [1] +QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy"); +// date is 1 December 2003 +//! [1] + + +//! [2] +QDate date = QDate::fromString("130", "Md"); // invalid +//! [2] + + +//! [3] +QDate::fromString("1.30", "M.d"); // January 30 1900 +QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000 +QDate::fromString("20000110", "yyyyMd"); // January 10, 2000 +//! [3] + + +//! [4] +QDate::isValid(2002, 5, 17); // true +QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist) +QDate::isValid(2004, 2, 29); // true (2004 is a leap year) +QDate::isValid(2000, 2, 29); // true (2000 is a leap year) +QDate::isValid(2006, 2, 29); // false (2006 is not a leap year) +QDate::isValid(2100, 2, 29); // false (2100 is not a leap year) +QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian) +//! [4] + + +//! [5] +QTime n(14, 0, 0); // n == 14:00:00 +QTime t; +t = n.addSecs(70); // t == 14:01:10 +t = n.addSecs(-70); // t == 13:58:50 +t = n.addSecs(10 * 60 * 60 + 5); // t == 00:00:05 +t = n.addSecs(-15 * 60 * 60); // t == 23:00:00 +//! [5] + + +//! [6] +QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss"); +// time is 12:01.00 +//! [6] + + +//! [7] +QTime time = QTime::fromString("00:710", "hh:ms"); // invalid +//! [7] + + +//! [8] +QTime time = QTime::fromString("1.30", "m.s"); +// time is 00:01:30.000 +//! [8] + + +//! [9] +QTime::isValid(21, 10, 30); // returns true +QTime::isValid(22, 5, 62); // returns false +//! [9] + + +//! [10] +QTime t; +t.start(); +some_lengthy_task(); +qDebug("Time elapsed: %d ms", t.elapsed()); +//! [10] + + +//! [11] +QDateTime now = QDateTime::currentDateTime(); +QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0)); +qDebug("There are %d seconds to Christmas", now.secsTo(xmas)); +//! [11] + + +//! [12] +QTime time1 = QTime::fromString("131", "HHh"); +// time1 is 13:00:00 +QTime time1 = QTime::fromString("1apA", "1amAM"); +// time1 is 01:00:00 + +QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02", + "'M'M'd'd'y'yyhh:mm:ss"); +// dateTime is 1 January 1998 00:01:02 +//! [12] + + +//! [13] +QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid +//! [13] + + +//! [14] +QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s"); +// dateTime is January 30 in 1900 at 00:00:01. +//! [14] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp new file mode 100644 index 0000000000..3aa0d986af --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +qreal myEasingFunction(qreal progress); +//! [0] + diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp new file mode 100644 index 0000000000..2fa73bac46 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp @@ -0,0 +1,299 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QHash hash; +//! [0] + + +//! [1] +hash["one"] = 1; +hash["three"] = 3; +hash["seven"] = 7; +//! [1] + + +//! [2] +hash.insert("twelve", 12); +//! [2] + + +//! [3] +int num1 = hash["thirteen"]; +int num2 = hash.value("thirteen"); +//! [3] + + +//! [4] +int timeout = 30; +if (hash.contains("TIMEOUT")) + timeout = hash.value("TIMEOUT"); +//! [4] + + +//! [5] +int timeout = hash.value("TIMEOUT", 30); +//! [5] + + +//! [6] +// WRONG +QHash hash; +... +for (int i = 0; i < 1000; ++i) { + if (hash[i] == okButton) + cout << "Found button at index " << i << endl; +} +//! [6] + + +//! [7] +QHashIterator i(hash); +while (i.hasNext()) { + i.next(); + cout << i.key() << ": " << i.value() << endl; +} +//! [7] + + +//! [8] +QHash::const_iterator i = hash.constBegin(); +while (i != hash.constEnd()) { + cout << i.key() << ": " << i.value() << endl; + ++i; +} +//! [8] + + +//! [9] +hash.insert("plenty", 100); +hash.insert("plenty", 2000); +// hash.value("plenty") == 2000 +//! [9] + + +//! [10] +QList values = hash.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [10] + + +//! [11] +QHash::iterator i = hash.find("plenty"); +while (i != hash.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [11] + + +//! [12] +QHash hash; +... +foreach (int value, hash) + cout << value << endl; +//! [12] + + +//! [13] +#ifndef EMPLOYEE_H +#define EMPLOYEE_H + +class Employee +{ +public: + Employee() {} + Employee(const QString &name, const QDate &dateOfBirth); + ... + +private: + QString myName; + QDate myDateOfBirth; +}; + +inline bool operator==(const Employee &e1, const Employee &e2) +{ + return e1.name() == e2.name() + && e1.dateOfBirth() == e2.dateOfBirth(); +} + +inline uint qHash(const Employee &key, uint seed) +{ + return qHash(key.name(), seed) ^ key.dateOfBirth().day(); +} + +#endif // EMPLOYEE_H +//! [13] + + +//! [14] +QHash hash; +hash.reserve(20000); +for (int i = 0; i < 20000; ++i) + hash.insert(keys[i], values[i]); +//! [14] + + +//! [15] +QHash objectHash; +... +QHash::iterator i = objectHash.find(obj); +while (i != objectHash.end() && i.key() == obj) { + if (i.value() == 0) { + i = objectHash.erase(i); + } else { + ++i; + } +} +//! [15] + + +//! [16] +QHash hash; +... +QHash::const_iterator i = hash.find("HDR"); +while (i != hash.end() && i.key() == "HDR") { + cout << i.value() << endl; + ++i; +} +//! [16] + + +//! [17] +QHash hash; +hash.insert("January", 1); +hash.insert("February", 2); +... +hash.insert("December", 12); + +QHash::iterator i; +for (i = hash.begin(); i != hash.end(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [17] + + +//! [18] +QHash::iterator i; +for (i = hash.begin(); i != hash.end(); ++i) + i.value() += 2; +//! [18] + + +//! [19] +QHash::iterator i = hash.begin(); +while (i != hash.end()) { + if (i.key().startsWith("_")) + i = hash.erase(i); + else + ++i; +} +//! [19] + + +//! [20] +QHash::iterator i = hash.begin(); +while (i != hash.end()) { + QHash::iterator prev = i; + ++i; + if (prev.key().startsWith("_")) + hash.erase(prev); +} +//! [20] + + +//! [21] +// WRONG +while (i != hash.end()) { + if (i.key().startsWith("_")) + hash.erase(i); + ++i; +} +//! [21] + + +//! [22] +if (i.key() == "Hello") + i.value() = "Bonjour"; +//! [22] + + +//! [23] +QHash hash; +hash.insert("January", 1); +hash.insert("February", 2); +... +hash.insert("December", 12); + +QHash::const_iterator i; +for (i = hash.constBegin(); i != hash.constEnd(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [23] + + +//! [24] +QMultiHash hash1, hash2, hash3; + +hash1.insert("plenty", 100); +hash1.insert("plenty", 2000); +// hash1.size() == 2 + +hash2.insert("plenty", 5000); +// hash2.size() == 1 + +hash3 = hash1 + hash2; +// hash3.size() == 3 +//! [24] + + +//! [25] +QList values = hash.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [25] + + +//! [26] +QMultiHash::iterator i = hash.find("plenty"); +while (i != hash.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [26] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp new file mode 100644 index 0000000000..b9861f16c0 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QLinkedList integerList; +QLinkedList timeList; +//! [0] + + +//! [1] +QLinkedList list; +list << "one" << "two" << "three"; +// list: ["one", "two", "three"] +//! [1] + + +//! [2] +QLinkedList list; +... +while (!list.isEmpty()) + delete list.takeFirst(); +//! [2] + + +//! [3] +QLinkedList list; +list.append("one"); +list.append("two"); +list.append("three"); +// list: ["one", "two", "three"] +//! [3] + + +//! [4] +QLinkedList list; +list.prepend("one"); +list.prepend("two"); +list.prepend("three"); +// list: ["three", "two", "one"] +//! [4] + + +//! [5] +QList list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeAll("sun"); +// list: ["cloud", "rain"] +//! [5] + + +//! [6] +QList list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeOne("sun"); +// list: ["cloud", "sun", "rain"] +//! [6] + + +//! [7] +QLinkedList list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QLinkedList::iterator i; +for (i = list.begin(); i != list.end(); ++i) + cout << *i << endl; +//! [7] + + +//! [8] +QLinkedList list; +... +QLinkedList::iterator it = qFind(list.begin(), + list.end(), "Joel"); +if (it != list.end()) + cout << "Found Joel" << endl; +//! [8] + + +//! [9] +QLinkedList::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i += 2; +//! [9] + + +//! [10] +QLinkedList list; +... +QLinkedList::iterator i = list.begin(); +while (i != list.end()) { + if ((*i).startsWith("_")) + i = list.erase(i); + else + ++i; +} +//! [10] + + +//! [11] +QLinkedList::iterator i = list.begin(); +while (i != list.end()) { + QLinkedList::iterator previous = i; + ++i; + if ((*previous).startsWith("_")) + list.erase(previous); +} +//! [11] + + +//! [12] +// WRONG +while (i != list.end()) { + if ((*i).startsWith("_")) + list.erase(i); + ++i; +} +//! [12] + + +//! [13] +if (*it == "Hello") + *it = "Bonjour"; +//! [13] + + +//! [14] +QLinkedList list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QLinkedList::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + cout << *i << endl; +//! [14] + + +//! [15] +QLinkedList list; +... +QLinkedList::iterator it = qFind(list.constBegin(), + list.constEnd(), "Joel"); +if (it != list.constEnd()) + cout << "Found Joel" << endl; +//! [15] + + +//! [16] +std::list stdlist; +list.push_back(1.2); +list.push_back(0.5); +list.push_back(3.14); + +QLinkedList list = QLinkedList::fromStdList(stdlist); +//! [16] + + +//! [17] +QLinkedList list; +list << 1.2 << 0.5 << 3.14; + +std::list stdlist = list.toStdList(); +//! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp new file mode 100644 index 0000000000..dca5473cd6 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp @@ -0,0 +1,267 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QList integerList; +QList dateList; +//! [0] + + +//! [1] +QList list; +list << "one" << "two" << "three"; +// list: ["one", "two", "three"] +//! [1] + + +//! [2] +if (list[0] == "Bob") + list[0] = "Robert"; +//! [2] + + +//! [3] +for (int i = 0; i < list.size(); ++i) { + if (list.at(i) == "Jane") + cout << "Found Jane at position " << i << endl; +} +//! [3] + + +//! [4] +QList list; +... +while (!list.isEmpty()) + delete list.takeFirst(); +//! [4] + + +//! [5] +int i = list.indexOf("Jane"); +if (i != -1) + cout << "First occurrence of Jane is at position " << i << endl; +//! [5] + + +//! [6] +QList list; +list.append("one"); +list.append("two"); +list.append("three"); +// list: ["one", "two", "three"] +//! [6] + + +//! [7] +QList list; +list.prepend("one"); +list.prepend("two"); +list.prepend("three"); +// list: ["three", "two", "one"] +//! [7] + + +//! [8] +QList list; +list << "alpha" << "beta" << "delta"; +list.insert(2, "gamma"); +// list: ["alpha", "beta", "gamma", "delta"] +//! [8] + + +//! [9] +QList list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeAll("sun"); +// list: ["cloud", "rain"] +//! [9] + + +//! [10] +QList list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeOne("sun"); +// list: ["cloud", ,"sun", "rain"] +//! [10] + + +//! [11] +QList list; +list << "A" << "B" << "C" << "D" << "E" << "F"; +list.move(1, 4); +// list: ["A", "C", "D", "E", "B", "F"] +//! [11] + + +//! [12] +QList list; +list << "A" << "B" << "C" << "D" << "E" << "F"; +list.swap(1, 4); +// list: ["A", "E", "C", "D", "B", "F"] +//! [12] + + +//! [13] +QList list; +list << "A" << "B" << "C" << "B" << "A"; +list.indexOf("B"); // returns 1 +list.indexOf("B", 1); // returns 1 +list.indexOf("B", 2); // returns 3 +list.indexOf("X"); // returns -1 +//! [13] + + +//! [14] +QList list; +list << "A" << "B" << "C" << "B" << "A"; +list.lastIndexOf("B"); // returns 3 +list.lastIndexOf("B", 3); // returns 3 +list.lastIndexOf("B", 2); // returns 1 +list.lastIndexOf("X"); // returns -1 +//! [14] + + +//! [15] +QList list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QList::iterator i; +for (i = list.begin(); i != list.end(); ++i) + cout << *i << endl; +//! [15] + + +//! [16] +QList::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i += 2; +//! [16] + + +//! [17] +QList list; +... +qDeleteAll(list.begin(), list.end()); +//! [17] + + +//! [18] +if (*it == "Hello") + *it = "Bonjour"; +//! [18] + + +//! [19] +QList list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QList::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + cout << *i << endl; +//! [19] + + +//! [20] +QList list; +... +qDeleteAll(list.constBegin(), list.constEnd()); +//! [20] + + +//! [21] +QVector vect; +vect << 20.0 << 30.0 << 40.0 << 50.0; + +QList list = QVector::fromVector(vect); +// list: [20.0, 30.0, 40.0, 50.0] +//! [21] + + +//! [22] +QStringList list; +list << "Sven" << "Kim" << "Ola"; + +QVector vect = list.toVector(); +// vect: ["Sven", "Kim", "Ola"] +//! [22] + + +//! [23] +QSet set; +set << 20 << 30 << 40 << ... << 70; + +QList list = QList::fromSet(set); +qSort(list); +//! [23] + + +//! [24] +QStringList list; +list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; + +QSet set = list.toSet(); +set.contains("Julia"); // returns true +set.contains("Mike"); // returns true +set.size(); // returns 2 +//! [24] + + +//! [25] +std::list stdlist; +list.push_back(1.2); +list.push_back(0.5); +list.push_back(3.14); + +QList list = QList::fromStdList(stdlist); +//! [25] + + +//! [26] +QList list; +list << 1.2 << 0.5 << 3.14; + +std::list stdlist = list.toStdList(); +//! [26] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp new file mode 100644 index 0000000000..2e3b599910 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QLocale egyptian(QLocale::Arabic, QLocale::Egypt); +QString s1 = egyptian.toString(1.571429E+07, 'e'); +QString s2 = egyptian.toString(10); + +double d = egyptian.toDouble(s1); +int i = egyptian.toInt(s2); +//! [0] + + +//! [1] +QLocale::setDefault(QLocale(QLocale::Hebrew, QLocale::Israel)); +QLocale hebrew; // Constructs a default QLocale +QString s1 = hebrew.toString(15714.3, 'e'); + +bool ok; +double d; + +QLocale::setDefault(QLocale::C); +d = QString("1234,56").toDouble(&ok); // ok == false +d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56 + +QLocale::setDefault(QLocale::German); +d = QString("1234,56").toDouble(&ok); // ok == true, d == 1234.56 +d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56 + +QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); +str = QString("%1 %L2 %L3") + .arg(12345).arg(12345).arg(12345, 0, 16); +// str == "12345 12,345 3039" +//! [1] + + +//! [2] +QLocale korean("ko"); +QLocale swiss("de_CH"); +//! [2] + + +//! [3] +bool ok; +double d; + +QLocale c(QLocale::C); +d = c.toDouble( "1234.56", &ok ); // ok == true, d == 1234.56 +d = c.toDouble( "1,234.56", &ok ); // ok == true, d == 1234.56 +d = c.toDouble( "1234,56", &ok ); // ok == false + +QLocale german(QLocale::German); +d = german.toDouble( "1234,56", &ok ); // ok == true, d == 1234.56 +d = german.toDouble( "1.234,56", &ok ); // ok == true, d == 1234.56 +d = german.toDouble( "1234.56", &ok ); // ok == false + +d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0 +//! [3] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp new file mode 100644 index 0000000000..2b9c1fb73e --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp @@ -0,0 +1,313 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QMap map; +//! [0] + + +//! [1] +map["one"] = 1; +map["three"] = 3; +map["seven"] = 7; +//! [1] + + +//! [2] +map.insert("twelve", 12); +//! [2] + + +//! [3] +int num1 = map["thirteen"]; +int num2 = map.value("thirteen"); +//! [3] + + +//! [4] +int timeout = 30; +if (map.contains("TIMEOUT")) + timeout = map.value("TIMEOUT"); +//! [4] + + +//! [5] +int timeout = map.value("TIMEOUT", 30); +//! [5] + + +//! [6] +// WRONG +QMap map; +... +for (int i = 0; i < 1000; ++i) { + if (map[i] == okButton) + cout << "Found button at index " << i << endl; +} +//! [6] + + +//! [7] +QMapIterator i(map); +while (i.hasNext()) { + i.next(); + cout << i.key() << ": " << i.value() << endl; +} +//! [7] + + +//! [8] +QMap::const_iterator i = map.constBegin(); +while (i != map.constEnd()) { + cout << i.key() << ": " << i.value() << endl; + ++i; +} +//! [8] + + +//! [9] +map.insert("plenty", 100); +map.insert("plenty", 2000); +// map.value("plenty") == 2000 +//! [9] + + +//! [10] +QList values = map.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [10] + + +//! [11] +QMap::iterator i = map.find("plenty"); +while (i != map.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [11] + + +//! [12] +QMap map; +... +foreach (int value, map) + cout << value << endl; +//! [12] + + +//! [13] +#ifndef EMPLOYEE_H +#define EMPLOYEE_H + +class Employee +{ +public: + Employee() {} + Employee(const QString &name, const QDate &dateOfBirth); + ... + +private: + QString myName; + QDate myDateOfBirth; +}; + +inline bool operator<(const Employee &e1, const Employee &e2) +{ + if (e1.name() != e2.name()) + return e1.name() < e2.name(); + return e1.dateOfBirth() < e2.dateOfBirth(); +} + +#endif // EMPLOYEE_H +//! [13] + + +//! [14] +QMap map; +... +QMap::const_iterator i = map.find("HDR"); +while (i != map.end() && i.key() == "HDR") { + cout << i.value() << endl; + ++i; +} +//! [14] + + +//! [15] +QMap map; +map.insert(1, "one"); +map.insert(5, "five"); +map.insert(10, "ten"); + +map.lowerBound(0); // returns iterator to (1, "one") +map.lowerBound(1); // returns iterator to (1, "one") +map.lowerBound(2); // returns iterator to (5, "five") +map.lowerBound(10); // returns iterator to (10, "ten") +map.lowerBound(999); // returns end() +//! [15] + + +//! [16] +QMap map; +... +QMap::const_iterator i = map.lowerBound("HDR"); +QMap::const_iterator upperBound = map.upperBound("HDR"); +while (i != upperBound) { + cout << i.value() << endl; + ++i; +} +//! [16] + + +//! [17] +QMap map; +map.insert(1, "one"); +map.insert(5, "five"); +map.insert(10, "ten"); + +map.upperBound(0); // returns iterator to (1, "one") +map.upperBound(1); // returns iterator to (5, "five") +map.upperBound(2); // returns iterator to (5, "five") +map.upperBound(10); // returns end() +map.upperBound(999); // returns end() +//! [17] + + +//! [18] +QMap map; +map.insert("January", 1); +map.insert("February", 2); +... +map.insert("December", 12); + +QMap::iterator i; +for (i = map.begin(); i != map.end(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [18] + + +//! [19] +QMap::iterator i; +for (i = map.begin(); i != map.end(); ++i) + i.value() += 2; +//! [19] + + +//! [20] +QMap::iterator i = map.begin(); +while (i != map.end()) { + if (i.key().startsWith("_")) + i = map.erase(i); + else + ++i; +} +//! [20] + + +//! [21] +QMap::iterator i = map.begin(); +while (i != map.end()) { + QMap::iterator prev = i; + ++i; + if (prev.key().startsWith("_")) + map.erase(prev); +} +//! [21] + + +//! [22] +// WRONG +while (i != map.end()) { + if (i.key().startsWith("_")) + map.erase(i); + ++i; +} +//! [22] + + +//! [23] +if (i.key() == "Hello") + i.value() = "Bonjour"; +//! [23] + + +//! [24] +QMap map; +map.insert("January", 1); +map.insert("February", 2); +... +map.insert("December", 12); + +QMap::const_iterator i; +for (i = map.constBegin(); i != map.constEnd(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [24] + + +//! [25] +QMultiMap map1, map2, map3; + +map1.insert("plenty", 100); +map1.insert("plenty", 2000); +// map1.size() == 2 + +map2.insert("plenty", 5000); +// map2.size() == 1 + +map3 = map1 + map2; +// map3.size() == 3 +//! [25] + + +//! [26] +QList values = map.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [26] + + +//! [27] +QMultiMap::iterator i = map.find("plenty"); +while (i != map.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [27] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp new file mode 100644 index 0000000000..4f48fcbcb1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QPoint p; + +p.setX(p.x() + 1); +p += QPoint(1, 0); +p.rx()++; +//! [0] + + +//! [1] +QPoint p(1, 2); +p.rx()--; // p becomes (0, 2) +//! [1] + + +//! [2] +QPoint p(1, 2); +p.ry()++; // p becomes (1, 3) +//! [2] + + +//! [3] +QPoint p( 3, 7); +QPoint q(-1, 4); +p += q; // p becomes (2, 11) +//! [3] + + +//! [4] +QPoint p( 3, 7); +QPoint q(-1, 4); +p -= q; // p becomes (4, 3) +//! [4] + + +//! [5] +QPoint p(-1, 4); +p *= 2.5; // p becomes (-3, 10) +//! [5] + + +//! [6] +QPoint p(-3, 10); +p /= 2.5; // p becomes (-1, 4) +//! [6] + + +//! [7] +QPoint oldPosition; + +MyWidget::mouseMoveEvent(QMouseEvent *event) +{ + QPoint point = event->pos() - oldPosition; + if (point.manhattanLength() > 3) + // the mouse has moved more than 3 pixels since the oldPosition +} +//! [7] + + +//! [8] +double trueLength = sqrt(pow(x(), 2) + pow(y(), 2)); +//! [8] + + +//! [9] +QPointF p; + +p.setX(p.x() + 1.0); +p += QPointF(1.0, 0.0); +p.rx()++; +//! [9] + + +//! [10] + QPointF p(1.1, 2.5); + p.rx()--; // p becomes (0.1, 2.5) +//! [10] + + +//! [11] +QPointF p(1.1, 2.5); +p.ry()++; // p becomes (1.1, 3.5) +//! [11] + + +//! [12] +QPointF p( 3.1, 7.1); +QPointF q(-1.0, 4.1); +p += q; // p becomes (2.1, 11.2) +//! [12] + + +//! [13] +QPointF p( 3.1, 7.1); +QPointF q(-1.0, 4.1); +p -= q; // p becomes (4.1, 3.0) +//! [13] + + +//! [14] +QPointF p(-1.1, 4.1); +p *= 2.5; // p becomes (-2.75, 10.25) +//! [14] + + +//! [15] +QPointF p(-2.75, 10.25); +p /= 2.5; // p becomes (-1.1, 4.1) +//! [15] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp new file mode 100644 index 0000000000..3a1cf7c1a1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QQueue queue; +queue.enqueue(1); +queue.enqueue(2); +queue.enqueue(3); +while (!queue.isEmpty()) + cout << queue.dequeue() << endl; +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp new file mode 100644 index 0000000000..ebf9118c20 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QRect r1(100, 200, 11, 16); +QRect r2(QPoint(100, 200), QSize(11, 16)); +//! [0] + + +//! [1] +QRectF r1(100, 200, 11, 16); +QRectF r2(QPoint(100, 200), QSize(11, 16)); +//! [1] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp new file mode 100644 index 0000000000..002ae96f23 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QRegExp rx("(\\d+)"); +QString str = "Offsets: 12 14 99 231 7"; +QStringList list; +int pos = 0; + +while ((pos = rx.indexIn(str, pos)) != -1) { + list << rx.cap(1); + pos += rx.matchedLength(); +} +// list: ["12", "14", "99", "231", "7"] +//! [0] + + +//! [1] +QRegExp rx("*.txt"); +rx.setPatternSyntax(QRegExp::Wildcard); +rx.exactMatch("README.txt"); // returns true +rx.exactMatch("welcome.txt.bak"); // returns false +//! [1] + + +//! [2] +QRegExp rx("ro+m"); +rx.setMinimal(true); +//! [2] + + +//! [3] +QRegExp mark("\\b" // word boundary + "[Mm]ark" // the word we want to match + ); +//! [3] + + +//! [4] +QRegExp rx("^\\d\\d?$"); // match integers 0 to 99 +rx.indexIn("123"); // returns -1 (no match) +rx.indexIn("-6"); // returns -1 (no match) +rx.indexIn("6"); // returns 0 (matched as position 0) +//! [4] + + +//! [5] +QRegExp rx("^\\S+$"); // match strings without whitespace +rx.indexIn("Hello world"); // returns -1 (no match) +rx.indexIn("This_is-OK"); // returns 0 (matched at position 0) +//! [5] + + +//! [6] +QRegExp rx("\\b(mail|letter|correspondence)\\b"); +rx.indexIn("I sent you an email"); // returns -1 (no match) +rx.indexIn("Please write the letter"); // returns 17 +//! [6] + + +//! [7] +QString captured = rx.cap(1); // captured == "letter" +//! [7] + + +//! [8] +QRegExp rx("&(?!amp;)"); // match ampersands but not & +QString line1 = "This & that"; +line1.replace(rx, "&"); +// line1 == "This & that" +QString line2 = "His & hers & theirs"; +line2.replace(rx, "&"); +// line2 == "His & hers & theirs" +//! [8] + + +//! [9] +QString str = "One Eric another Eirik, and an Ericsson. " + "How many Eiriks, Eric?"; +QRegExp rx("\\b(Eric|Eirik)\\b"); // match Eric or Eirik +int pos = 0; // where we are in the string +int count = 0; // how many Eric and Eirik's we've counted +while (pos >= 0) { + pos = rx.indexIn(str, pos); + if (pos >= 0) { + ++pos; // move along in str + ++count; // count our Eric or Eirik + } +} +//! [9] + + +//! [10] +str = "Nokia Corporation\tqt.nokia.com\tNorway"; +QString company, web, country; +rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$"); +if (rx.indexIn(str) != -1) { + company = rx.cap(1); + web = rx.cap(2); + country = rx.cap(3); +} +//! [10] + + +//! [11] +QStringList field = str.split("\t"); +//! [11] + + +//! [12] +QRegExp rx("*.html"); +rx.setPatternSyntax(QRegExp::Wildcard); +rx.exactMatch("index.html"); // returns true +rx.exactMatch("default.htm"); // returns false +rx.exactMatch("readme.txt"); // returns false +//! [12] + + +//! [13] +QString str = "offsets: 1.23 .50 71.00 6.00"; +QRegExp rx("\\d*\\.\\d+"); // primitive floating point matching +int count = 0; +int pos = 0; +while ((pos = rx.indexIn(str, pos)) != -1) { + ++count; + pos += rx.matchedLength(); +} +// pos will be 9, 14, 18 and finally 24; count will end up as 4 +//! [13] + + +//! [14] +QRegExp rx("(\\d+)(\\s*)(cm|inch(es)?)"); +int pos = rx.indexIn("Length: 36 inches"); +QStringList list = rx.capturedTexts(); +// list is now ("36 inches", "36", " ", "inches", "es") +//! [14] + + +//! [15] +QRegExp rx("(\\d+)(?:\\s*)(cm|inch(?:es)?)"); +int pos = rx.indexIn("Length: 36 inches"); +QStringList list = rx.capturedTexts(); +// list is now ("36 inches", "36", "inches") +//! [15] + + +//! [16] +QStringList list = rx.capturedTexts(); +QStringList::iterator it = list.begin(); +while (it != list.end()) { + myProcessing(*it); + ++it; +} +//! [16] + + +//! [17] +QRegExp rxlen("(\\d+)(?:\\s*)(cm|inch)"); +int pos = rxlen.indexIn("Length: 189cm"); +if (pos > -1) { + QString value = rxlen.cap(1); // "189" + QString unit = rxlen.cap(2); // "cm" + // ... +} +//! [17] + + +//! [18] +QRegExp rx("/([a-z]+)/([a-z]+)"); +rx.indexIn("Output /dev/null"); // returns 7 (position of /dev/null) +rx.pos(0); // returns 7 (position of /dev/null) +rx.pos(1); // returns 8 (position of dev) +rx.pos(2); // returns 12 (position of null) +//! [18] + + +//! [19] +s1 = QRegExp::escape("bingo"); // s1 == "bingo" +s2 = QRegExp::escape("f(x)"); // s2 == "f\\(x\\)" +//! [19] + + +//! [20] +QRegExp rx("(" + QRegExp::escape(name) + + "|" + QRegExp::escape(alias) + ")"); +//! [20] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp new file mode 100644 index 0000000000..cab89d9c9e --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp @@ -0,0 +1,289 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Giuseppe D'Angelo . +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QRegularExpression re("a pattern"); +//! [0] + + +//! [1] +QRegularExpression re; +re.setPattern("another pattern"); +//! [1] + + +//! [2] +// matches two digits followed by a space and a word +QRegularExpression re("\\d\\d \\w+"); + +// matches a backslash +QRegularExpression re2("\\\\"); +//! [2] + + +//! [3] +QRegularExpression re("a third pattern"); +QString pattern = re.pattern(); // pattern == "a third pattern" +//! [3] + + +//! [4] +// matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc. +QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption); +//! [4] + + +//! [5] +QRegularExpression re("^\\d+$"); +re.setPatternOptions(QRegularExpression::MultilineOption); +// re matches any line in the subject string that contains only digits (but at least one) +//! [5] + + +//! [6] +QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption + | QRegularExpression::DotMatchesEverythingOption); + +QRegularExpression::PatternOptions options = re.patternOptions(); +// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption +//! [6] + + +//! [7] +// match two digits followed by a space and a word +QRegularExpression re("\\d\\d \\w+"); +QRegularExpressionMatch match = re.match("abc123 def"); +bool hasMatch = match.hasMatch(); // true +//! [7] + + +//! [8] +QRegularExpression re("\\d\\d \\w+"); +QRegularExpressionMatch match = re.match("abc123 def"); +if (match.hasMatch()) { + QString matched = match.captured(0); // matched == "23 def" + // ... +} +//! [8] + + +//! [9] +QRegularExpression re("\\d\\d \\w+"); +QRegularExpressionMatch match = re.match("12 abc 45 def", 1); +if (match.hasMatch()) { + QString matched = match.captured(0); // matched == "45 def" + // ... +} +//! [9] + + +//! [10] +QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$"); +QRegularExpressionMatch match = re.match("08/12/1985"); +if (match.hasMatch()) { + QString day = re.captured(1); // day == "08" + QString month = re.captured(2); // month == "12" + QString year = re.captured(3); // year == "1985" + // ... +} +//! [10] + + +//! [11] +QRegularExpression re("abc(\\d+)def"); +QRegularExpressionMatch match = re.match("XYZabc123defXYZ"); +if (match.hasMatch()) { + int startOffset = re.capturedStart(1); // startOffset == 6 + int endOffset = re.capturedEnd(1); // endOffset == 9 + // ... +} +//! [11] + + +//! [12] +QRegularExpression re("^(?\\d\\d)/(?\\d\\d)/(?\\d\\d\\d\\d)$"); +QRegularExpressionMatch match = re.match("08/12/1985"); +if (match.hasMatch()) { + QString date = match.captured("date"); // date == "08" + QString month = match.captured("month"); // month == "12" + QString year = match.captured("year"); // year == 1985 +} +//! [12] + + +//! [13] +QRegularExpression re("(\\w+)"); +QRegularExpressionMatchIterator i = re.globalMatch("the quick fox"); +//! [13] + + +//! [14] +QStringList words; +while (i.hasNext()) { + QRegularExpressionMatch match = i.next(); + QString word = match.captured(1); + words << word; +} +// words contains "the", "quick", "fox" +//! [14] + + +//! [15] +QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$"); +QRegularExpression re(pattern); + +QString input("Jan 21,"); +QRegularExpressionMatch match = re.match(input, 0, QRegularExpressionMatch::PartialPreferCompleteMatch); +bool hasMatch = match.hasMatch(); // false +bool hasPartialMatch = match.hasPartialMatch(); // true +//! [15] + + +//! [16] +QString input("Dec 8, 1985"); +QRegularExpressionMatch match = re.match(input, 0, QRegularExpressionMatch::PartialPreferCompleteMatch); +bool hasMatch = match.hasMatch(); // true +bool hasPartialMatch = match.hasPartialMatch(); // false +//! [16] + + +//! [17] +QRegularExpression re("abc\\w+X|def"); +QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpressionMatch::PartialPreferCompleteMatch); +bool hasMatch = match.hasMatch(); // true +bool hasPartialMatch = match.hasPartialMatch(); // false +QString captured = match.captured(0); // captured == "def" +//! [17] + + +//! [18] +QRegularExpression re("abc\\w+X|defY"); +QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpressionMatch::PartialPreferCompleteMatch); +bool hasMatch = match.hasMatch(); // false +bool hasPartialMatch = match.hasPartialMatch(); // true +QString captured = match.captured(0); // captured == "abcdef" +//! [18] + + +//! [19] +QRegularExpression re("abc|ab"); +QRegularExpressionMatch match = re.match("ab", 0, QRegularExpressionMatch::PartialPreferFirstMatch); +bool hasMatch = match.hasMatch(); // false +bool hasPartialMatch = match.hasPartialMatch(); // true +//! [19] + + +//! [20] +QRegularExpression re("abc(def)?"); +QRegularExpressionMatch match = re.match("abc", 0, QRegularExpressionMatch::PartialPreferFirstMatch); +bool hasMatch = match.hasMatch(); // false +bool hasPartialMatch = match.hasPartialMatch(); // true +//! [20] + +//! [21] +QRegularExpression re("(abc)*"); +QRegularExpressionMatch match = re.match("abc", 0, QRegularExpressionMatch::PartialPreferFirstMatch); +bool hasMatch = match.hasMatch(); // false +bool hasPartialMatch = match.hasPartialMatch(); // true +//! [21] + +//! [22] +QRegularExpression invalidRe("(unmatched|parenthesis"); +bool isValid = invalidRe.isValid(); // false +//! [22] + +//! [23] +QRegularExpression invalidRe("(unmatched|parenthesis"); +if (!invalidRe.isValid()) { + QString errorString = invalidRe.errorString(); // errorString == "missing )" + int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22 + // ... +} +//! [23] + +//! [24] +QRegularExpression re("^this pattern must match exactly$"); +//! [24] + +//! [25] +QString p("a .*|pattern"); +QRegularExpression re("\\A(?:" + p + ")\\z"); // re matches exactly the pattern string p +//! [25] + +//! [26] +QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)"); +// escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)" +//! [26] + +//! [27] +QString pattern = "(" + QRegularExpression::escape(name) + + "|" + QRegularExpression::escape(nickname) + ")"; +QRegularExpression re(pattern); +//! [27] + +//! [28] +QRegularExpressionMatch match = re.match(...); +for (int i = 0; i <= match.lastCapturedIndex(); ++i) { + QString captured = match.captured(i); + // ... +} +//! [28] + +//! [29] +QRegularExpression("(\d\d) (?\w+)"); +QRegularExpressionMatch match = re.match("23 Jordan"); +if (match.hasMatch()) { + QString number = match.captured(1); // first == "23" + QString name = match.captured("name"); // name == "Jordan" +} +//! [29] + +//! [30] +// extracts the words +QRegularExpression re("(\w+)"); +QString subject("the quick fox"); +QRegularExpressionMatchIterator i = re.globalMatch(subject); +while (i.hasNext()) { + QRegularExpressionMatch match = i.next(); + // ... +} +//! [30] + + diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp new file mode 100644 index 0000000000..fd9cd3e4b0 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +void myFunction(bool useSubClass) +{ + MyClass *p = useSubClass ? new MyClass() : new MySubClass; + QIODevice *device = handsOverOwnership(); + + if (m_value > 3) { + delete p; + delete device; + return; + } + + try { + process(device); + } + catch (...) { + delete p; + delete device; + throw; + } + + delete p; + delete device; +} +//! [0] + +//! [1] +void myFunction(bool useSubClass) +{ + // assuming that MyClass has a virtual destructor + QScopedPointer p(useSubClass ? new MyClass() : new MySubClass); + QScopedPointer device(handsOverOwnership()); + + if (m_value > 3) + return; + + process(device); +} +//! [1] + +//! [2] + const QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer p(new QWidget()); + + QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer p(new QWidget()); + + const QWidget *p = new QWidget(); + // is equivalent to: + QScopedPointer p(new QWidget()); +//! [2] + +//! [3] +if (scopedPointer) { + ... +} +//! [3] + +//! [4] +class MyPrivateClass; // forward declare MyPrivateClass + +class MyClass +{ +private: + QScopedPointer privatePtr; // QScopedPointer to forward declared class + +public: + MyClass(); // OK + inline ~MyClass() {} // VIOLATION - Destructor must not be inline + +private: + Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators + // are now disabled, so the compiler won't implicitely + // generate them. +}; +//! [4] + +//! [5] +// this QScopedPointer deletes its data using the delete[] operator: +QScopedPointer > arrayPointer(new int[42]); + +// this QScopedPointer frees its data using free(): +QScopedPointer podPointer(reinterpret_cast(malloc(42))); + +// this struct calls "myCustomDeallocator" to delete the pointer +struct ScopedPointerCustomDeleter +{ + static inline void cleanup(MyCustomClass *pointer) + { + myCustomDeallocator(pointer); + } +}; + +// QScopedPointer using a custom deleter: +QScopedPointer customPointer(new MyCustomClass); +//! [5] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp new file mode 100644 index 0000000000..8772152ad6 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QSize t1(10, 12); +t1.scale(60, 60, Qt::IgnoreAspectRatio); +// t1 is (60, 60) + +QSize t2(10, 12); +t2.scale(60, 60, Qt::KeepAspectRatio); +// t2 is (50, 60) + +QSize t3(10, 12); +t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); +// t3 is (60, 72) +//! [0] + + +//! [1] +QSize size(100, 10); +size.rwidth() += 20; + +// size becomes (120,10) +//! [1] + + +//! [2] +QSize size(100, 10); +size.rheight() += 5; + +// size becomes (100,15) +//! [2] + + +//! [3] +QSize s( 3, 7); +QSize r(-1, 4); +s += r; + +// s becomes (2,11) +//! [3] + + +//! [4] +QSize s( 3, 7); +QSize r(-1, 4); +s -= r; + +// s becomes (4,3) +//! [4] + + +//! [5] +QSizeF t1(10, 12); +t1.scale(60, 60, Qt::IgnoreAspectRatio); +// t1 is (60, 60) + +QSizeF t2(10, 12); +t2.scale(60, 60, Qt::KeepAspectRatio); +// t2 is (50, 60) + +QSizeF t3(10, 12); +t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); +// t3 is (60, 72) +//! [5] + + +//! [6] +QSizeF size(100.3, 10); +size.rwidth() += 20.5; + + // size becomes (120.8,10) +//! [6] + + +//! [7] +QSizeF size(100, 10.2); +size.rheight() += 5.5; + +// size becomes (100,15.7) +//! [7] + + +//! [8] +QSizeF s( 3, 7); +QSizeF r(-1, 4); +s += r; + +// s becomes (2,11) +//! [8] + + +//! [9] +QSizeF s( 3, 7); +QSizeF r(-1, 4); +s -= r; + +// s becomes (4,3) +//! [9] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qstring.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qstring.cpp new file mode 100644 index 0000000000..507e929f47 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qstring.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +DEFINES += QT_NO_CAST_FROM_ASCII \ + QT_NO_CAST_TO_ASCII +//! [0] + + +//! [1] +QString url = QLatin1String("http://www.unicode.org/"); +//! [1] + + +//! [2] +double d = 12.34; +QString str = QString("delta: %1").arg(d, 0, 'E', 3); +// str == "delta: 1.234E+01" +//! [2] + + +//! [3] +if (str == "auto" || str == "extern" + || str == "static" || str == "register") { + ... +} +//! [3] + + +//! [4] +if (str == QString("auto") || str == QString("extern") + || str == QString("static") || str == QString("register")) { + ... +} +//! [4] + + +//! [5] +if (str == QLatin1String("auto") + || str == QLatin1String("extern") + || str == QLatin1String("static") + || str == QLatin1String("register") { + ... +} +//! [5] + + +//! [6] +QLabel *label = new QLabel(QLatin1String("MOD"), this); +//! [6] + + +//! [7] +QString plain = "#include " +QString html = plain.toHtmlEscaped(); +// html == "#include <QtCore>" +//! [7] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp new file mode 100644 index 0000000000..e33c94efda --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +... +progressBar = new QProgressBar(this); +progressBar->setRange(0, 100); + +// Construct a 1-second timeline with a frame range of 0 - 100 +QTimeLine *timeLine = new QTimeLine(1000, this); +timeLine->setFrameRange(0, 100); +connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int))); + +// Clicking the push button will start the progress bar animation +pushButton = new QPushButton(tr("Start animation"), this); +connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start())); +... +//! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp new file mode 100644 index 0000000000..b3f9a7e675 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp @@ -0,0 +1,183 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QVector integerVector; +QVector stringVector; +//! [0] + + +//! [1] +QVector vector(200); +//! [1] + + +//! [2] +QVector vector(200, "Pass"); +//! [2] + + +//! [3] +if (vector[0] == "Liz") + vector[0] = "Elizabeth"; +//! [3] + + +//! [4] +for (int i = 0; i < vector.size(); ++i) { + if (vector.at(i) == "Alfonso") + cout << "Found Alfonso at position " << i << endl; +} +//! [4] + + +//! [5] +int i = vector.indexOf("Harumi"); +if (i != -1) + cout << "First occurrence of Harumi is at position " << i << endl; +//! [5] + + +//! [6] +QVector vector(10); +int *data = vector.data(); +for (int i = 0; i < 10; ++i) + data[i] = 2 * i; +//! [6] + + +//! [7] +QVector vector(0); +vector.append("one"); +vector.append("two"); +vector.append("three"); +// vector: ["one", "two", "three"] +//! [7] + + +//! [8] +QVector vector; +vector.prepend("one"); +vector.prepend("two"); +vector.prepend("three"); +// vector: ["three", "two", "one"] +//! [8] + + +//! [9] +QVector vector; +vector << "alpha" << "beta" << "delta"; +vector.insert(2, "gamma"); +// vector: ["alpha", "beta", "gamma", "delta"] +//! [9] + + +//! [10] +QVector vector; +vector << 2.718 << 1.442 << 0.4342; +vector.insert(1, 3, 9.9); +// vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] +//! [10] + + +//! [11] +QVector vector(3); +vector.fill("Yes"); +// vector: ["Yes", "Yes", "Yes"] + +vector.fill("oh", 5); +// vector: ["oh", "oh", "oh", "oh", "oh"] +//! [11] + + +//! [12] +QVector vector; +vector << "A" << "B" << "C" << "B" << "A"; +vector.indexOf("B"); // returns 1 +vector.indexOf("B", 1); // returns 1 +vector.indexOf("B", 2); // returns 3 +vector.indexOf("X"); // returns -1 +//! [12] + + +//! [13] +QList vector; +vector << "A" << "B" << "C" << "B" << "A"; +vector.lastIndexOf("B"); // returns 3 +vector.lastIndexOf("B", 3); // returns 3 +vector.lastIndexOf("B", 2); // returns 1 +vector.lastIndexOf("X"); // returns -1 +//! [13] + + +//! [14] +QVector vect; +vect << "red" << "green" << "blue" << "black"; + +QList list = vect.toList(); +// list: ["red", "green", "blue", "black"] +//! [14] + + +//! [15] +QStringList list; +list << "Sven" << "Kim" << "Ola"; + +QVector vect = QVector::fromList(list); +// vect: ["Sven", "Kim", "Ola"] +//! [15] + + +//! [16] +std::vector stdvector; +vector.push_back(1.2); +vector.push_back(0.5); +vector.push_back(3.14); + +QVector vector = QVector::fromStdVector(stdvector); +//! [16] + + +//! [17] +QVector vector; +vector << 1.2 << 0.5 << 3.14; + +std::vector stdvector = vector.toStdVector(); +//! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_xml_qxmlstream.cpp b/src/corelib/doc/snippets/code/src_corelib_xml_qxmlstream.cpp new file mode 100644 index 0000000000..9f40777df1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_xml_qxmlstream.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] + QXmlStreamReader xml; + ... + while (!xml.atEnd()) { + xml.readNext(); + ... // do processing + } + if (xml.hasError()) { + ... // do error handling + } +//! [0] + + +//! [1] + writeStartElement(qualifiedName); + writeCharacters(text); + writeEndElement(); +//! [1] + + +//! [2] + writeStartElement(namespaceUri, name); + writeCharacters(text); + writeEndElement(); +//! [2] + + diff --git a/src/corelib/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp b/src/corelib/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp new file mode 100644 index 0000000000..90bc2e9b0a --- /dev/null +++ b/src/corelib/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +int ret = QMessageBox::warning(this, tr("My Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel, + QMessageBox::Save); +//! [0] + + +//! [1] +QMessageBox msgBox; +msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); +switch (msgBox.exec()) { +case QMessageBox::Yes: + // yes was clicked + break; +case QMessageBox::No: + // no was clicked + break; +default: + // should never be reached + break; +} +//! [1] + + +//! [2] +QMessageBox msgBox; +QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); +QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); + +msgBox.exec(); + +if (msgBox.clickedButton() == connectButton) { + // connect +} else if (msgBox.clickedButton() == abortButton) { + // abort +} +//! [2] + + +//! [3] +QMessageBox messageBox(this); +QAbstractButton *disconnectButton = + messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole); +... +messageBox.exec(); +if (messageBox.clickedButton() == disconnectButton) { + ... +} +//! [3] + + +//! [4] +#include +#include + +int main(int argc, char *argv[]) +{ + QT_REQUIRE_VERSION(argc, argv, "4.0.2") + + QApplication app(argc, argv); + ... + return app.exec(); +} +//! [4] + +//! [5] +QMessageBox msgBox; +msgBox.setText("The document has been modified."); +msgBox.exec(); +//! [5] + +//! [6] +QMessageBox msgBox; +msgBox.setText("The document has been modified."); +msgBox.setInformativeText("Do you want to save your changes?"); +msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); +msgBox.setDefaultButton(QMessageBox::Save); +int ret = msgBox.exec(); +//! [6] + +//! [7] +switch (ret) { + case QMessageBox::Save: + // Save was clicked + break; + case QMessageBox::Discard: + // Don't Save was clicked + break; + case QMessageBox::Cancel: + // Cancel was clicked + break; + default: + // should never be reached + break; +} +//! [7] + +//! [9] +QMessageBox msgBox(this); +msgBox.setText(tr("The document has been modified.\n" + "Do you want to save your changes?")); +msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); +msgBox.setDefaultButton(QMessageBox::Save); +//! [9] diff --git a/src/corelib/doc/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp b/src/corelib/doc/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp new file mode 100644 index 0000000000..cf4707cbeb --- /dev/null +++ b/src/corelib/doc/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +class DateFormatProxyModel : public QIdentityProxyModel +{ + // ... + + void setDateFormatString(const QString &formatString) + { + m_formatString = formatString; + } + + QVariant data(const QModelIndex &index, int role) + { + if (role != Qt::DisplayRole) + return QIdentityProxyModel::data(index, role); + + const QDateTime dateTime = sourceModel()->data(SourceClass::DateRole).toDateTime(); + + return dateTime.toString(m_formatString); + } + +private: + QString m_formatString; +}; +//! [0] diff --git a/src/corelib/doc/snippets/code/src_gui_itemviews_qitemselectionmodel.cpp b/src/corelib/doc/snippets/code/src_gui_itemviews_qitemselectionmodel.cpp new file mode 100644 index 0000000000..ba81221e9c --- /dev/null +++ b/src/corelib/doc/snippets/code/src_gui_itemviews_qitemselectionmodel.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QItemSelection *selection = new QItemSelection(topLeft, bottomRight); +//! [0] + + +//! [1] +QItemSelection *selection = new QItemSelection(); +... +selection->select(topLeft, bottomRight); +//! [1] -- cgit v1.2.3