diff options
Diffstat (limited to 'src/corelib/doc/snippets')
114 files changed, 14114 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/buffer/buffer.cpp b/src/corelib/doc/snippets/buffer/buffer.cpp new file mode 100644 index 0000000000..681fb56791 --- /dev/null +++ b/src/corelib/doc/snippets/buffer/buffer.cpp @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** 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 <QApplication> +#include <QBuffer> +#include <QPalette> + +static void main_snippet() +{ +//! [0] + QBuffer buffer; + char ch; + + buffer.open(QBuffer::ReadWrite); + buffer.write("Qt rocks!"); + buffer.seek(0); + buffer.getChar(&ch); // ch == 'Q' + buffer.getChar(&ch); // ch == 't' + buffer.getChar(&ch); // ch == ' ' + buffer.getChar(&ch); // ch == 'r' +//! [0] +} + +static void write_datastream_snippets() +{ +//! [1] + QByteArray byteArray; + QBuffer buffer(&byteArray); + buffer.open(QIODevice::WriteOnly); + + QDataStream out(&buffer); + out << QApplication::palette(); +//! [1] +} + +static void read_datastream_snippets() +{ + QByteArray byteArray; + +//! [2] + QPalette palette; + QBuffer buffer(&byteArray); + buffer.open(QIODevice::ReadOnly); + + QDataStream in(&buffer); + in >> palette; +//! [2] +} + +static void bytearray_ptr_ctor_snippet() +{ +//! [3] + QByteArray byteArray("abc"); + QBuffer buffer(&byteArray); + buffer.open(QIODevice::WriteOnly); + buffer.seek(3); + buffer.write("def", 3); + buffer.close(); + // byteArray == "abcdef" +//! [3] +} + +static void setBuffer_snippet() +{ +//! [4] + QByteArray byteArray("abc"); + QBuffer buffer; + buffer.setBuffer(&byteArray); + buffer.open(QIODevice::WriteOnly); + buffer.seek(3); + buffer.write("def", 3); + buffer.close(); + // byteArray == "abcdef" +//! [4] +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + main_snippet(); + bytearray_ptr_ctor_snippet(); + write_datastream_snippets(); + read_datastream_snippets(); + setBuffer_snippet(); + return 0; +} 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<QString> list; +list << "A" << "B" << "C" << "D"; + +QListIterator<QString> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [1] + + +//! [2] +QListIterator<QString> i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [2] + + +//! [3] +QMutableListIterator<int> i(list); +while (i.hasNext()) { + if (i.next() % 2 != 0) + i.remove(); +} +//! [3] + + +//! [4] +QMutableListIterator<int> i(list); +i.toBack(); +while (i.hasPrevious()) { + if (i.previous() % 2 != 0) + i.remove(); +} +//! [4] + + +//! [5] +QMutableListIterator<int> i(list); +while (i.hasNext()) { + if (i.next() > 128) + i.setValue(128); +} +//! [5] + + +//! [6] +QMutableListIterator<int> i(list); +while (i.hasNext()) + i.next() *= 2; +//! [6] + + +//! [7] +QMap<QString, QString> map; +map.insert("Paris", "France"); +map.insert("Guatemala City", "Guatemala"); +map.insert("Mexico City", "Mexico"); +map.insert("Moscow", "Russia"); +... + +QMutableMapIterator<QString, QString> i(map); +while (i.hasNext()) { + if (i.next().key().endsWith("City")) + i.remove(); +} +//! [7] + + +//! [8] +QMap<int, QWidget *> map; +QHash<int, QWidget *> hash; + +QMapIterator<int, QWidget *> i(map); +while (i.hasNext()) { + i.next(); + hash.insert(i.key(), i.value()); +} +//! [8] + + +//! [9] +QMutableMapIterator<int, QWidget *> i(map); +while (i.findNext(widget)) + i.remove(); +//! [9] + + +//! [10] +QList<QString> list; +list << "A" << "B" << "C" << "D"; + +QList<QString>::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i = (*i).toLower(); +//! [10] + + +//! [11] +QList<QString> list; +list << "A" << "B" << "C" << "D"; + +QList<QString>::iterator i = list.end(); +while (i != list.begin()) { + --i; + *i = (*i).toLower(); +} +//! [11] + + +//! [12] +QList<QString>::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + qDebug() << *i; +//! [12] + + +//! [13] +QMap<int, int> map; +... +QMap<int, int>::const_iterator i; +for (i = map.constBegin(); i != map.constEnd(); ++i) + qDebug() << i.key() << ":" << i.value(); +//! [13] + + +//! [14] +// RIGHT +const QList<int> sizes = splitter->sizes(); +QList<int>::const_iterator i; +for (i = sizes.begin(); i != sizes.end(); ++i) + ... + +// WRONG +QList<int>::const_iterator i; +for (i = splitter->sizes().begin(); + i != splitter->sizes().end(); ++i) + ... +//! [14] + + +//! [15] +QLinkedList<QString> list; +... +QString str; +foreach (str, list) + qDebug() << str; +//! [15] + + +//! [16] +QLinkedList<QString> list; +... +QLinkedListIterator<QString> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [16] + + +//! [17] +QLinkedList<QString> list; +... +foreach (const QString &str, list) + qDebug() << str; +//! [17] + + +//! [18] +QLinkedList<QString> list; +... +foreach (const QString &str, list) { + if (str.isEmpty()) + break; + qDebug() << str; +} +//! [18] + + +//! [19] +QMap<QString, int> map; +... +foreach (const QString &str, map.keys()) + qDebug() << str << ":" << map.value(str); +//! [19] + + +//! [20] +QMultiMap<QString, int> 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; i<count; ++i) { + QMetaProperty metaproperty = metaobject->property(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<QString, int> 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<QString> vect1(3); +qCopy(list.begin(), list.end(), vect1.begin()); +// vect: [ "one", "two", "three" ] + +QVector<QString> vect2(8); +qCopy(list.begin(), list.end(), vect2.begin() + 2); +// vect: [ "", "", "one", "two", "three", "", "", "" ] +//! [4] + + +//! [5] +QStringList list; +list << "one" << "two" << "three"; + +QVector<QString> vect(5); +qCopyBackward(list.begin(), list.end(), vect.end()); +// vect: [ "", "", "one", "two", "three" ] +//! [5] + + +//! [6] +QStringList list; +list << "one" << "two" << "three"; + +QVector<QString> 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<int> 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<int> 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<int> list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qGreater<int>()); +// list: [ 68, 33, 12, 12, 6 ] +//! [13] + + +//! [14] +QStringList list; +list << "AlPha" << "beTA" << "gamma" << "DELTA"; + +QMap<QString, QString> map; +foreach (const QString &str, list) + map.insert(str.toLower(), str); + +list = map.values(); +//! [14] + + +//! [15] +QList<int> 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<int> list; +list << 33 << 12 << 68 << 6 << 12; +qStableSort(list.begin(), list.end(), qGreater<int>()); +// list: [ 68, 33, 12, 12, 6 ] +//! [17] + + +//! [18] +QList<int> list; +list << 3 << 3 << 6 << 6 << 6 << 8; + +QList<int>::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<int> vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; +QVector<int>::iterator begin6 = + qLowerBound(vect.begin(), vect.end(), 6); +QVector<int>::iterator end6 = + qUpperBound(begin6, vect.end(), 6); + +QVector<int>::iterator i = begin6; +while (i != end6) { + *i = 7; + ++i; +} +// vect: [ 3, 3, 7, 7, 7, 8 ] +//! [19] + + +//! [20] +QList<int> list; +list << 3 << 3 << 6 << 6 << 6 << 8; + +QList<int>::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<int> vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; +QVector<int>::iterator begin6 = + qLowerBound(vect.begin(), vect.end(), 6); +QVector<int>::iterator end6 = + qUpperBound(vect.begin(), vect.end(), 6); + +QVector<int>::iterator i = begin6; +while (i != end6) { + *i = 7; + ++i; +} +// vect: [ 3, 3, 7, 7, 7, 8 ] +//! [21] + + +//! [22] +QVector<int> vect; +vect << 3 << 3 << 6 << 6 << 6 << 8; + +QVector<int>::iterator i = + qBinaryFind(vect.begin(), vect.end(), 6); +// i == vect.begin() + 2 (or 3 or 4) +//! [22] + + +//! [23] +QList<Employee *> list; +list.append(new Employee("Blackpool", "Stephen")); +list.append(new Employee("Twist", "Oliver")); + +qDeleteAll(list.begin(), list.end()); +list.clear(); +//! [23] + + +//! [24] +QList<int> list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qLess<int>()); +// list: [ 6, 12, 12, 33, 68 ] +//! [24] + + +//! [25] +QList<int> list; +list << 33 << 12 << 68 << 6 << 12; +qSort(list.begin(), list.end(), qGreater<int>()); +// 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<int, Employee> cache; +//! [0] + + +//! [1] +Employee *employee = new Employee; +employee->setId(37); +employee->setName("Richard Schmit"); +... +cache.insert(employee->id(), employee); +//! [1] + + +//! [2] +QCache<int, MyDataStructure> 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<float> list; +... +QListIterator<float> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [0] + + +//! [1] +QListIterator<float> i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [1] + + +//! [2] +QLinkedList<float> list; +... +QLinkedListIterator<float> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [2] + + +//! [3] +QLinkedListIterator<float> i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [3] + + +//! [4] +QVector<float> vector; +... +QVectorIterator<float> i(vector); +while (i.hasNext()) + qDebug() << i.next(); +//! [4] + + +//! [5] +QVectorIterator<float> i(vector); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [5] + + +//! [6] +QSet<QString> set; +... +QSetIterator<QString> i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [6] + + +//! [7] +QSetIterator<QString> i(set); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [7] + + +//! [8] +QList<float> list; +... +QMutableListIterator<float> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [8] + + +//! [9] +QMutableListIterator<float> i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [9] + + +//! [10] +QMutableListIterator<int> i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [10] + + +//! [11] +QLinkedList<float> list; +... +QMutableLinkedListIterator<float> i(list); +while (i.hasNext()) + qDebug() << i.next(); +//! [11] + + +//! [12] +QMutableLinkedListIterator<float> i(list); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [12] + + +//! [13] +QMutableLinkedListIterator<int> i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [13] + + +//! [14] +QVector<float> vector; +... +QMutableVectorIterator<float> i(vector); +while (i.hasNext()) + qDebug() << i.next(); +//! [14] + + +//! [15] +QMutableVectorIterator<float> i(vector); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [15] + + +//! [16] +QMutableVectorIterator<int> i(vector); +while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } +} +//! [16] + + +//! [17] +QSet<float> set; +... +QMutableSetIterator<float> i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [17] + + +//! [18] +QMutableSetIterator<float> i(set); +i.toBack(); +while (i.hasPrevious()) + qDebug() << i.previous(); +//! [18] + + +//! [19] +QMutableListIterator<int> i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [19] + + +//! [20] +QMutableLinkedListIterator<int> i(list); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [20] + + +//! [21] +QMutableVectorIterator<int> i(vector); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [21] + + +//! [22] +QMutableSetIterator<int> i(set); +while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); +} +//! [22] + + +//! [23] +QMutableListIterator<double> i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [23] + + +//! [24] +QMutableLinkedListIterator<double> i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [24] + + +//! [25] +QMutableVectorIterator<double> i(list); +while (i.hasNext()) { + double val = i.next(); + i.setValue(sqrt(val)); +} +//! [25] + + +//! [26] +QMap<int, QWidget *> map; +... +QMapIterator<int, QWidget *> i(map); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [26] + + +//! [27] +QMapIterator<int, QWidget *> i(map); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [27] + + +//! [28] +QMapIterator<int, QWidget *> i(map); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [28] + + +//! [29] +QHash<int, QWidget *> hash; +... +QHashIterator<int, QWidget *> i(hash); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [29] + + +//! [30] +QHashIterator<int, QWidget *> i(hash); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [30] + + +//! [31] +QHashIterator<int, QWidget *> i(hash); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [31] + + +//! [32] +QMap<int, QWidget *> map; +... +QMutableMapIterator<int, QWidget *> i(map); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [32] + + +//! [33] +QMutableMapIterator<int, QWidget *> i(map); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [33] + + +//! [34] +QMutableMapIterator<int, QWidget *> i(map); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [34] + + +//! [35] +QMutableMapIterator<QString, QString> i(map); +while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); +} +//! [35] + + +//! [36] +QHash<int, QWidget *> hash; +... +QMutableHashIterator<QString, QWidget *> i(hash); +while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); +} +//! [36] + + +//! [37] +QMutableHashIterator<int, QWidget *> i(hash); +i.toBack(); +while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); +} +//! [37] + + +//! [38] +QMutableHashIterator<int, QWidget *> i(hash); +while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); +} +//! [38] + + +//! [39] +QMutableHashIterator<QString, QString> 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<QString, double> pair; +//! [0] + + +//! [1] +pair.first = "pi"; +pair.second = 3.14159265358979323846; +//! [1] + + +//! [2] +QList<QPair<int, double> > 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<QString> 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<QWidget *> i(set); +while (i.hasNext()) + qDebug() << i.next(); +//! [4] + + +//! [5] +QSet<QWidget *>::const_iterator i = set.constBegin(); +while (i != set.constEnd()) { + qDebug() << *i; + ++i; +} +//! [5] + + +//! [6] +QSet<QString> set; +... +foreach (const QString &value, set) + qDebug() << value; +//! [6] + + +//! [7] +QSet<QString> set; +set.reserve(20000); +for (int i = 0; i < 20000; ++i) + set.insert(values[i]); +//! [7] + + +//! [8] +QSet<QString> set; +set << "January" << "February" << ... << "December"; + +QSet<QString>::iterator i; +for (i = set.begin(); i != set.end(); ++i) + qDebug() << *i; +//! [8] + + +//! [9] +QSet<QString> set; +set << "January" << "February" << ... << "December"; + +QSet<QString>::iterator i = set.begin(); +while (i != set.end()) { + if ((*i).startsWith('J')) { + i = set.erase(i); + } else { + ++i; + } +} +//! [9] + + +//! [10] +QSet<QString> set; +... +QSet<QString>::iterator it = qFind(set.begin(), set.end(), "Jeanette"); +if (it != set.end()) + cout << "Found Jeanette" << endl; +//! [10] + + +//! [11] +QSet<QString> set; +set << "January" << "February" << ... << "December"; + +QSet<QString>::const_iterator i; +for (i = set.begin(); i != set.end(); ++i) + qDebug() << *i; +//! [11] + + +//! [12] +QSet<QString> set; +... +QSet<QString>::iterator it = qFind(set.begin(), set.end(), "Jeanette"); +if (it != set.constEnd()) + cout << "Found Jeanette" << endl; +//! [12] + + +//! [13] +QSet<QString> set; +set << "red" << "green" << "blue" << ... << "black"; + +QList<QString> list = set.toList(); +qSort(list); +//! [13] + + +//! [14] +QStringList list; +list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; + +QSet<QString> set = QSet<QString>::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 <QtCore> +//! [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<int, 1024> array(n + 1); + ... + return array[n]; +} +//! [2] + + +//! [3] +QVarLengthArray<int> 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 <QtGui> +#include <QtDebug> +#include <QDeclarativeComponent> + +//! [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<Enum> 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 <QtGlobal> + +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 <QtGlobal> + +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<typename TInputType> +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 <qapplication.h> +#include <stdio.h> +#include <stdlib.h> + +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 <stdio.h> + +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<QColor>(); +//! [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<Login> 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<Login> 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<MyStruct>(); +//! [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>("MyClass"); +//! [4] + + +//! [5] +qRegisterMetaTypeStreamOperators<MyClass>("MyClass"); +//! [5] + + +//! [6] +QDataStream &operator<<(QDataStream &out, const MyClass &myObj); +QDataStream &operator>>(QDataStream &in, MyClass &myObj); +//! [6] + + +//! [7] +int id = qRegisterMetaType<MyStruct>(); +//! [7] + + +//! [8] +int id = qMetaTypeId<QString>(); // id is now QMetaType::QString +id = qMetaTypeId<MyStruct>(); // compile error if MyStruct not declared +//! [8] + +//! [9] +typedef QString CustomString; +qRegisterMetaType<CustomString>("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<const MyMimeData *>(event->mimeData()); + if (myData) { + // access myData's data directly (not through QMimeData's API) + } +} +//! [2] + + +//! [3] +application/x-qt-windows-mime;value="<custom type>" +//! [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<QImage>(event->mimeData()->imageData()); + ... +} +//! [5] + + +//! [6] +mimeData->setImageData(QImage("beautifulfjord.png")); +//! [6] + + +//! [7] +if (event->mimeData()->hasColor()) { + QColor color = qvariant_cast<QColor>(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<QLineEdit *>( + 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<QTimer *>(obj); +// timer == (QObject *)obj + +QAbstractButton *button = qobject_cast<QAbstractButton *>(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<QKeyEvent*>(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<QObject *> list = window()->queryList("QAbstractButton")); +foreach (QObject *obj, list) + static_cast<QAbstractButton *>(obj)->setEnabled(false); +//! [9] + + +//! [10] +QPushButton *button = parentWidget->findChild<QPushButton *>("button1"); +//! [10] + + +//! [11] +QListWidget *list = parentWidget->findChild<QListWidget *>(); +//! [11] + + +//! [12] +QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname"); +//! [12] + + +//! [13] +QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>(); +//! [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<QKeyEvent *>(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_<object name>_<signal name>(<signal parameters>); +//! [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<QPushButton *>("button1", Qt::FindDirectChildOnly); +//! [41] + + +//! [42] +QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDirectChildOnly); +//! [42] + + +//! [43] +QList<QPushButton *> childButtons = parentWidget.findChildren<QPushButton *>(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 <QApplication> +#include <QTimer> + +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<QColor>(); +//! [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<MyCustomStruct>(); +//! [4] + + +//! [5] +QVariant v; + +MyCustomStruct c; +if (v.canConvert<MyCustomStruct>()) + c = v.value<MyCustomStruct>(); + +v = 7; +int i = v.value<int>(); // same as v.toInt() +QString s = v.value<QString>(); // same as v.toString(), s is now "7" +MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty +//! [5] + + +//! [6] +QVariant v = 42; + +v.canConvert<int>(); // returns true +v.canConvert<QString>(); // returns true + +MyCustomStruct s; +v.setValue(s); + +v.canConvert<int>(); // returns false +v.canConvert<MyCustomStruct>(); // 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 <b>bold</b>, very <b>bold</b>"); +int j = 0; +while ((j = ba.indexOf("<b>", j)) != -1) { + cout << "Found <b> 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<QString, int> 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<int, QWidget *> hash; +... +for (int i = 0; i < 1000; ++i) { + if (hash[i] == okButton) + cout << "Found button at index " << i << endl; +} +//! [6] + + +//! [7] +QHashIterator<QString, int> i(hash); +while (i.hasNext()) { + i.next(); + cout << i.key() << ": " << i.value() << endl; +} +//! [7] + + +//! [8] +QHash<QString, int>::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<int> values = hash.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [10] + + +//! [11] +QHash<QString, int>::iterator i = hash.find("plenty"); +while (i != hash.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [11] + + +//! [12] +QHash<QString, int> 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<QString, int> hash; +hash.reserve(20000); +for (int i = 0; i < 20000; ++i) + hash.insert(keys[i], values[i]); +//! [14] + + +//! [15] +QHash<QObject *, int> objectHash; +... +QHash<QObject *, int>::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<QString, int> hash; +... +QHash<QString, int>::const_iterator i = hash.find("HDR"); +while (i != hash.end() && i.key() == "HDR") { + cout << i.value() << endl; + ++i; +} +//! [16] + + +//! [17] +QHash<QString, int> hash; +hash.insert("January", 1); +hash.insert("February", 2); +... +hash.insert("December", 12); + +QHash<QString, int>::iterator i; +for (i = hash.begin(); i != hash.end(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [17] + + +//! [18] +QHash<QString, int>::iterator i; +for (i = hash.begin(); i != hash.end(); ++i) + i.value() += 2; +//! [18] + + +//! [19] +QHash<QString, int>::iterator i = hash.begin(); +while (i != hash.end()) { + if (i.key().startsWith("_")) + i = hash.erase(i); + else + ++i; +} +//! [19] + + +//! [20] +QHash<QString, int>::iterator i = hash.begin(); +while (i != hash.end()) { + QHash<QString, int>::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<QString, int> hash; +hash.insert("January", 1); +hash.insert("February", 2); +... +hash.insert("December", 12); + +QHash<QString, int>::const_iterator i; +for (i = hash.constBegin(); i != hash.constEnd(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [23] + + +//! [24] +QMultiHash<QString, int> 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<int> values = hash.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [25] + + +//! [26] +QMultiHash<QString, int>::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<int> integerList; +QLinkedList<QTime> timeList; +//! [0] + + +//! [1] +QLinkedList<QString> list; +list << "one" << "two" << "three"; +// list: ["one", "two", "three"] +//! [1] + + +//! [2] +QLinkedList<QWidget *> list; +... +while (!list.isEmpty()) + delete list.takeFirst(); +//! [2] + + +//! [3] +QLinkedList<QString> list; +list.append("one"); +list.append("two"); +list.append("three"); +// list: ["one", "two", "three"] +//! [3] + + +//! [4] +QLinkedList<QString> list; +list.prepend("one"); +list.prepend("two"); +list.prepend("three"); +// list: ["three", "two", "one"] +//! [4] + + +//! [5] +QList<QString> list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeAll("sun"); +// list: ["cloud", "rain"] +//! [5] + + +//! [6] +QList<QString> list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeOne("sun"); +// list: ["cloud", "sun", "rain"] +//! [6] + + +//! [7] +QLinkedList<QString> list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QLinkedList<QString>::iterator i; +for (i = list.begin(); i != list.end(); ++i) + cout << *i << endl; +//! [7] + + +//! [8] +QLinkedList<QString> list; +... +QLinkedList<QString>::iterator it = qFind(list.begin(), + list.end(), "Joel"); +if (it != list.end()) + cout << "Found Joel" << endl; +//! [8] + + +//! [9] +QLinkedList<int>::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i += 2; +//! [9] + + +//! [10] +QLinkedList<QString> list; +... +QLinkedList<QString>::iterator i = list.begin(); +while (i != list.end()) { + if ((*i).startsWith("_")) + i = list.erase(i); + else + ++i; +} +//! [10] + + +//! [11] +QLinkedList<QString>::iterator i = list.begin(); +while (i != list.end()) { + QLinkedList<QString>::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<QString> list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QLinkedList<QString>::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + cout << *i << endl; +//! [14] + + +//! [15] +QLinkedList<QString> list; +... +QLinkedList<QString>::iterator it = qFind(list.constBegin(), + list.constEnd(), "Joel"); +if (it != list.constEnd()) + cout << "Found Joel" << endl; +//! [15] + + +//! [16] +std::list<double> stdlist; +list.push_back(1.2); +list.push_back(0.5); +list.push_back(3.14); + +QLinkedList<double> list = QLinkedList<double>::fromStdList(stdlist); +//! [16] + + +//! [17] +QLinkedList<double> list; +list << 1.2 << 0.5 << 3.14; + +std::list<double> 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<int> integerList; +QList<QDate> dateList; +//! [0] + + +//! [1] +QList<QString> 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<QWidget *> 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<QString> list; +list.append("one"); +list.append("two"); +list.append("three"); +// list: ["one", "two", "three"] +//! [6] + + +//! [7] +QList<QString> list; +list.prepend("one"); +list.prepend("two"); +list.prepend("three"); +// list: ["three", "two", "one"] +//! [7] + + +//! [8] +QList<QString> list; +list << "alpha" << "beta" << "delta"; +list.insert(2, "gamma"); +// list: ["alpha", "beta", "gamma", "delta"] +//! [8] + + +//! [9] +QList<QString> list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeAll("sun"); +// list: ["cloud", "rain"] +//! [9] + + +//! [10] +QList<QString> list; +list << "sun" << "cloud" << "sun" << "rain"; +list.removeOne("sun"); +// list: ["cloud", ,"sun", "rain"] +//! [10] + + +//! [11] +QList<QString> list; +list << "A" << "B" << "C" << "D" << "E" << "F"; +list.move(1, 4); +// list: ["A", "C", "D", "E", "B", "F"] +//! [11] + + +//! [12] +QList<QString> list; +list << "A" << "B" << "C" << "D" << "E" << "F"; +list.swap(1, 4); +// list: ["A", "E", "C", "D", "B", "F"] +//! [12] + + +//! [13] +QList<QString> 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<QString> 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<QString> list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QList<QString>::iterator i; +for (i = list.begin(); i != list.end(); ++i) + cout << *i << endl; +//! [15] + + +//! [16] +QList<int>::iterator i; +for (i = list.begin(); i != list.end(); ++i) + *i += 2; +//! [16] + + +//! [17] +QList<QWidget *> list; +... +qDeleteAll(list.begin(), list.end()); +//! [17] + + +//! [18] +if (*it == "Hello") + *it = "Bonjour"; +//! [18] + + +//! [19] +QList<QString> list; +list.append("January"); +list.append("February"); +... +list.append("December"); + +QList<QString>::const_iterator i; +for (i = list.constBegin(); i != list.constEnd(); ++i) + cout << *i << endl; +//! [19] + + +//! [20] +QList<QWidget *> list; +... +qDeleteAll(list.constBegin(), list.constEnd()); +//! [20] + + +//! [21] +QVector<double> vect; +vect << 20.0 << 30.0 << 40.0 << 50.0; + +QList<double> list = QVector<T>::fromVector(vect); +// list: [20.0, 30.0, 40.0, 50.0] +//! [21] + + +//! [22] +QStringList list; +list << "Sven" << "Kim" << "Ola"; + +QVector<QString> vect = list.toVector(); +// vect: ["Sven", "Kim", "Ola"] +//! [22] + + +//! [23] +QSet<int> set; +set << 20 << 30 << 40 << ... << 70; + +QList<int> list = QList<int>::fromSet(set); +qSort(list); +//! [23] + + +//! [24] +QStringList list; +list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; + +QSet<QString> set = list.toSet(); +set.contains("Julia"); // returns true +set.contains("Mike"); // returns true +set.size(); // returns 2 +//! [24] + + +//! [25] +std::list<double> stdlist; +list.push_back(1.2); +list.push_back(0.5); +list.push_back(3.14); + +QList<double> list = QList<double>::fromStdList(stdlist); +//! [25] + + +//! [26] +QList<double> list; +list << 1.2 << 0.5 << 3.14; + +std::list<double> 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<QString, int> 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<int, QWidget *> map; +... +for (int i = 0; i < 1000; ++i) { + if (map[i] == okButton) + cout << "Found button at index " << i << endl; +} +//! [6] + + +//! [7] +QMapIterator<QString, int> i(map); +while (i.hasNext()) { + i.next(); + cout << i.key() << ": " << i.value() << endl; +} +//! [7] + + +//! [8] +QMap<QString, int>::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<int> values = map.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [10] + + +//! [11] +QMap<QString, int>::iterator i = map.find("plenty"); +while (i != map.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; +} +//! [11] + + +//! [12] +QMap<QString, int> 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<QString, int> map; +... +QMap<QString, int>::const_iterator i = map.find("HDR"); +while (i != map.end() && i.key() == "HDR") { + cout << i.value() << endl; + ++i; +} +//! [14] + + +//! [15] +QMap<int, QString> 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<QString, int> map; +... +QMap<QString, int>::const_iterator i = map.lowerBound("HDR"); +QMap<QString, int>::const_iterator upperBound = map.upperBound("HDR"); +while (i != upperBound) { + cout << i.value() << endl; + ++i; +} +//! [16] + + +//! [17] +QMap<int, QString> 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<QString, int> map; +map.insert("January", 1); +map.insert("February", 2); +... +map.insert("December", 12); + +QMap<QString, int>::iterator i; +for (i = map.begin(); i != map.end(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [18] + + +//! [19] +QMap<QString, int>::iterator i; +for (i = map.begin(); i != map.end(); ++i) + i.value() += 2; +//! [19] + + +//! [20] +QMap<QString, int>::iterator i = map.begin(); +while (i != map.end()) { + if (i.key().startsWith("_")) + i = map.erase(i); + else + ++i; +} +//! [20] + + +//! [21] +QMap<QString, int>::iterator i = map.begin(); +while (i != map.end()) { + QMap<QString, int>::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<QString, int> map; +map.insert("January", 1); +map.insert("February", 2); +... +map.insert("December", 12); + +QMap<QString, int>::const_iterator i; +for (i = map.constBegin(); i != map.constEnd(); ++i) + cout << i.key() << ": " << i.value() << endl; +//! [24] + + +//! [25] +QMultiMap<QString, int> 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<int> values = map.values("plenty"); +for (int i = 0; i < values.size(); ++i) + cout << values.at(i) << endl; +//! [26] + + +//! [27] +QMultiMap<QString, int>::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<int> 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 <dangelog@gmail.com>. +** 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("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\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) (?<name>\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<MyClass> p(useSubClass ? new MyClass() : new MySubClass); + QScopedPointer<QIODevice> device(handsOverOwnership()); + + if (m_value > 3) + return; + + process(device); +} +//! [1] + +//! [2] + const QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer<const QWidget> p(new QWidget()); + + QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer<QWidget> p(new QWidget()); + + const QWidget *p = new QWidget(); + // is equivalent to: + QScopedPointer<const QWidget> p(new QWidget()); +//! [2] + +//! [3] +if (scopedPointer) { + ... +} +//! [3] + +//! [4] +class MyPrivateClass; // forward declare MyPrivateClass + +class MyClass +{ +private: + QScopedPointer<MyPrivateClass> 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<int, QScopedPointerArrayDeleter<int> > arrayPointer(new int[42]); + +// this QScopedPointer frees its data using free(): +QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(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<MyCustomClass, ScopedPointerCustomDeleter> 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 <QtCore>" +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<int> integerVector; +QVector<QString> stringVector; +//! [0] + + +//! [1] +QVector<QString> vector(200); +//! [1] + + +//! [2] +QVector<QString> 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<int> vector(10); +int *data = vector.data(); +for (int i = 0; i < 10; ++i) + data[i] = 2 * i; +//! [6] + + +//! [7] +QVector<QString> vector(0); +vector.append("one"); +vector.append("two"); +vector.append("three"); +// vector: ["one", "two", "three"] +//! [7] + + +//! [8] +QVector<QString> vector; +vector.prepend("one"); +vector.prepend("two"); +vector.prepend("three"); +// vector: ["three", "two", "one"] +//! [8] + + +//! [9] +QVector<QString> vector; +vector << "alpha" << "beta" << "delta"; +vector.insert(2, "gamma"); +// vector: ["alpha", "beta", "gamma", "delta"] +//! [9] + + +//! [10] +QVector<double> 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<QString> vector(3); +vector.fill("Yes"); +// vector: ["Yes", "Yes", "Yes"] + +vector.fill("oh", 5); +// vector: ["oh", "oh", "oh", "oh", "oh"] +//! [11] + + +//! [12] +QVector<QString> 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<QString> 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<double> vect; +vect << "red" << "green" << "blue" << "black"; + +QList<double> list = vect.toList(); +// list: ["red", "green", "blue", "black"] +//! [14] + + +//! [15] +QStringList list; +list << "Sven" << "Kim" << "Ola"; + +QVector<QString> vect = QVector<QString>::fromList(list); +// vect: ["Sven", "Kim", "Ola"] +//! [15] + + +//! [16] +std::vector<double> stdvector; +vector.push_back(1.2); +vector.push_back(0.5); +vector.push_back(3.14); + +QVector<double> vector = QVector<double>::fromStdVector(stdvector); +//! [16] + + +//! [17] +QVector<double> vector; +vector << 1.2 << 0.5 << 3.14; + +std::vector<double> 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 <QApplication> +#include <QMessageBox> + +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 <stephen.kelly@kdab.com> +** 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] diff --git a/src/corelib/doc/snippets/file/file.cpp b/src/corelib/doc/snippets/file/file.cpp new file mode 100644 index 0000000000..f03c6e6847 --- /dev/null +++ b/src/corelib/doc/snippets/file/file.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** 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 <QFile> +#include <QTextStream> + +static void process_line(const QByteArray &) +{ +} + +static void process_line(const QString &) +{ +} + +static void noStream_snippet() +{ +//! [0] + QFile file("in.txt"); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + while (!file.atEnd()) { + QByteArray line = file.readLine(); + process_line(line); + } +//! [0] +} + +static void readTextStream_snippet() +{ +//! [1] + QFile file("in.txt"); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + QTextStream in(&file); + while (!in.atEnd()) { + QString line = in.readLine(); + process_line(line); + } +//! [1] +} + +static void writeTextStream_snippet() +{ +//! [2] + QFile file("out.txt"); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QTextStream out(&file); + out << "The magic number is: " << 49 << "\n"; +//! [2] +} + +static void writeTextStream_snippet() +{ + QFile file("out.dat"); + if (!file.open(QIODevice::WriteOnly)) + return; + + QDataStream out(&file); + out << "The magic number is: " << 49 << "\n"; +} + +static void readRegularEmptyFile_snippet() +{ +//! [3] + QFile file("/proc/modules"); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + QTextStream in(&file); + QString line = in.readLine(); + while (!line.isNull()) { + process_line(line); + line = in.readLine(); + } +//! [3] +} + +int main() +{ + lineByLine_snippet(); + writeStream_snippet(); +} diff --git a/src/corelib/doc/snippets/fileinfo/main.cpp b/src/corelib/doc/snippets/fileinfo/main.cpp new file mode 100644 index 0000000000..48d8223d79 --- /dev/null +++ b/src/corelib/doc/snippets/fileinfo/main.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** 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 <QApplication> +#include <QPushButton> +#include <QFileInfo> +#include <QDir> +#include <QGroupBox> +#include <QVBoxLayout> +#include <QDebug> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//! [0] + QFileInfo fileInfo1("~/examples/191697/."); + QFileInfo fileInfo2("~/examples/191697/.."); + QFileInfo fileInfo3("~/examples/191697/main.cpp"); +//! [0] +//! [1] + QFileInfo fileInfo4("."); + QFileInfo fileInfo5(".."); + QFileInfo fileInfo6("main.cpp"); +//! [1] + + qDebug() << fileInfo1.fileName(); + qDebug() << fileInfo2.fileName(); + qDebug() << fileInfo3.fileName(); + qDebug() << fileInfo4.fileName(); + qDebug() << fileInfo5.fileName(); + qDebug() << fileInfo6.fileName(); + + QPushButton* button1 = new QPushButton(fileInfo1.dir().path()); + QPushButton* button2 = new QPushButton(fileInfo2.dir().path()); + QPushButton* button3 = new QPushButton(fileInfo3.dir().path()); + QPushButton* button4 = new QPushButton(fileInfo4.dir().path()); + QPushButton* button5 = new QPushButton(fileInfo5.dir().path()); + QPushButton* button6 = new QPushButton(fileInfo6.dir().path()); + + QVBoxLayout* vbox = new QVBoxLayout; + vbox->addWidget(button1); + vbox->addWidget(button2); + vbox->addWidget(button3); + vbox->addWidget(button4); + vbox->addWidget(button5); + vbox->addWidget(button6); + vbox->addStretch(1); + + QGroupBox *groupBox = new QGroupBox("QFileInfo::dir() test"); + groupBox->setLayout(vbox); + groupBox->show(); + + return app.exec(); +} diff --git a/src/corelib/doc/snippets/ntfsp.cpp b/src/corelib/doc/snippets/ntfsp.cpp new file mode 100644 index 0000000000..8f2ff1fc98 --- /dev/null +++ b/src/corelib/doc/snippets/ntfsp.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] +extern Q_CORE_EXPORT int qt_ntfs_permission_lookup; +//! [0] + +//! [1] +qt_ntfs_permission_lookup++; // turn checking on +qt_ntfs_permission_lookup--; // turn it off again +//! [1] + diff --git a/src/corelib/doc/snippets/pointer/pointer.cpp b/src/corelib/doc/snippets/pointer/pointer.cpp new file mode 100644 index 0000000000..dd2bc82102 --- /dev/null +++ b/src/corelib/doc/snippets/pointer/pointer.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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QLabel> +#include <QPointer> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//! [0] + QPointer<QLabel> label = new QLabel; + label->setText("&Status:"); +//! [0] + +//! [1] + if (label) +//! [1] //! [2] + label->show(); +//! [2] + return 0; +} diff --git a/src/corelib/doc/snippets/process/process.cpp b/src/corelib/doc/snippets/process/process.cpp new file mode 100644 index 0000000000..19f241a695 --- /dev/null +++ b/src/corelib/doc/snippets/process/process.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 <QProcess> + +bool zip() +{ +//! [0] + QProcess gzip; + gzip.start("gzip", QStringList() << "-c"); + if (!gzip.waitForStarted()) + return false; + + gzip.write("Qt rocks!"); + gzip.closeWriteChannel(); + + if (!gzip.waitForFinished()) + return false; + + QByteArray result = gzip.readAll(); +//! [0] + + gzip.start("gzip", QStringList() << "-d" << "-c"); + gzip.write(result); + gzip.closeWriteChannel(); + + if (!gzip.waitForFinished()) + return false; + + qDebug("Result: %s", gzip.readAll().data()); + return true; +} + + +int main() +{ + zip(); + return 0; +} diff --git a/src/corelib/doc/snippets/qdebug/qdebugsnippet.cpp b/src/corelib/doc/snippets/qdebug/qdebugsnippet.cpp new file mode 100644 index 0000000000..2dbfe49c7d --- /dev/null +++ b/src/corelib/doc/snippets/qdebug/qdebugsnippet.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$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtDebug> + +class Coordinate : public QObject +{ +public: + int myX, myY; + + int x() const { return myX; }; + int y() const { return myY; }; +}; + +//! [0] +QDebug operator<<(QDebug dbg, const Coordinate &c) +{ + dbg.nospace() << "(" << c.x() << ", " << c.y() << ")"; + + return dbg.space(); +} +//! [0] + +int main(int argv, char **args) +{ + Coordinate coordinate; + coordinate.myX = 10; + coordinate.myY = 44; + +//! [1] + qDebug() << "Date:" << QDate::currentDate(); + qDebug() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40); + qDebug() << "Custom coordinate type:" << coordinate; +//! [1] +} diff --git a/src/corelib/doc/snippets/qdir-listfiles/main.cpp b/src/corelib/doc/snippets/qdir-listfiles/main.cpp new file mode 100644 index 0000000000..599046aabd --- /dev/null +++ b/src/corelib/doc/snippets/qdir-listfiles/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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 <QDir> +#include <iostream> + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QDir dir; + dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); + dir.setSorting(QDir::Size | QDir::Reversed); + + QFileInfoList list = dir.entryInfoList(); + std::cout << " Bytes Filename" << std::endl; + for (int i = 0; i < list.size(); ++i) { + QFileInfo fileInfo = list.at(i); + std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10) + .arg(fileInfo.fileName())); + std::cout << std::endl; + } + return 0; +} +//! [0] diff --git a/src/corelib/doc/snippets/qdir-namefilters/main.cpp b/src/corelib/doc/snippets/qdir-namefilters/main.cpp new file mode 100644 index 0000000000..7b48f7eaf7 --- /dev/null +++ b/src/corelib/doc/snippets/qdir-namefilters/main.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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 <QDir> +#include <iostream> + +int main(int argc, char *argv[]) +{ + QDir dir; + dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); + dir.setSorting(QDir::Size | QDir::Reversed); + +//! [0] + QStringList filters; + filters << "*.cpp" << "*.cxx" << "*.cc"; + dir.setNameFilters(filters); +//! [0] + + QFileInfoList list = dir.entryInfoList(); + std::cout << " Bytes Filename" << std::endl; + for (int i = 0; i < list.size(); ++i) { + QFileInfo fileInfo = list.at(i); + std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10) + .arg(fileInfo.fileName())); + std::cout << std::endl; + } + return 0; +} diff --git a/src/corelib/doc/snippets/qelapsedtimer/main.cpp b/src/corelib/doc/snippets/qelapsedtimer/main.cpp new file mode 100644 index 0000000000..7b4b6529e7 --- /dev/null +++ b/src/corelib/doc/snippets/qelapsedtimer/main.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtNetwork 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$ +** +****************************************************************************/ +#include <QtCore> + +void slowOperation1() +{ + static char buf[256]; + for (int i = 0; i < (1<<20); ++i) + buf[i % sizeof buf] = i; +} + +void slowOperation2(int) { slowOperation1(); } + +void startExample() +{ +//![0] + QElapsedTimer timer; + timer.start(); + + slowOperation1(); + + qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds"; +//![0] +} + +//![1] +void executeSlowOperations(int timeout) +{ + QElapsedTimer timer; + timer.start(); + slowOperation1(); + + int remainingTime = timeout - timer.elapsed(); + if (remainingTime > 0) + slowOperation2(remainingTime); +} +//![1] + +//![2] +void executeOperationsForTime(int ms) +{ + QElapsedTimer timer; + timer.start(); + + while (!timer.hasExpired(ms)) + slowOperation1(); +} +//![2] + +int restartExample() +{ +//![3] + QElapsedTimer timer; + + int count = 1; + timer.start(); + do { + count *= 2; + slowOperation2(count); + } while (timer.restart() < 250); + + return count; +//![3] +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + startExample(); + restartExample(); + executeSlowOperations(5); + executeOperationsForTime(5); +} diff --git a/src/corelib/doc/snippets/qmetaobject-invokable/main.cpp b/src/corelib/doc/snippets/qmetaobject-invokable/main.cpp new file mode 100644 index 0000000000..b22df1ea15 --- /dev/null +++ b/src/corelib/doc/snippets/qmetaobject-invokable/main.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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QDebug> +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + qDebug() << window.metaObject()->methodCount(); + window.show(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qmetaobject-invokable/qmetaobject-invokable.pro b/src/corelib/doc/snippets/qmetaobject-invokable/qmetaobject-invokable.pro new file mode 100644 index 0000000000..4df19215f0 --- /dev/null +++ b/src/corelib/doc/snippets/qmetaobject-invokable/qmetaobject-invokable.pro @@ -0,0 +1,3 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp diff --git a/src/corelib/doc/snippets/qmetaobject-invokable/window.cpp b/src/corelib/doc/snippets/qmetaobject-invokable/window.cpp new file mode 100644 index 0000000000..5ab8d83f63 --- /dev/null +++ b/src/corelib/doc/snippets/qmetaobject-invokable/window.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$ +** +****************************************************************************/ + +#include "window.h" + +Window::Window() +{ +} + +void Window::normalMethod() +{ + // Cannot be called by the meta-object system. + show(); +} + +void Window::invokableMethod() +{ + // Can be called by the meta-object system. + show(); +} diff --git a/src/corelib/doc/snippets/qmetaobject-invokable/window.h b/src/corelib/doc/snippets/qmetaobject-invokable/window.h new file mode 100644 index 0000000000..f2f34792e5 --- /dev/null +++ b/src/corelib/doc/snippets/qmetaobject-invokable/window.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +//! [Window class with invokable method] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + void normalMethod(); + Q_INVOKABLE void invokableMethod(); +}; +//! [Window class with invokable method] + +#endif diff --git a/src/corelib/doc/snippets/qprocess-environment/main.cpp b/src/corelib/doc/snippets/qprocess-environment/main.cpp new file mode 100644 index 0000000000..124668a544 --- /dev/null +++ b/src/corelib/doc/snippets/qprocess-environment/main.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$ +** +****************************************************************************/ + +#include <QtCore> + +void startProcess() +{ + { +//! [0] +QProcess process; +QStringList env = QProcess::systemEnvironment(); +env << "TMPDIR=C:\\MyApp\\temp"; // Add an environment variable +env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;C:\\Bin"); +process.setEnvironment(env); +process.start("myapp"); +//! [0] + } + + { +//! [1] +QProcess process; +QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); +env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable +env.insert("PATH", env.value("Path") + ";C:\\Bin"); +process.setProcessEnvironment(env); +process.start("myapp"); +//! [1] + } +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + startProcess(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qprocess/qprocess-simpleexecution.cpp b/src/corelib/doc/snippets/qprocess/qprocess-simpleexecution.cpp new file mode 100644 index 0000000000..ea1892078e --- /dev/null +++ b/src/corelib/doc/snippets/qprocess/qprocess-simpleexecution.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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QtGui> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); +//! [0] + QObject *parent; +//! [0] + parent = &app; + +//! [1] + QString program = "./path/to/Qt/examples/widgets/analogclock"; +//! [1] + program = "./../../../../examples/widgets/analogclock/analogclock"; + +//! [2] + QStringList arguments; + arguments << "-style" << "motif"; + + QProcess *myProcess = new QProcess(parent); + myProcess->start(program, arguments); +//! [2] + + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp new file mode 100644 index 0000000000..cda905e3a2 --- /dev/null +++ b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.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$ +** +****************************************************************************/ + +#include <QtGui> + +#include "buttonwidget.h" + +//! [0] +ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent) + : QWidget(parent) +{ + signalMapper = new QSignalMapper(this); + + QGridLayout *gridLayout = new QGridLayout; + for (int i = 0; i < texts.size(); ++i) { + QPushButton *button = new QPushButton(texts[i]); + connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); +//! [0] //! [1] + signalMapper->setMapping(button, texts[i]); + gridLayout->addWidget(button, i / 3, i % 3); + } + + connect(signalMapper, SIGNAL(mapped(const QString &)), +//! [1] //! [2] + this, SIGNAL(clicked(const QString &))); + + setLayout(gridLayout); +} +//! [2] diff --git a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h new file mode 100644 index 0000000000..9204de935a --- /dev/null +++ b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h @@ -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$ +** +****************************************************************************/ + +#ifndef BUTTONWIDGET_H +#define BUTTONWIDGET_H + +#include <qwidget.h> + +class QSignalMapper; +class QString; +class QStringList; + +//! [0] +class ButtonWidget : public QWidget +{ + Q_OBJECT + +public: + ButtonWidget(QStringList texts, QWidget *parent = 0); + +signals: + void clicked(const QString &text); + +private: + QSignalMapper *signalMapper; +//! [0] //! [1] +}; +//! [1] + +#endif diff --git a/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp b/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp new file mode 100644 index 0000000000..549d24b249 --- /dev/null +++ b/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.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$ +** +****************************************************************************/ + +#include <QtGui> +#include <QApplication> +#include <QSortFilterProxyModel> + +class MyItemModel : public QStandardItemModel +{ +public: + MyItemModel(QWidget *parent = 0); +}; + +MyItemModel::MyItemModel(QWidget *parent) + : QStandardItemModel(parent) +{}; + +class Widget : public QWidget +{ +public: + Widget(QWidget *parent = 0); +}; + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +//! [0] //! [1] + QTreeView *treeView = new QTreeView; +//! [0] + MyItemModel *model = new MyItemModel(this); + + treeView->setModel(model); +//! [1] + +//! [2] + MyItemModel *sourceModel = new MyItemModel(this); + QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this); + + proxyModel->setSourceModel(sourceModel); + treeView->setModel(proxyModel); +//! [2] + +//! [3] + treeView->setSortingEnabled(true); +//! [3] + +//! [4] + proxyModel->sort(2, Qt::AscendingOrder); +//! [4] //! [5] + proxyModel->setFilterRegExp(QRegExp(".png", Qt::CaseInsensitive, + QRegExp::FixedString)); + proxyModel->setFilterKeyColumn(1); +//! [5] +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Widget widget; + widget.show(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qstack/main.cpp b/src/corelib/doc/snippets/qstack/main.cpp new file mode 100644 index 0000000000..cc96f8c55d --- /dev/null +++ b/src/corelib/doc/snippets/qstack/main.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$ +** +****************************************************************************/ + +#include <QtGui> +#include <iostream> +using namespace std; + +int main(int argc, char *argv[]) +{ +//! [0] + QStack<int> stack; + stack.push(1); + stack.push(2); + stack.push(3); + while (!stack.isEmpty()) + cout << stack.pop() << endl; +//! [0] +} diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp new file mode 100644 index 0000000000..d7299e80d5 --- /dev/null +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -0,0 +1,997 @@ +/**************************************************************************** +** +** 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 <QtGui> +#include <QApplication> +#include <stdio.h> + +class Widget : public QWidget +{ +public: + Widget(QWidget *parent = 0); + + void constCharPointer(); + void constCharArray(); + void characterReference(); + void atFunction(); + void stringLiteral(); + void modify(); + void index(); + QString boolToString(bool b); + void nullVsEmpty(); + + void appendFunction(); + void argFunction(); + void chopFunction(); + void compareFunction(); + void compareSensitiveFunction(); + void containsFunction(); + void countFunction(); + void dataFunction(); + void endsWithFunction(); + void fillFunction(); + void fromRawDataFunction(); + + void indexOfFunction(); + void firstIndexOfFunction(); + void insertFunction(); + void isNullFunction(); + void isEmptyFunction(); + void lastIndexOfFunction(); + void leftFunction(); + void leftJustifiedFunction(); + void leftRefFunction(); + void midFunction(); + void midRefFunction(); + void numberFunction(); + + void prependFunction(); + void removeFunction(); + void replaceFunction(); + void reserveFunction(); + void resizeFunction(); + void rightFunction(); + void rightJustifiedFunction(); + void rightRefFunction(); + void sectionFunction(); + void setNumFunction(); + void simplifiedFunction(); + + void sizeFunction(); + void splitFunction(); + void splitCaseSensitiveFunction(); + void sprintfFunction(); + void startsWithFunction(); + void toDoubleFunction(); + void toFloatFunction(); + void toIntFunction(); + void toLongFunction(); + void toLongLongFunction(); + + void toLowerFunction(); + void toShortFunction(); + void toUIntFunction(); + void toULongFunction(); + void toULongLongFunction(); + void toUShortFunction(); + void toUpperFunction(); + void trimmedFunction(); + void truncateFunction(); + + void plusEqualOperator(); + void arrayOperator(); +}; + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +} + +void Widget::constCharPointer() +{ + //! [0] + QString str = "Hello"; + //! [0] +} + +void Widget::constCharArray() +{ + //! [1] + static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 }; + QString str(data, 4); + //! [1] +} + +void Widget::characterReference() +{ + //! [2] + QString str; + str.resize(4); + + str[0] = QChar('U'); + str[1] = QChar('n'); + str[2] = QChar(0x10e3); + str[3] = QChar(0x03a3); + //! [2] +} + +void Widget::atFunction() +{ + //! [3] + QString str; + + for (int i = 0; i < str.size(); ++i) { + if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f')) + qDebug() << "Found character in range [a-f]"; + } + //! [3] +} + +void Widget::stringLiteral() +{ + //! [4] + QString str; + + if (str == "auto" || str == "extern" + || str == "static" || str == "register") { + // ... + } + //! [4] +} + +void Widget::modify() +{ + //! [5] + QString str = "and"; + str.prepend("rock "); // str == "rock and" + str.append(" roll"); // str == "rock and roll" + str.replace(5, 3, "&"); // str == "rock & roll" + //! [5] +} + +void Widget::index() +{ + //! [6] + QString str = "We must be <b>bold</b>, very <b>bold</b>"; + int j = 0; + + while ((j = str.indexOf("<b>", j)) != -1) { + qDebug() << "Found <b> tag at index position" << j; + ++j; + } + //! [6] +} + +//! [7] +QString Widget::boolToString(bool b) +{ + QString result; + if (b) + result = "True"; + else + result = "False"; + return result; +} +//! [7] + + +void Widget::nullVsEmpty() +{ + //! [8] + QString().isNull(); // returns true + QString().isEmpty(); // returns true + + QString("").isNull(); // returns false + QString("").isEmpty(); // returns true + + QString("abc").isNull(); // returns false + QString("abc").isEmpty(); // returns false + //! [8] +} + +void Widget::appendFunction() +{ + //! [9] + QString x = "free"; + QString y = "dom"; + + x.append(y); + // x == "freedom" + //! [9] + + //! [10] + x.insert(x.size(), y); + //! [10] +} + +void Widget::argFunction() +{ + //! [11] + QString i; // current file's number + QString total; // number of files to process + QString fileName; // current file's name + + QString status = QString("Processing file %1 of %2: %3") + .arg(i).arg(total).arg(fileName); + //! [11] + + //! [12] //! [13] + QString str; + //! [12] + str = "%1 %2"; + + str.arg("%1f", "Hello"); // returns "%1f Hello" + str.arg("%1f").arg("Hello"); // returns "Hellof %2" + //! [13] + + //! [14] + str = QString("Decimal 63 is %1 in hexadecimal") + .arg(63, 0, 16); + // str == "Decimal 63 is 3f in hexadecimal" + + 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" + //! [14] +} + +void Widget::chopFunction() +{ + //! [15] + QString str("LOGOUT\r\n"); + str.chop(2); + // str == "LOGOUT" + //! [15] +} + +void Widget::compareFunction() +{ + int x = QString::compare("auto", "auto"); // x == 0 + int y = QString::compare("auto", "car"); // y < 0 + int z = QString::compare("car", "auto"); // z > 0 +} + +void Widget::compareSensitiveFunction() +{ + //! [16] + int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0 + int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0 + int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0 + //! [16] +} + +void Widget::containsFunction() +{ + //! [17] + QString str = "Peter Pan"; + str.contains("peter", Qt::CaseInsensitive); // returns true + //! [17] +} + +void Widget::countFunction() +{ + //! [18] + QString str = "banana and panama"; + str.count(QRegExp("a[nm]a")); // returns 4 + //! [18] + + //! [95] + QString str = "banana and panama"; + str.count(QRegularExpression("a[nm]a")); // returns 4 + //! [95] +} + +void Widget::dataFunction() +{ + //! [19] + QString str = "Hello world"; + QChar *data = str.data(); + while (!data->isNull()) { + qDebug() << data->unicode(); + ++data; + } + //! [19] +} + +void Widget::endsWithFunction() +{ + //! [20] + QString str = "Bananas"; + str.endsWith("anas"); // returns true + str.endsWith("pple"); // returns false + //! [20] +} + +void Widget::fillFunction() +{ + //! [21] + QString str = "Berlin"; + str.fill('z'); + // str == "zzzzzz" + + str.fill('A', 2); + // str == "AA" + //! [21] +} + +void Widget::fromRawDataFunction() +{ + //! [22] + QRegExp pattern; + static const QChar unicode[] = { + 0x005A, 0x007F, 0x00A4, 0x0060, + 0x1009, 0x0020, 0x0020}; + int size = sizeof(unicode) / sizeof(QChar); + + QString str = QString::fromRawData(unicode, size); + if (str.contains(QRegExp(pattern))) { + // ... + //! [22] //! [23] + } + //! [23] +} + +void Widget::indexOfFunction() +{ + //! [24] + QString x = "sticky question"; + QString 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 + //! [24] +} + +void Widget::firstIndexOfFunction() +{ + //! [25] + QString str = "the minimum"; + str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4 + //! [25] + + //! [93] + QString str = "the minimum"; + str.indexOf(QRegularExpression("m[aeiou]"), 0); // returns 4 + //! [93] +} + +void Widget::insertFunction() +{ + //! [26] + QString str = "Meal"; + str.insert(1, QString("ontr")); + // str == "Montreal" + //! [26] +} + +void Widget::isEmptyFunction() +{ + //! [27] + QString().isEmpty(); // returns true + QString("").isEmpty(); // returns true + QString("x").isEmpty(); // returns false + QString("abc").isEmpty(); // returns false + //! [27] +} + +void Widget::isNullFunction() +{ + //! [28] + QString().isNull(); // returns true + QString("").isNull(); // returns false + QString("abc").isNull(); // returns false + //! [28] +} + +void Widget::lastIndexOfFunction() +{ + //! [29] + QString x = "crazy azimuths"; + QString 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 + //! [29] + + //! [30] + QString str = "the minimum"; + str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8 + //! [30] + + //! [94] + QString str = "the minimum"; + str.lastIndexOf(QRegularExpression("m[aeiou]")); // returns 8 + //! [94] +} + +void Widget::leftFunction() +{ + //! [31] + QString x = "Pineapple"; + QString y = x.left(4); // y == "Pine" + //! [31] +} + +void Widget::leftJustifiedFunction() +{ + //! [32] + QString s = "apple"; + QString t = s.leftJustified(8, '.'); // t == "apple..." + //! [32] + + //! [33] + QString str = "Pineapple"; + str = str.leftJustified(5, '.', true); // str == "Pinea" + //! [33] +} + +void Widget::midFunction() +{ + //! [34] + QString x = "Nine pineapples"; + QString y = x.mid(5, 4); // y == "pine" + QString z = x.mid(5); // z == "pineapples" + //! [34] +} + +void Widget::numberFunction() +{ + //! [35] + long a = 63; + QString s = QString::number(a, 16); // s == "3f" + QString t = QString::number(a, 16).toUpper(); // t == "3F" + //! [35] +} + +void Widget::prependFunction() +{ + //! [36] + QString x = "ship"; + QString y = "air"; + x.prepend(y); + // x == "airship" + //! [36] +} + +void Widget::removeFunction() +{ + //! [37] + QString s = "Montreal"; + s.remove(1, 4); + // s == "Meal" + //! [37] + + //! [38] + QString t = "Ali Baba"; + t.remove(QChar('a'), Qt::CaseInsensitive); + // t == "li Bb" + //! [38] + + //! [39] + QString r = "Telephone"; + r.remove(QRegExp("[aeiou].")); + // r == "The" + //! [39] + + //! [96] + QString r = "Telephone"; + r.remove(QRegularExpression("[aeiou].")); + // r == "The" + //! [96] +} + +void Widget::replaceFunction() +{ + //! [40] + QString x = "Say yes!"; + QString y = "no"; + x.replace(4, 3, y); + // x == "Say no!" + //! [40] + + //! [41] + QString str = "colour behaviour flavour neighbour"; + str.replace(QString("ou"), QString("o")); + // str == "color behavior flavor neighbor" + //! [41] + + //! [42] + QString s = "Banana"; + s.replace(QRegExp("a[mn]"), "ox"); + // s == "Boxoxa" + //! [42] + + //! [43] + QString t = "A <i>bon mot</i>."; + t.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}"); + // t == "A \\emph{bon mot}." + //! [43] + + //! [86] + QString equis = "xxxxxx"; + equis.replace("xx", "x"); + // equis == "xxx" + //! [86] + + //! [87] + QString s = "Banana"; + s.replace(QRegularExpression("a[mn]"), "ox"); + // s == "Boxoxa" + //! [87] + + //! [88] + QString t = "A <i>bon mot</i>."; + t.replace(QRegularExpression("<i>([^<]*)</i>"), "\\emph{\\1}"); + // t == "A \\emph{bon mot}." + //! [88] +} + +void Widget::reserveFunction() +{ + //! [44] + QString result; + int maxSize; + bool condition; + QChar nextChar; + + result.reserve(maxSize); + + while (condition) + result.append(nextChar); + + result.squeeze(); + //! [44] +} + +void Widget::resizeFunction() +{ + //! [45] + QString s = "Hello world"; + s.resize(5); + // s == "Hello" + + s.resize(8); + // s == "Hello???" (where ? stands for any character) + //! [45] + + //! [46] + QString t = "Hello"; + t += QString(10, 'X'); + // t == "HelloXXXXXXXXXX" + //! [46] + + //! [47] + QString r = "Hello"; + r = r.leftJustified(10, ' '); + // r == "Hello " + //! [47] +} + +void Widget::rightFunction() +{ + //! [48] + QString x = "Pineapple"; + QString y = x.right(5); // y == "apple" + //! [48] +} + +void Widget::rightJustifiedFunction() +{ + //! [49] + QString s = "apple"; + QString t = s.rightJustified(8, '.'); // t == "...apple" + //! [49] + + //! [50] + QString str = "Pineapple"; + str = str.rightJustified(5, '.', true); // str == "Pinea" + //! [50] +} + +void Widget::sectionFunction() +{ + //! [51] //! [52] + QString str; + //! [51] + QString csv = "forename,middlename,surname,phone"; + QString path = "/usr/local/bin/myapp"; // First field is empty + QString::SectionFlag flag = QString::SectionSkipEmpty; + + + str = csv.section(',', 2, 2); // str == "surname" + str = path.section('/', 3, 4); // str == "bin/myapp" + str = path.section('/', 3, 3, flag); // str == "myapp" + //! [52] + + //! [53] + str = csv.section(',', -3, -2); // str == "middlename,surname" + str = path.section('/', -1); // str == "myapp" + //! [53] + + //! [54] + QString data = "forename**middlename**surname**phone"; + + str = data.section("**", 2, 2); // str == "surname" + str = data.section("**", -3, -2); // str == "middlename**surname" + //! [54] + + //! [55] + QString line = "forename\tmiddlename surname \t \t phone"; + QRegExp sep("\\s+"); + str = line.section(sep, 2, 2); // str == "surname" + str = line.section(sep, -3, -2); // str == "middlename surname" + //! [55] + + //! [89] + QString line = "forename\tmiddlename surname \t \t phone"; + QRegularExpression sep("\\s+"); + str = line.section(sep, 2, 2); // str == "surname" + str = line.section(sep, -3, -2); // str == "middlename surname" + //! [89] +} + +void Widget::setNumFunction() +{ + //! [56] + QString str; + str.setNum(1234); // str == "1234" + //! [56] +} + +void Widget::simplifiedFunction() +{ + //! [57] + QString str = " lots\t of\nwhitespace\r\n "; + str = str.simplified(); + // str == "lots of whitespace"; + //! [57] +} + +void Widget::sizeFunction() +{ + //! [58] + QString str = "World"; + int n = str.size(); // n == 5 + str.data()[0]; // returns 'W' + str.data()[4]; // returns 'd' + str.data()[5]; // returns '\0' + //! [58] +} + +void Widget::splitFunction() +{ + //! [59] + QString str; + QStringList list; + + str = "Some text\n\twith strange whitespace."; + list = str.split(QRegExp("\\s+")); + // list: [ "Some", "text", "with", "strange", "whitespace." ] + //! [59] + + //! [60] + str = "This time, a normal English sentence."; + list = str.split(QRegExp("\\W+"), QString::SkipEmptyParts); + // list: [ "This", "time", "a", "normal", "English", "sentence" ] + //! [60] + + //! [61] + str = "Now: this sentence fragment."; + list = str.split(QRegExp("\\b")); + // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ] + //! [61] + + //! [90] + QString str; + QStringList list; + + str = "Some text\n\twith strange whitespace."; + list = str.split(QRegularExpression("\\s+")); + // list: [ "Some", "text", "with", "strange", "whitespace." ] + //! [90] + + //! [91] + str = "This time, a normal English sentence."; + list = str.split(QRegularExpression("\\W+"), QString::SkipEmptyParts); + // list: [ "This", "time", "a", "normal", "English", "sentence" ] + //! [91] + + //! [92] + str = "Now: this sentence fragment."; + list = str.split(QRegularExpression("\\b")); + // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ] + //! [92] +} + +void Widget::splitCaseSensitiveFunction() +{ + //! [62] + QString str = "a,,b,c"; + + QStringList list1 = str.split(","); + // list1: [ "a", "", "b", "c" ] + + QStringList list2 = str.split(",", QString::SkipEmptyParts); + // list2: [ "a", "b", "c" ] + //! [62] +} + +void Widget::sprintfFunction() +{ + //! [63] + size_t BufSize; + char buf[BufSize]; + + ::snprintf(buf, BufSize, "%lld", 123456789LL); + QString str = QString::fromAscii(buf); + //! [63] + + //! [64] + QString result; + QTextStream(&result) << "pi = " << 3.14; + // result == "pi = 3.14" + //! [64] +} + +void Widget::startsWithFunction() +{ + //! [65] + QString str = "Bananas"; + str.startsWith("Ban"); // returns true + str.startsWith("Car"); // returns false + //! [65] +} + +void Widget::toDoubleFunction() +{ + //! [66] + QString str = "1234.56"; + double val = str.toDouble(); // val == 1234.56 + //! [66] + + //! [67] + bool ok; + double d; + + d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456 + //! [67] + + //! [68] + d = QString( "1234,56" ).toDouble(&ok); // ok == false + d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56 + //! [68] + + //! [69] + d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false + d = QString( "1234567.89" ).toDouble(&ok); // ok == true + //! [69] +} + +void Widget::toFloatFunction() +{ + //! [71] + QString str1 = "1234.56"; + str1.toFloat(); // returns 1234.56 + + bool ok; + QString str2 = "R2D2"; + str2.toFloat(&ok); // returns 0.0, sets ok to false + //! [71] +} + +void Widget::toIntFunction() +{ + //! [72] + QString str = "FF"; + bool ok; + int hex = str.toInt(&ok, 16); // hex == 255, ok == true + int dec = str.toInt(&ok, 10); // dec == 0, ok == false + //! [72] +} + +void Widget::toLongFunction() +{ + //! [73] + QString str = "FF"; + bool ok; + + long hex = str.toLong(&ok, 16); // hex == 255, ok == true + long dec = str.toLong(&ok, 10); // dec == 0, ok == false + //! [73] +} + +void Widget::toLongLongFunction() +{ + //! [74] + QString str = "FF"; + bool ok; + + qint64 hex = str.toLongLong(&ok, 16); // hex == 255, ok == true + qint64 dec = str.toLongLong(&ok, 10); // dec == 0, ok == false + //! [74] +} + +void Widget::toLowerFunction() +{ + //! [75] + QString str = "Qt by NOKIA"; + str = str.toLower(); // str == "qt by nokia" + //! [75] +} + +void Widget::toShortFunction() +{ + //! [76] + QString str = "FF"; + bool ok; + + short hex = str.toShort(&ok, 16); // hex == 255, ok == true + short dec = str.toShort(&ok, 10); // dec == 0, ok == false + //! [76] +} + +void Widget::toUIntFunction() +{ + //! [77] + QString str = "FF"; + bool ok; + + uint hex = str.toUInt(&ok, 16); // hex == 255, ok == true + uint dec = str.toUInt(&ok, 10); // dec == 0, ok == false + //! [77] +} + +void Widget::toULongFunction() +{ + //! [78] + QString str = "FF"; + bool ok; + + ulong hex = str.toULong(&ok, 16); // hex == 255, ok == true + ulong dec = str.toULong(&ok, 10); // dec == 0, ok == false + //! [78] +} + +void Widget::toULongLongFunction() +{ + //! [79] + QString str = "FF"; + bool ok; + + quint64 hex = str.toULongLong(&ok, 16); // hex == 255, ok == true + quint64 dec = str.toULongLong(&ok, 10); // dec == 0, ok == false + //! [79] +} + +void Widget::toUShortFunction() +{ + //! [80] + QString str = "FF"; + bool ok; + + ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true + ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false + //! [80] +} + +void Widget::toUpperFunction() +{ + //! [81] + QString str = "TeXt"; + str = str.toUpper(); // str == "TEXT" + //! [81] +} + +void Widget::trimmedFunction() +{ + //! [82] + QString str = " lots\t of\nwhitespace\r\n "; + str = str.trimmed(); + // str == "lots\t of\nwhitespace" + //! [82] +} + +void Widget::truncateFunction() +{ + //! [83] + QString str = "Vladivostok"; + str.truncate(4); + // str == "Vlad" + //! [83] +} + +void Widget::plusEqualOperator() +{ + //! [84] + QString x = "free"; + QString y = "dom"; + x += y; + // x == "freedom" + //! [84] +} + +void Widget::arrayOperator() +{ + //! [85] + QString str; + + if (str[0] == QChar('?')) + str[0] = QChar('_'); + //! [85] +} + +void Widget::midRefFunction() +{ + //! [midRef] + QString x = "Nine pineapples"; + QStringRef y = x.midRef(5, 4); // y == "pine" + QStringRef z = x.midRef(5); // z == "pineapples" + //! [midRef] +} + +void Widget::leftRefFunction() +{ + //! [leftRef] + QString x = "Pineapple"; + QStringRef y = x.leftRef(4); // y == "Pine" + //! [leftRef] +} + +void Widget::rightRefFunction() +{ + //! [rightRef] + QString x = "Pineapple"; + QStringRef y = x.rightRef(5); // y == "apple" + //! [rightRef] +} + + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Widget widget; + widget.show(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qstring/stringbuilder.cpp b/src/corelib/doc/snippets/qstring/stringbuilder.cpp new file mode 100644 index 0000000000..53cf0b7be0 --- /dev/null +++ b/src/corelib/doc/snippets/qstring/stringbuilder.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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] + QString foo; + QString type = "long"; + + foo->setText(QLatin1String("vector<") + type + QLatin1String(">::iterator")); + + if (foo.startsWith("(" + type + ") 0x")) + ... +//! [0] + +//! [3] + DEFINES *= QT_USE_QSTRINGBUILDER +//! [3] + +//! [5] + #include <QStringBuilder> + + QString hello("hello"); + QStringRef el(&hello, 2, 3); + QLatin1String world("world"); + QString message = hello % el % world % QChar('!'); +//! [5] diff --git a/src/corelib/doc/snippets/qstringlist/main.cpp b/src/corelib/doc/snippets/qstringlist/main.cpp new file mode 100644 index 0000000000..8ca463371e --- /dev/null +++ b/src/corelib/doc/snippets/qstringlist/main.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** 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 <QtGui> +#include <iostream> +using namespace std; + +class Widget : public QWidget +{ +public: + Widget(QWidget *parent = 0); +}; + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +//! [0] + QStringList fonts; + fonts << "Arial" << "Helvetica" << "Times" << "Courier"; +//! [0] + +//! [1] + for (int i = 0; i < fonts.size(); ++i) + cout << fonts.at(i).toLocal8Bit().constData() << endl; +//! [1] + +//! [2] + QStringListIterator javaStyleIterator(fonts); + while (javaStyleIterator.hasNext()) + cout << javaStyleIterator.next().toLocal8Bit().constData() << endl; +//! [2] + +//! [3] + QStringList::const_iterator constIterator; + for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd(); + ++constIterator) + cout << (*constIterator).toLocal8Bit().constData() << endl; +//! [3] + +//! [4] + QString str = fonts.join(","); + // str == "Arial,Helvetica,Times,Courier" +//! [4] + +//! [5] //! [6] + QStringList list; +//! [5] + list = str.split(","); + // list: ["Arial", "Helvetica", "Times", "Courier"] +//! [6] + +//! [7] + QStringList monospacedFonts = fonts.filter(QRegExp("Courier|Fixed")); +//! [7] + +//! [8] + QStringList files; + files << "$QTDIR/src/moc/moc.y" + << "$QTDIR/src/moc/moc.l" + << "$QTDIR/include/qconfig.h"; + + files.replaceInStrings("$QTDIR", "/usr/lib/qt"); + // files: [ "/usr/lib/qt/src/moc/moc.y", ...] +//! [8] + + QString str1, str2, str3; +//! [9] + QStringList longerList = (QStringList() << str1 << str2 << str3); +//! [9] + + list.clear(); +//! [10] + list << "Bill Murray" << "John Doe" << "Bill Clinton"; + +//! [11] + QStringList result; +//! [11] + result = list.filter("Bill"); + // result: ["Bill Murray", "Bill Clinton"] +//! [10] + + result.clear(); +//! [12] + foreach (const QString &str, list) { + if (str.contains("Bill")) + result += str; + } +//! [12] + + list.clear(); +//! [13] + list << "alpha" << "beta" << "gamma" << "epsilon"; + list.replaceInStrings("a", "o"); + // list == ["olpho", "beto", "gommo", "epsilon"] +//! [13] + + list.clear(); +//! [14] + list << "alpha" << "beta" << "gamma" << "epsilon"; + list.replaceInStrings(QRegExp("^a"), "o"); + // list == ["olpha", "beta", "gamma", "epsilon"] +//! [14] + + list.clear(); +//! [15] + list << "Bill Clinton" << "Murray, Bill"; + list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1"); + // list == ["Bill Clinton", "Bill Murray"] +//! [15] + + list.clear(); +//! [16] + list << "alpha" << "beta" << "gamma" << "epsilon"; + list.replaceInStrings(QRegularExpression("^a"), "o"); + // list == ["olpha", "beta", "gamma", "epsilon"] +//! [16] + + list.clear(); +//! [17] + list << "Bill Clinton" << "Murray, Bill"; + list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1"); + // list == ["Bill Clinton", "Bill Murray"] +//! [17] + +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Widget widget; + widget.show(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qstringlistmodel/main.cpp b/src/corelib/doc/snippets/qstringlistmodel/main.cpp new file mode 100644 index 0000000000..24de0fd0ae --- /dev/null +++ b/src/corelib/doc/snippets/qstringlistmodel/main.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$ +** +****************************************************************************/ + +#include <QtGui> + +class Widget : public QWidget +{ +public: + Widget(QWidget *parent = 0); +}; + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +//! [0] + QStringListModel *model = new QStringListModel(); + QStringList list; + list << "a" << "b" << "c"; + model->setStringList(list); +//! [0] +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Widget widget; + widget.show(); + return app.exec(); +} diff --git a/src/corelib/doc/snippets/qtcast/qtcast.cpp b/src/corelib/doc/snippets/qtcast/qtcast.cpp new file mode 100644 index 0000000000..f0c3834bf0 --- /dev/null +++ b/src/corelib/doc/snippets/qtcast/qtcast.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** 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 <QLabel> +#include <QPushButton> + +#include "qtcast.h" + +MyWidget::MyWidget() +{ +//! [0] + QObject *obj = new MyWidget; +//! [0] + +//! [1] + QWidget *widget = qobject_cast<QWidget *>(obj); +//! [1] + +//! [2] + MyWidget *myWidget = qobject_cast<MyWidget *>(obj); +//! [2] + +//! [3] + QLabel *label = qobject_cast<QLabel *>(obj); +//! [3] //! [4] + // label is 0 +//! [4] + +//! [5] + if (QLabel *label = qobject_cast<QLabel *>(obj)) { +//! [5] //! [6] + label->setText(tr("Ping")); + } else if (QPushButton *button = qobject_cast<QPushButton *>(obj)) { + button->setText(tr("Pong!")); + } +//! [6] +} + +int main() +{ + return 0; +} diff --git a/src/corelib/doc/snippets/qxmlstreamwriter/main.cpp b/src/corelib/doc/snippets/qxmlstreamwriter/main.cpp new file mode 100644 index 0000000000..907d779357 --- /dev/null +++ b/src/corelib/doc/snippets/qxmlstreamwriter/main.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 <QCoreApplication> +#include <QFile> +#include <QXmlStreamWriter> +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QFile output; + output.open(stdout, QIODevice::WriteOnly); +//! [write output] +//! [start stream] + QXmlStreamWriter stream(&output); + stream.setAutoFormatting(true); + stream.writeStartDocument(); +//! [start stream] + stream.writeDTD("<!DOCTYPE xbel>"); + stream.writeStartElement("xbel"); + stream.writeAttribute("version", "1.0"); + stream.writeStartElement("folder"); + stream.writeAttribute("folded", "no"); +//! [write element] + stream.writeStartElement("bookmark"); + stream.writeAttribute("href", "http://qt.nokia.com/"); + stream.writeTextElement("title", "Qt Home"); + stream.writeEndElement(); // bookmark +//! [write element] + stream.writeEndElement(); // folder + stream.writeEndElement(); // xbel +//! [finish stream] + stream.writeEndDocument(); +//! [finish stream] +//! [write output] + output.close(); + return 0; +} diff --git a/src/corelib/doc/snippets/settings/settings.cpp b/src/corelib/doc/snippets/settings/settings.cpp new file mode 100644 index 0000000000..8648843d43 --- /dev/null +++ b/src/corelib/doc/snippets/settings/settings.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** 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 <QtGui> + +QWidget *win; +QWidget *panel; + +void snippet_ctor1() +{ +//! [0] + QSettings settings("MySoft", "Star Runner"); +//! [0] +} + +void snippet_ctor2() +{ +//! [1] + QCoreApplication::setOrganizationName("MySoft"); +//! [1] //! [2] + QCoreApplication::setOrganizationDomain("mysoft.com"); +//! [2] //! [3] + QCoreApplication::setApplicationName("Star Runner"); +//! [3] + +//! [4] + QSettings settings; +//! [4] + +//! [5] + settings.setValue("editor/wrapMargin", 68); +//! [5] //! [6] + int margin = settings.value("editor/wrapMargin").toInt(); +//! [6] + { +//! [7] + int margin = settings.value("editor/wrapMargin", 80).toInt(); +//! [7] + } + +//! [8] + settings.setValue("mainwindow/size", win->size()); +//! [8] //! [9] + settings.setValue("mainwindow/fullScreen", win->isFullScreen()); +//! [9] //! [10] + settings.setValue("outputpanel/visible", panel->isVisible()); +//! [10] + +//! [11] + settings.beginGroup("mainwindow"); + settings.setValue("size", win->size()); + settings.setValue("fullScreen", win->isFullScreen()); + settings.endGroup(); +//! [11] + +//! [12] + settings.beginGroup("outputpanel"); + settings.setValue("visible", panel->isVisible()); + settings.endGroup(); +//! [12] +} + +void snippet_locations() +{ +//! [13] + QSettings obj1("MySoft", "Star Runner"); +//! [13] //! [14] + QSettings obj2("MySoft"); + QSettings obj3(QSettings::SystemScope, "MySoft", "Star Runner"); + QSettings obj4(QSettings::SystemScope, "MySoft"); +//! [14] + + { +//! [15] + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + "MySoft", "Star Runner"); +//! [15] + } + + { + QSettings settings("starrunner.ini", QSettings::IniFormat); + } + + { + QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft", + QSettings::NativeFormat); + } +} + +class MainWindow : public QMainWindow +{ +public: + MainWindow(); + + void writeSettings(); + void readSettings(); + +protected: + void closeEvent(QCloseEvent *event); +}; + +//! [16] +void MainWindow::writeSettings() +{ + QSettings settings("Moose Soft", "Clipper"); + + settings.beginGroup("MainWindow"); + settings.setValue("size", size()); + settings.setValue("pos", pos()); + settings.endGroup(); +} +//! [16] + +//! [17] +void MainWindow::readSettings() +{ + QSettings settings("Moose Soft", "Clipper"); + + settings.beginGroup("MainWindow"); + resize(settings.value("size", QSize(400, 400)).toSize()); + move(settings.value("pos", QPoint(200, 200)).toPoint()); + settings.endGroup(); +} +//! [17] + +//! [18] +MainWindow::MainWindow() +{ +//! [18] //! [19] + readSettings(); +//! [19] //! [20] +} +//! [20] + +bool userReallyWantsToQuit() { return true; } + +//! [21] +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (userReallyWantsToQuit()) { + writeSettings(); + event->accept(); + } else { + event->ignore(); + } +} +//! [21] diff --git a/src/corelib/doc/snippets/sharedemployee/employee.h b/src/corelib/doc/snippets/sharedemployee/employee.h new file mode 100644 index 0000000000..efb1d32000 --- /dev/null +++ b/src/corelib/doc/snippets/sharedemployee/employee.h @@ -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$ +** +****************************************************************************/ + +#ifndef EMPLOYEE_H +#define EMPLOYEE_H + +//! [0] +#include <QSharedData> +#include <QString> + +class EmployeeData : public QSharedData +{ + public: + EmployeeData() : id(-1) { } + EmployeeData(const EmployeeData &other) + : QSharedData(other), id(other.id), name(other.name) { } + ~EmployeeData() { } + + int id; + QString name; +}; + +class Employee +{ + public: +//! [1] + Employee() { d = new EmployeeData; } +//! [1] //! [2] + Employee(int id, QString name) { + d = new EmployeeData; + setId(id); + setName(name); + } +//! [2] //! [7] + Employee(const Employee &other) + : d (other.d) + { + } +//! [7] +//! [3] + void setId(int id) { d->id = id; } +//! [3] //! [4] + void setName(QString name) { d->name = name; } +//! [4] + +//! [5] + int id() const { return d->id; } +//! [5] //! [6] + QString name() const { return d->name; } +//! [6] + + private: + QSharedDataPointer<EmployeeData> d; +}; +//! [0] + +#endif diff --git a/src/corelib/doc/snippets/sharedemployee/main.cpp b/src/corelib/doc/snippets/sharedemployee/main.cpp new file mode 100644 index 0000000000..6e840ab5e6 --- /dev/null +++ b/src/corelib/doc/snippets/sharedemployee/main.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] +#include "employee.h" + +int main() +{ + Employee e1(1001, "Albrecht Durer"); + Employee e2 = e1; + e1.setName("Hans Holbein"); +} +//! [0] diff --git a/src/corelib/doc/snippets/signalmapper/filereader.cpp b/src/corelib/doc/snippets/signalmapper/filereader.cpp new file mode 100644 index 0000000000..067ba8280e --- /dev/null +++ b/src/corelib/doc/snippets/signalmapper/filereader.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the config.tests 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 <QtGui> +#include "filereader.h" + + +FileReader::FileReader(QWidget *parent) + : QWidget(parent) +{ + textEdit = new QTextEdit; + + taxFileButton = new QPushButton("Tax File"); + accountFileButton = new QPushButton("Accounts File"); + reportFileButton = new QPushButton("Report File"); + +//! [0] + signalMapper = new QSignalMapper(this); + signalMapper->setMapping(taxFileButton, QString("taxfile.txt")); + signalMapper->setMapping(accountFileButton, QString("accountsfile.txt")); + signalMapper->setMapping(reportFileButton, QString("reportfile.txt")); + + connect(taxFileButton, &QPushButton::clicked, + signalMapper, &QSignalMapper::map); + connect(accountFileButton, &QPushButton::clicked, + signalMapper, &QSignalMapper::map); + connect(reportFileButton, &QPushButton::clicked, + signalMapper, &QSignalMapper::map); +//! [0] + +//! [1] + connect(signalMapper, SIGNAL(mapped(QString)), + this, SLOT(readFile(QString))); +//! [1] + +/* +//! [2] + //slower due to signature normalization at runtime + + connect(signalMapper, SIGNAL(mapped(const QString &)), + this, SLOT(readFile(const QString &))); +//! [2] +*/ + QHBoxLayout *buttonLayout = new QHBoxLayout; + buttonLayout->addWidget(taxFileButton); + buttonLayout->addWidget(accountFileButton); + buttonLayout->addWidget(reportFileButton); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(textEdit); + mainLayout->addLayout(buttonLayout); + + setLayout(mainLayout); +} + +void FileReader::readFile(const QString &filename) +{ + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + + QTextStream in(&file); + textEdit->setPlainText(in.readAll()); +} + diff --git a/src/corelib/doc/snippets/signalsandslots/lcdnumber.h b/src/corelib/doc/snippets/signalsandslots/lcdnumber.h new file mode 100644 index 0000000000..7056254f9f --- /dev/null +++ b/src/corelib/doc/snippets/signalsandslots/lcdnumber.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** 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] +#ifndef LCDNUMBER_H +//! [0] //! [1] +#define LCDNUMBER_H +//! [1] + +//! [2] +#include <QFrame> +//! [2] + +//! [3] +class LcdNumber : public QFrame +//! [3] //! [4] +{ +//! [4] //! [5] + Q_OBJECT +//! [5] + +//! [6] +public: +//! [6] //! [7] + LcdNumber(QWidget *parent = 0); +//! [7] + +//! [8] +signals: +//! [8] //! [9] + void overflow(); +//! [9] + +//! [10] +public slots: +//! [10] //! [11] + void display(int num); + void display(double num); + void display(const QString &str); + void setHexMode(); + void setDecMode(); + void setOctMode(); + void setBinMode(); + void setSmallDecimalPoint(bool point); +//! [11] //! [12] +}; +//! [12] + +//! [13] +#endif +//! [13] diff --git a/src/corelib/doc/snippets/signalsandslots/signalsandslots.cpp b/src/corelib/doc/snippets/signalsandslots/signalsandslots.cpp new file mode 100644 index 0000000000..3072650585 --- /dev/null +++ b/src/corelib/doc/snippets/signalsandslots/signalsandslots.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$ +** +****************************************************************************/ + +#include <QAbstractButton> + +#include "signalsandslots.h" + +//! [0] +void Counter::setValue(int value) +{ + if (value != m_value) { + m_value = value; + emit valueChanged(value); + } +} +//! [0] + +int main() +{ +//! [1] + Counter a, b; +//! [1] //! [2] + QObject::connect(&a, &Counter::valueChanged, + &b, &Counter::setValue); +//! [2] + +//! [3] + a.setValue(12); // a.value() == 12, b.value() == 12 +//! [3] //! [4] + b.setValue(48); // a.value() == 12, b.value() == 48 +//! [4] + + + QWidget *widget = reinterpret_cast<QWidget *>(new QObject(0)); +//! [5] + if (widget->inherits("QAbstractButton")) { + QAbstractButton *button = static_cast<QAbstractButton *>(widget); + button->toggle(); +//! [5] //! [6] + } +//! [6] + +//! [7] + if (QAbstractButton *button = qobject_cast<QAbstractButton *>(widget)) + button->toggle(); +//! [7] +} diff --git a/src/corelib/doc/snippets/signalsandslots/signalsandslots.h b/src/corelib/doc/snippets/signalsandslots/signalsandslots.h new file mode 100644 index 0000000000..e18f955a41 --- /dev/null +++ b/src/corelib/doc/snippets/signalsandslots/signalsandslots.h @@ -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$ +** +****************************************************************************/ + +#ifndef SIGNALSANDSLOTS_H +#define SIGNALSANDSLOTS_H + +#define Counter PlainCounter + +//! [0] +class Counter +{ +public: + Counter() { m_value = 0; } + + int value() const { return m_value; } + void setValue(int value); + +private: + int m_value; +}; +//! [0] + +#undef Counter +#define Counter ObjectCounter + +//! [1] +#include <QObject> +//! [1] + +//! [2] +class Counter : public QObject +//! [2] //! [3] +{ + Q_OBJECT + +public: + Counter() { m_value = 0; } + + int value() const { return m_value; } + +public slots: + void setValue(int value); + +signals: + void valueChanged(int newValue); + +private: + int m_value; +}; +//! [3] + +#endif diff --git a/src/corelib/doc/snippets/streaming/main.cpp b/src/corelib/doc/snippets/streaming/main.cpp new file mode 100644 index 0000000000..7c992345c2 --- /dev/null +++ b/src/corelib/doc/snippets/streaming/main.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$ +** +****************************************************************************/ + +#include <QtCore> +#include <QByteArray> +#include <QDataStream> + +//! [0] +struct Movie +{ + int id; + QString title; + QDate releaseDate; +}; +//! [0] + +//! [1] +QDataStream &operator<<(QDataStream &out, const Movie &movie) +{ + out << (quint32)movie.id << movie.title + << movie.releaseDate; + return out; +} +//! [1] + +//! [2] +QDataStream &operator>>(QDataStream &in, Movie &movie) +{ + quint32 id; + QDate date; + + in >> id >> movie.title >> date; + movie.id = (int)id; + movie.releaseDate = date; + return in; +} +//! [2] + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + Movie m; + m.id = 0001; + m.title = "Happy Meal"; + m.releaseDate = QDate(1995, 5, 17); + + QByteArray byteArray; + QDataStream stream(&byteArray, QIODevice::WriteOnly); + stream << m; + + // display + qDebug() << m.id << m.releaseDate << m.title; + + Movie m2; + + int id2; + QString title2; + QDate date2; + + QDataStream stream2(byteArray); + stream2 >> id2 >> title2 >> date2; + + m2.id = id2; + m2.title = title2; + m2.releaseDate = date2; + + qDebug() << id2 << " " << date2 << " " << title2; + + return 0; +} diff --git a/src/corelib/doc/snippets/streaming/streaming.pro b/src/corelib/doc/snippets/streaming/streaming.pro new file mode 100644 index 0000000000..8c5eb91f05 --- /dev/null +++ b/src/corelib/doc/snippets/streaming/streaming.pro @@ -0,0 +1,2 @@ +SOURCES = main.cpp +CONFIG += CONSOLE
\ No newline at end of file diff --git a/src/corelib/doc/snippets/threads/threads.cpp b/src/corelib/doc/snippets/threads/threads.cpp new file mode 100644 index 0000000000..9ef716705a --- /dev/null +++ b/src/corelib/doc/snippets/threads/threads.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** 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 <QCache> +#include <QMutex> +#include <QThreadStorage> + +#include "threads.h" + +//! [0] +void MyThread::run() +//! [0] //! [1] +{ +//! [1] //! [2] +} +//! [2] + +#define Counter ReentrantCounter + +//! [3] +class Counter +//! [3] //! [4] +{ +public: + Counter() { n = 0; } + + void increment() { ++n; } + void decrement() { --n; } + int value() const { return n; } + +private: + int n; +}; +//! [4] + +#undef Counter +#define Counter ThreadSafeCounter + +//! [5] +class Counter +//! [5] //! [6] +{ +public: + Counter() { n = 0; } + + void increment() { QMutexLocker locker(&mutex); ++n; } + void decrement() { QMutexLocker locker(&mutex); --n; } + int value() const { QMutexLocker locker(&mutex); return n; } + +private: + mutable QMutex mutex; + int n; +}; +//! [6] + +typedef int SomeClass; + +//! [7] +QThreadStorage<QCache<QString, SomeClass> > caches; + +void cacheObject(const QString &key, SomeClass *object) +//! [7] //! [8] +{ + caches.localData().insert(key, object); +} + +void removeFromCache(const QString &key) +//! [8] //! [9] +{ + if (!caches.hasLocalData()) + return; + + caches.localData().remove(key); +} +//! [9] + +int main() +{ + return 0; +} diff --git a/src/corelib/doc/snippets/threads/threads.h b/src/corelib/doc/snippets/threads/threads.h new file mode 100644 index 0000000000..a45a2c92da --- /dev/null +++ b/src/corelib/doc/snippets/threads/threads.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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 <QThread> + +//! [0] +class MyThread : public QThread +{ + Q_OBJECT + +protected: + void run(); +}; +//! [0] diff --git a/src/corelib/doc/snippets/timers/timers.cpp b/src/corelib/doc/snippets/timers/timers.cpp new file mode 100644 index 0000000000..65b1c0d146 --- /dev/null +++ b/src/corelib/doc/snippets/timers/timers.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$ +** +****************************************************************************/ + +#include <QTimer> + +class Foo : public QObject +{ +public: + Foo(); +}; + +Foo::Foo() +{ +//! [0] + QTimer *timer = new QTimer(this); +//! [0] //! [1] + connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption())); +//! [1] //! [2] + timer->start(1000); +//! [2] + +//! [3] + QTimer::singleShot(200, this, SLOT(updateCaption())); +//! [3] + + { + // ZERO-CASE +//! [4] + QTimer *timer = new QTimer(this); +//! [4] //! [5] + connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing())); +//! [5] //! [6] + timer->start(); +//! [6] + } +} + +int main() +{ + +} |