diff options
Diffstat (limited to 'src/corelib/doc')
5 files changed, 136 insertions, 2 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp index 2d7b9a9ac8..9d029e5d4d 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -440,6 +440,54 @@ QString global_greeting(int type) //! [36] +//! [qttrnnoop] +static const char * const StatusClass::status_strings[] = { + QT_TR_N_NOOP("There are %n new message(s)"), + QT_TR_N_NOOP("There are %n total message(s)") +}; + +QString StatusClass::status(int type, int count) +{ + return tr(status_strings[type], nullptr, count); +} +//! [qttrnnoop] + +//! [qttranslatennoop] +static const char * const greeting_strings[] = { + QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"), + QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)") +}; + +QString global_greeting(int type, int msgcnt) +{ + return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt); +} +//! [qttranslatennoop] + +//! [qttranslatennoop3] +static { const char * const source; const char * const comment; } status_strings[] = { + QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)", + "A login message status"), + QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)", + "A new message query status") +}; + +QString FriendlyConversation::greeting(int type, int count) +{ + return tr(status_strings[type].source, + status_strings[type].comment, count); +} + +QString global_greeting(int type, int count) +{ + return qApp->translate("Message Status", + status_strings[type].source, + status_strings[type].comment, + count); +} +//! [qttranslatennoop3] + + //! [qttrid] //% "%n fooish bar(s) found.\n" //% "Do you want to continue?" diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp index d163129d54..3394358003 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp @@ -353,9 +353,21 @@ long dec = str.toLong(&ok, 10); // dec == 0, ok == false //! [38] QByteArray string("1234.56"); -double a = string.toDouble(); // a == 1234.56 +bool ok; +double a = string.toDouble(&ok); // a == 1234.56, ok == true + +string = "1234.56 Volt"; +a = str.toDouble(&ok); // a == 0, ok == false //! [38] +//! [38float] +QByteArray string("1234.56"); +bool ok; +double a = string.toFloat(&ok); // a == 1234.56, ok == true + +string = "1234.56 Volt"; +a = str.toFloat(&ok); // a == 0, ok == false +//! [38float] //! [39] QByteArray text("Qt is great!"); diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp new file mode 100644 index 0000000000..c8c5f694c6 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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 The Qt Company Ltd 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 myComplexCodeWithMultipleReturnPoints(int v) +{ + // The lambda will be executed right before your function returns + auto cleanup = qScopeGuard([] { code you want executed goes HERE; }); + + if (v == -1) + return; + + int v2 = code_that_might_through_exceptions(); + + if (v2 == -1) + return; + + (...) +} +//! [0] diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index b936f0c057..5b640f3fdc 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -853,6 +853,8 @@ void Widget::toDoubleFunction() double d; d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456 + + d = QString( "1234.56e-02 Volt" ).toDouble(&ok); // ok == false, d == 0 //! [67] //! [68] @@ -875,6 +877,9 @@ void Widget::toFloatFunction() bool ok; QString str2 = "R2D2"; str2.toFloat(&ok); // returns 0.0, sets ok to false + + QString str3 = "1234.56 Volt"; + str3.toFloat(&ok); // returns 0.0, sets ok to false //! [71] } diff --git a/src/corelib/doc/src/objectmodel/signalsandslots.qdoc b/src/corelib/doc/src/objectmodel/signalsandslots.qdoc index 6d3064d217..0cbbef0502 100644 --- a/src/corelib/doc/src/objectmodel/signalsandslots.qdoc +++ b/src/corelib/doc/src/objectmodel/signalsandslots.qdoc @@ -361,9 +361,11 @@ You can also connect to functors or C++11 lambdas: \code - connect(sender, &QObject::destroyed, [=](){ this->m_objects.remove(sender); }); + connect(sender, &QObject::destroyed, context, [=](){ this->m_objects.remove(sender); }); \endcode + The lambda will be disconnected when the context is destroyed. + The other way to connect a signal to a slot is to use QObject::connect() and the \c{SIGNAL} and \c{SLOT} macros. The rule about whether to |