diff options
Diffstat (limited to 'examples/corelib/threads')
24 files changed, 330 insertions, 1049 deletions
diff --git a/examples/corelib/threads/CMakeLists.txt b/examples/corelib/threads/CMakeLists.txt index 848880d076..670821a77d 100644 --- a/examples/corelib/threads/CMakeLists.txt +++ b/examples/corelib/threads/CMakeLists.txt @@ -1,8 +1,11 @@ -# Generated from threads.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -qt_internal_add_example(semaphores) -qt_internal_add_example(waitconditions) -if(TARGET Qt::Widgets) +if(NOT ANDROID) + qt_internal_add_example(semaphores) + qt_internal_add_example(waitconditions) +endif() +if(TARGET Qt6::Widgets) qt_internal_add_example(mandelbrot) qt_internal_add_example(queuedcustomtype) endif() diff --git a/examples/corelib/threads/doc/src/mandelbrot.qdoc b/examples/corelib/threads/doc/src/mandelbrot.qdoc index 2b12743538..6081912e69 100644 --- a/examples/corelib/threads/doc/src/mandelbrot.qdoc +++ b/examples/corelib/threads/doc/src/mandelbrot.qdoc @@ -1,33 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example threads/mandelbrot - \title Mandelbrot Example + \examplecategory {Data Processing & I/O} + \title Mandelbrot \ingroup qtconcurrent-mtexamples \brief The Mandelbrot example demonstrates multi-thread programming @@ -37,17 +14,17 @@ \image mandelbrot-example.png Screenshot of the Mandelbrot example - The heavy computation here is the Mandelbrot set, probably the - world's most famous fractal. These days, while sophisticated - programs such as \l{http://matek.hu/xaos/doku.php}{XaoS} that provide real-time zooming in the - Mandelbrot set, the standard Mandelbrot algorithm is just slow - enough for our purposes. + The heavy computation here is the Mandelbrot set, probably the world's most + famous fractal. These days, while sophisticated programs, such as + \l{https://xaos-project.github.io/}{XaoS}, provide real-time zooming in + the Mandelbrot set, the standard Mandelbrot algorithm is just slow enough + for our purposes. In real life, the approach described here is applicable to a large set of problems, including synchronous network I/O and database access, where the user interface must remain responsive while some heavy operation is taking place. The \l - {Blocking Fortune Client Example} shows the same principle at + {Blocking Fortune Client} example shows the same principle at work in a TCP client. The Mandelbrot application supports zooming and scrolling using @@ -170,12 +147,12 @@ rendering parameters in local variables. As usual, we protect accesses to the member variables using the class's mutex. Storing the member variables in local variables allows us to minimize the - amout of code that needs to be protected by a mutex. This ensures + amount of code that needs to be protected by a mutex. This ensures that the main thread will never have to block for too long when it needs to access \c{RenderThread}'s member variables (e.g., in \c render()). - The \c forever keyword is, like \c foreach, a Qt pseudo-keyword. + The \c forever keyword is a Qt pseudo-keyword. \snippet threads/mandelbrot/renderthread.cpp 4 \snippet threads/mandelbrot/renderthread.cpp 5 @@ -327,6 +304,11 @@ \snippet threads/mandelbrot/mandelbrotwidget.cpp 13 + Pinch to zoom has been implemented with QGesture as outlined in + \l{Gestures in Widgets and Graphics View}. + + \snippet threads/mandelbrot/mandelbrotwidget.cpp gesture1 + When the user presses the left mouse button, we store the mouse pointer position in \c lastDragPos. diff --git a/examples/corelib/threads/doc/src/queuedcustomtype.qdoc b/examples/corelib/threads/doc/src/queuedcustomtype.qdoc index 86d5992294..8d56695c79 100644 --- a/examples/corelib/threads/doc/src/queuedcustomtype.qdoc +++ b/examples/corelib/threads/doc/src/queuedcustomtype.qdoc @@ -1,34 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example threads/queuedcustomtype - \title Queued Custom Type Example - \brief Demonstrates multi-thread programming using Qt. + \examplecategory {Data Processing & I/O} + \title Queued Custom Type \ingroup qtconcurrent-mtexamples \brief The Queued Custom Type example shows how to send custom types between @@ -42,20 +18,15 @@ \section1 Overview - In the \l{Custom Type Example}, we showed how to integrate custom types with - the meta-object system, enabling them to be stored in QVariant objects, written - out in debugging information and used in signal-slot communication. - - In this example, we create a new value class, \c Block, and register it - with the meta-object system to enable us to send instances of it between - threads using queued signals and slots. + In this example, we create a value class, \c Block, and register it with + the meta-object system to enable us to send instances of it between threads + using queued signals and slots. \section1 The Block Class - The \c Block class is similar to the \c Message class described in the - \l{Custom Type Example}. It provides the default constructor, copy - constructor and destructor in the public section of the class that the - meta-object system requires. It describes a colored rectangle. + The \c Block class provides the default constructor, copy constructor, and + a destructor in the public section of the class as required by the + meta-object system. The class describes a colored rectangle. \snippet threads/queuedcustomtype/block.h custom type definition and meta-type declaration @@ -151,9 +122,7 @@ This example showed how a custom type can be registered with the meta-object system so that it can be used with signal-slot connections - between threads. For ordinary communication involving direct signals and - slots, it is enough to simply declare the type in the way described in the - \l{Custom Type Example}. + between threads. In practice, both the Q_DECLARE_METATYPE() macro and the qRegisterMetaType() template function can be used to register custom types, but diff --git a/examples/corelib/threads/doc/src/semaphores.qdoc b/examples/corelib/threads/doc/src/semaphores.qdoc index 5293e80c0e..f5ff90b014 100644 --- a/examples/corelib/threads/doc/src/semaphores.qdoc +++ b/examples/corelib/threads/doc/src/semaphores.qdoc @@ -1,39 +1,15 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example threads/semaphores - \title Semaphores Example - \brief Demonstrates multi-thread programming using Qt. + \examplecategory {Data Processing & I/O} + \title Producer and Consumer using Semaphores \ingroup qtconcurrent-mtexamples - \brief The Semaphores example shows how to use QSemaphore to control - access to a circular buffer shared by a producer thread and a - consumer thread. + \brief The Producer and Consumer using Semaphores example shows how + to use QSemaphore to control access to a circular buffer shared + by a producer thread and a consumer thread. The producer writes data to the buffer until it reaches the end of the buffer, at which point it restarts from the beginning, @@ -54,7 +30,7 @@ An alternative to using QSemaphore to solve the producer-consumer problem is to use QWaitCondition and QMutex. This is what the - \l{Wait Conditions Example} does. + \l{Producer and Consumer using Wait Conditions} example does. \section1 Global Variables @@ -63,7 +39,7 @@ \snippet threads/semaphores/semaphores.cpp 0 - \c DataSize is the amout of data that the producer will generate. + \c DataSize is the amount of data that the producer will generate. To keep the example as simple as possible, we make it a constant. \c BufferSize is the size of the circular buffer. It is less than \c DataSize, meaning that at some point the producer will reach diff --git a/examples/corelib/threads/doc/src/waitconditions.qdoc b/examples/corelib/threads/doc/src/waitconditions.qdoc index cfac460345..d46442d079 100644 --- a/examples/corelib/threads/doc/src/waitconditions.qdoc +++ b/examples/corelib/threads/doc/src/waitconditions.qdoc @@ -1,39 +1,15 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example threads/waitconditions - \title Wait Conditions Example - \brief Demonstrates multi-thread programming using Qt. + \examplecategory {Data Processing & I/O} + \title Producer and Consumer using Wait Conditions \ingroup qtconcurrent-mtexamples - \brief The Wait Conditions example shows how to use QWaitCondition and - QMutex to control access to a circular buffer shared by a - producer thread and a consumer thread. + \brief The Producer and Consumer using Wait Conditions example shows + how to use QWaitCondition and QMutex to control access to a circular + buffer shared by a producer thread and a consumer thread. The producer writes data to the buffer until it reaches the end of the buffer, at which point it restarts from the beginning, @@ -54,7 +30,7 @@ An alternative to using QWaitCondition and QMutex to solve the producer-consumer problem is to use QSemaphore. This is what the - \l{Semaphores Example} does. + \l{Producer and Consumer using Semaphores} example does. \section1 Global Variables @@ -133,7 +109,7 @@ thread is the only one that can do anything; the consumer is blocked waiting for the \c bufferNotEmpty condition to be signalled (\c numUsedBytes is 0). Once the producer has put one - byte in the buffer, \c numUsedBytes is \c BufferSize - 1 and the + byte in the buffer, \c numUsedBytes is strictly greater than 0, and the \c bufferNotEmpty condition is signalled. At that point, two things can happen: Either the consumer thread takes over and reads that byte, or the producer gets to produce a second byte. diff --git a/examples/corelib/threads/mandelbrot/CMakeLists.txt b/examples/corelib/threads/mandelbrot/CMakeLists.txt index cb026b9bb0..be296918cc 100644 --- a/examples/corelib/threads/mandelbrot/CMakeLists.txt +++ b/examples/corelib/threads/mandelbrot/CMakeLists.txt @@ -1,47 +1,39 @@ -# Generated from mandelbrot.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(mandelbrot LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/mandelbrot") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) +qt_standard_project_setup() qt_add_executable(mandelbrot main.cpp mandelbrotwidget.cpp mandelbrotwidget.h renderthread.cpp renderthread.h ) + set_target_properties(mandelbrot PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) -target_link_libraries(mandelbrot PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets -) -if(UNIX AND NOT APPLE AND NOT HAIKU AND NOT INTEGRITY AND NOT VXWORKS) - target_link_libraries(mandelbrot PUBLIC - m - ) -endif() +target_link_libraries(mandelbrot PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets +) install(TARGETS mandelbrot - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET mandelbrot + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/threads/mandelbrot/main.cpp b/examples/corelib/threads/mandelbrot/main.cpp index 19ea8cf417..8aafebf7d7 100644 --- a/examples/corelib/threads/mandelbrot/main.cpp +++ b/examples/corelib/threads/mandelbrot/main.cpp @@ -1,63 +1,15 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mandelbrotwidget.h" +#include "renderthread.h" #include <QApplication> - -#include <QScreen> - #include <QCommandLineParser> #include <QCommandLineOption> #include <QDebug> -#include <QRect> + +using namespace Qt::StringLiterals; //! [0] int main(int argc, char *argv[]) @@ -65,10 +17,10 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QCommandLineParser parser; - parser.setApplicationDescription("Qt Mandelbrot Example"); + parser.setApplicationDescription(u"Qt Mandelbrot Example"_s); parser.addHelpOption(); parser.addVersionOption(); - QCommandLineOption passesOption("passes", "Number of passes (1-8)", "passes"); + QCommandLineOption passesOption(u"passes"_s, u"Number of passes (1-8)"_s, u"passes"_s); parser.addOption(passesOption); parser.process(app); @@ -84,11 +36,7 @@ int main(int argc, char *argv[]) } MandelbrotWidget widget; - const auto geometry = widget.screen()->availableGeometry(); - widget.resize((2 * geometry.size()) / 3); - const auto pos = (geometry.size() - widget.size()) / 2; - widget.move(geometry.topLeft() + QPoint(pos.width(), pos.height())); - + widget.grabGesture(Qt::PinchGesture); widget.show(); return app.exec(); } diff --git a/examples/corelib/threads/mandelbrot/mandelbrot.pro b/examples/corelib/threads/mandelbrot/mandelbrot.pro index 92010fdc1f..7edf01ff30 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrot.pro +++ b/examples/corelib/threads/mandelbrot/mandelbrot.pro @@ -6,8 +6,6 @@ SOURCES = main.cpp \ mandelbrotwidget.cpp \ renderthread.cpp -unix:!mac:!vxworks:!integrity:!haiku:LIBS += -lm - # install target.path = $$[QT_INSTALL_EXAMPLES]/corelib/threads/mandelbrot INSTALLS += target diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index a1431db0d3..bbe694831d 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -1,68 +1,23 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mandelbrotwidget.h" -#include <QPainter> +#include <QGesture> +#include <QGestureEvent> #include <QKeyEvent> +#include <QPainter> #include <math.h> //! [0] -const double DefaultCenterX = -0.637011; -const double DefaultCenterY = -0.0395159; -const double DefaultScale = 0.00403897; +constexpr double DefaultCenterX = -0.637011; +constexpr double DefaultCenterY = -0.0395159; +constexpr double DefaultScale = 0.00403897; -const double ZoomInFactor = 0.8; -const double ZoomOutFactor = 1 / ZoomInFactor; -const int ScrollStep = 20; +constexpr double ZoomInFactor = 0.8; +constexpr double ZoomOutFactor = 1 / ZoomInFactor; +constexpr int ScrollStep = 20; //! [0] //! [1] @@ -73,8 +28,7 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) : pixmapScale(DefaultScale), curScale(DefaultScale) { - help = tr("Use mouse wheel or the '+' and '-' keys to zoom. " - "Press and hold left mouse button to scroll."); + help = tr("Zoom with mouse wheel, +/- keys or pinch. Scroll with arrow keys or by dragging."); connect(&thread, &RenderThread::renderedImage, this, &MandelbrotWidget::updatePixmap); @@ -93,7 +47,8 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) if (pixmap.isNull()) { painter.setPen(Qt::white); - painter.drawText(rect(), Qt::AlignCenter, tr("Rendering initial image, please wait...")); + painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, + tr("Rendering initial image, please wait...")); //! [2] //! [3] return; //! [3] //! [4] @@ -107,37 +62,51 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) //! [6] //! [7] } else { //! [7] //! [8] - auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1)) + const auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1)) ? pixmap : pixmap.scaled(pixmap.deviceIndependentSize().toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation); - double scaleFactor = pixmapScale / curScale; - int newWidth = int(previewPixmap.width() * scaleFactor); - int newHeight = int(previewPixmap.height() * scaleFactor); - int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2; - int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2; + const double scaleFactor = pixmapScale / curScale; + const int newWidth = int(previewPixmap.width() * scaleFactor); + const int newHeight = int(previewPixmap.height() * scaleFactor); + const int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2; + const int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2; painter.save(); painter.translate(newX, newY); painter.scale(scaleFactor, scaleFactor); - QRectF exposed = painter.transform().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1); + const QRectF exposed = painter.transform().inverted().mapRect(rect()) + .adjusted(-1, -1, 1, 1); painter.drawPixmap(exposed, previewPixmap, exposed); painter.restore(); } //! [8] //! [9] - QString text = help; - if (!info.isEmpty()) - text += ' ' + info; - QFontMetrics metrics = painter.fontMetrics(); - int textWidth = metrics.horizontalAdvance(text); + const QFontMetrics metrics = painter.fontMetrics(); + if (!info.isEmpty()){ + const int infoWidth = metrics.horizontalAdvance(info); + const int infoHeight = (infoWidth/width() + 1) * (metrics.height() + 5); + + painter.setPen(Qt::NoPen); + painter.setBrush(QColor(0, 0, 0, 127)); + painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight); + + painter.setPen(Qt::white); + painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info); + } + + const int helpWidth = metrics.horizontalAdvance(help); + const int helpHeight = (helpWidth/width() + 1) * (metrics.height() + 5); painter.setPen(Qt::NoPen); painter.setBrush(QColor(0, 0, 0, 127)); - painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5); + painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, + helpHeight); + painter.setPen(Qt::white); - painter.drawText((width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text); + painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignBottom|Qt::TextWordWrap, help); + } //! [9] @@ -217,8 +186,8 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event) lastDragPos = QPoint(); const auto pixmapSize = pixmap.deviceIndependentSize().toSize(); - int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); - int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); + const int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); + const int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); scroll(deltaX, deltaY); } } @@ -258,3 +227,24 @@ void MandelbrotWidget::scroll(int deltaX, int deltaY) thread.render(centerX, centerY, curScale, size(), devicePixelRatio()); } //! [18] + +//! [gesture1] +#ifndef QT_NO_GESTURES +bool MandelbrotWidget::gestureEvent(QGestureEvent *event) +{ + if (auto *pinch = static_cast<QPinchGesture *>(event->gesture(Qt::PinchGesture))) { + if (pinch->changeFlags().testFlag(QPinchGesture::ScaleFactorChanged)) + zoom(1.0 / pinch->scaleFactor()); + return true; + } + return false; +} + +bool MandelbrotWidget::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast<QGestureEvent*>(event)); + return QWidget::event(event); +} +#endif +//! [gesture1] diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h index 956ffc10a8..0d0fce56b3 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h @@ -1,70 +1,29 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef MANDELBROTWIDGET_H #define MANDELBROTWIDGET_H +#include "renderthread.h" + +#include <QCoreApplication> #include <QPixmap> #include <QWidget> -#include "renderthread.h" +QT_BEGIN_NAMESPACE +class QGestureEvent; +QT_END_NAMESPACE //! [0] class MandelbrotWidget : public QWidget { - Q_OBJECT + Q_DECLARE_TR_FUNCTIONS(MandelbrotWidget) public: MandelbrotWidget(QWidget *parent = nullptr); protected: + QSize sizeHint() const override { return {1024, 768}; }; void paintEvent(QPaintEvent *event) override; void resizeEvent(QResizeEvent *event) override; void keyPressEvent(QKeyEvent *event) override; @@ -74,13 +33,17 @@ protected: void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; +#ifndef QT_NO_GESTURES + bool event(QEvent *event) override; +#endif -private slots: +private: void updatePixmap(const QImage &image, double scaleFactor); void zoom(double zoomFactor); - -private: void scroll(int deltaX, int deltaY); +#ifndef QT_NO_GESTURES + bool gestureEvent(QGestureEvent *event); +#endif RenderThread thread; QPixmap pixmap; diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp index 1f2af3b999..77a14a6ac1 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.cpp +++ b/examples/corelib/threads/mandelbrot/renderthread.cpp @@ -1,57 +1,9 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "renderthread.h" #include <QImage> - #include <QElapsedTimer> #include <QTextStream> @@ -117,16 +69,16 @@ void RenderThread::run() //! [3] //! [4] - int halfWidth = resultSize.width() / 2; + const int halfWidth = resultSize.width() / 2; //! [4] //! [5] - int halfHeight = resultSize.height() / 2; + const int halfHeight = resultSize.height() / 2; QImage image(resultSize, QImage::Format_RGB32); image.setDevicePixelRatio(devicePixelRatio); int pass = 0; while (pass < numPasses) { const int MaxIterations = (1 << (2 * pass + 6)) + 32; - const int Limit = 4; + constexpr int Limit = 4; bool allBlack = true; timer.restart(); diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h index 7bbd9ae056..ecd8ae084d 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.h +++ b/examples/corelib/threads/mandelbrot/renderthread.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef RENDERTHREAD_H #define RENDERTHREAD_H @@ -96,7 +49,7 @@ private: bool restart = false; bool abort = false; - enum { ColormapSize = 512 }; + static constexpr int ColormapSize = 512; uint colormap[ColormapSize]; }; //! [0] diff --git a/examples/corelib/threads/queuedcustomtype/CMakeLists.txt b/examples/corelib/threads/queuedcustomtype/CMakeLists.txt index 921aa1faa4..725c268615 100644 --- a/examples/corelib/threads/queuedcustomtype/CMakeLists.txt +++ b/examples/corelib/threads/queuedcustomtype/CMakeLists.txt @@ -1,23 +1,12 @@ -# Generated from queuedcustomtype.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(queuedcustomtype LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/queuedcustomtype") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) +qt_standard_project_setup() qt_add_executable(queuedcustomtype block.cpp block.h @@ -25,18 +14,27 @@ qt_add_executable(queuedcustomtype renderthread.cpp renderthread.h window.cpp window.h ) + set_target_properties(queuedcustomtype PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) -target_link_libraries(queuedcustomtype PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets + +target_link_libraries(queuedcustomtype PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets ) install(TARGETS queuedcustomtype - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET queuedcustomtype + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/threads/queuedcustomtype/block.cpp b/examples/corelib/threads/queuedcustomtype/block.cpp index 9cfad8673c..3fcc5383b7 100644 --- a/examples/corelib/threads/queuedcustomtype/block.cpp +++ b/examples/corelib/threads/queuedcustomtype/block.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "block.h" diff --git a/examples/corelib/threads/queuedcustomtype/block.h b/examples/corelib/threads/queuedcustomtype/block.h index eef48b25c2..cbebfb0e3d 100644 --- a/examples/corelib/threads/queuedcustomtype/block.h +++ b/examples/corelib/threads/queuedcustomtype/block.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BLOCK_H #define BLOCK_H diff --git a/examples/corelib/threads/queuedcustomtype/main.cpp b/examples/corelib/threads/queuedcustomtype/main.cpp index 1f25fafa1b..0a14cb48e1 100644 --- a/examples/corelib/threads/queuedcustomtype/main.cpp +++ b/examples/corelib/threads/queuedcustomtype/main.cpp @@ -1,59 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QApplication> -#include <QPainter> -#include <QTime> #include "block.h" #include "window.h" +#include <QApplication> +#include <QBrush> +#include <QImage> +#include <QPainter> +#include <QPen> +#include <QPointF> +#include <QRect> + QImage createImage(int width, int height) { QImage image(width, height, QImage::Format_RGB16); @@ -90,8 +48,8 @@ QImage createImage(int width, int height) int x = 0; int y = 0; - int starWidth = image.width()/3; - int starHeight = image.height()/3; + const int starWidth = image.width()/3; + const int starHeight = image.height()/3; QRect rect(x, y, starWidth, starHeight); diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.cpp b/examples/corelib/threads/queuedcustomtype/renderthread.cpp index 67cedf1e74..34a439f3fe 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.cpp +++ b/examples/corelib/threads/queuedcustomtype/renderthread.cpp @@ -1,69 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include "block.h" #include "renderthread.h" #include <QRandomGenerator> +#include <QRgb> RenderThread::RenderThread(QObject *parent) : QThread(parent) { - m_abort = false; } RenderThread::~RenderThread() { - mutex.lock(); - m_abort = true; - mutex.unlock(); - wait(); } @@ -74,27 +24,26 @@ void RenderThread::processImage(const QImage &image) return; m_image = image; - m_abort = false; start(); } void RenderThread::run() { - int size = qMax(m_image.width()/20, m_image.height()/20); + const int size = qMax(m_image.width()/20, m_image.height()/20); for (int s = size; s > 0; --s) { for (int c = 0; c < 400; ++c) { //![processing the image (start)] - int x1 = qMax(0, QRandomGenerator::global()->bounded(m_image.width()) - s/2); - int x2 = qMin(x1 + s/2 + 1, m_image.width()); - int y1 = qMax(0, QRandomGenerator::global()->bounded(m_image.height()) - s/2); - int y2 = qMin(y1 + s/2 + 1, m_image.height()); + const int x1 = qMax(0, QRandomGenerator::global()->bounded(m_image.width()) - s/2); + const int x2 = qMin(x1 + s/2 + 1, m_image.width()); + const int y1 = qMax(0, QRandomGenerator::global()->bounded(m_image.height()) - s/2); + const int y2 = qMin(y1 + s/2 + 1, m_image.height()); int n = 0; int red = 0; int green = 0; int blue = 0; for (int i = y1; i < y2; ++i) { for (int j = x1; j < x2; ++j) { - QRgb pixel = m_image.pixel(j, i); + const QRgb pixel = m_image.pixel(j, i); red += qRed(pixel); green += qGreen(pixel); blue += qBlue(pixel); @@ -102,20 +51,13 @@ void RenderThread::run() } } //![processing the image (finish)] - Block block(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1), + const Block block(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1), QColor(red/n, green/n, blue/n)); emit sendBlock(block); - if (m_abort) + if (isInterruptionRequested()) return; msleep(10); } } } //![processing the image (finish)] - -void RenderThread::stopProcess() -{ - mutex.lock(); - m_abort = true; - mutex.unlock(); -} diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.h b/examples/corelib/threads/queuedcustomtype/renderthread.h index 9375b60e74..c3152e4081 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.h +++ b/examples/corelib/threads/queuedcustomtype/renderthread.h @@ -1,60 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef RENDERTHREAD_H #define RENDERTHREAD_H #include <QImage> -#include <QMutex> #include <QThread> -#include "block.h" + +class Block; //! [RenderThread class definition] class RenderThread : public QThread @@ -70,16 +23,11 @@ public: signals: void sendBlock(const Block &block); -public slots: - void stopProcess(); - protected: void run(); private: - bool m_abort; QImage m_image; - QMutex mutex; }; //! [RenderThread class definition] diff --git a/examples/corelib/threads/queuedcustomtype/window.cpp b/examples/corelib/threads/queuedcustomtype/window.cpp index 183d9824cb..db12133206 100644 --- a/examples/corelib/threads/queuedcustomtype/window.cpp +++ b/examples/corelib/threads/queuedcustomtype/window.cpp @@ -1,55 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include "block.h" +#include "renderthread.h" #include "window.h" -#include <QtWidgets> + +#include <QFileDialog> +#include <QGuiApplication> +#include <QHBoxLayout> +#include <QImageReader> +#include <QPainter> +#include <QScreen> +#include <QVBoxLayout> //! [Window constructor start] Window::Window(QWidget *parent) @@ -67,7 +29,7 @@ Window::Window(QWidget *parent) connect(loadButton, &QPushButton::clicked, this, QOverload<>::of(&Window::loadImage)); connect(resetButton, &QPushButton::clicked, - thread, &RenderThread::stopProcess); + thread, &RenderThread::requestInterruption); connect(thread, &RenderThread::finished, this, &Window::resetUi); //! [set up widgets and connections] //! [connecting signal with custom type] @@ -98,13 +60,13 @@ void Window::loadImage() if (format.toLower() == format) formats.append(QLatin1String("*.") + QString::fromLatin1(format)); - QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"), + const QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"), path, tr("Image files (%1)").arg(formats.join(' '))); if (newPath.isEmpty()) return; - QImage image(newPath); + const QImage image(newPath); if (!image.isNull()) { loadImage(image); path = newPath; @@ -114,7 +76,7 @@ void Window::loadImage() void Window::loadImage(const QImage &image) { QImage useImage; - QRect space = QGuiApplication::primaryScreen()->availableGeometry(); + const QRect space = QGuiApplication::primaryScreen()->availableGeometry(); if (image.width() > 0.75*space.width() || image.height() > 0.75*space.height()) useImage = image.scaled(0.75*space.width(), 0.75*space.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); diff --git a/examples/corelib/threads/queuedcustomtype/window.h b/examples/corelib/threads/queuedcustomtype/window.h index 4215dfd5a7..a4f995696e 100644 --- a/examples/corelib/threads/queuedcustomtype/window.h +++ b/examples/corelib/threads/queuedcustomtype/window.h @@ -1,63 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef WINDOW_H #define WINDOW_H +#include <QImage> +#include <QLabel> +#include <QPixmap> +#include <QPushButton> #include <QWidget> -#include "renderthread.h" -QT_BEGIN_NAMESPACE -class QLabel; -class QPushButton; -QT_END_NAMESPACE +class Block; +class RenderThread; //! [Window class definition] class Window : public QWidget diff --git a/examples/corelib/threads/semaphores/CMakeLists.txt b/examples/corelib/threads/semaphores/CMakeLists.txt index e6ea405673..a096616e5a 100644 --- a/examples/corelib/threads/semaphores/CMakeLists.txt +++ b/examples/corelib/threads/semaphores/CMakeLists.txt @@ -1,35 +1,34 @@ -# Generated from semaphores.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(semaphores LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") +if (ANDROID) + message(FATAL_ERROR "This project cannot be built on Android.") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/semaphores") +find_package(Qt6 REQUIRED COMPONENTS Core) -find_package(Qt6 COMPONENTS Core) +qt_standard_project_setup() qt_add_executable(semaphores semaphores.cpp ) -set_target_properties(semaphores PROPERTIES - WIN32_EXECUTABLE FALSE - MACOSX_BUNDLE FALSE -) -target_link_libraries(semaphores PUBLIC - Qt::Core + +target_link_libraries(semaphores PRIVATE + Qt6::Core ) install(TARGETS semaphores - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET semaphores + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/threads/semaphores/semaphores.cpp b/examples/corelib/threads/semaphores/semaphores.cpp index a245b9906b..103f331b0c 100644 --- a/examples/corelib/threads/semaphores/semaphores.cpp +++ b/examples/corelib/threads/semaphores/semaphores.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include <QtCore> @@ -54,9 +7,9 @@ #include <stdlib.h> //! [0] -const int DataSize = 100000; +constexpr int DataSize = 100000; -const int BufferSize = 8192; +constexpr int BufferSize = 8192; char buffer[BufferSize]; QSemaphore freeBytes(BufferSize); @@ -83,7 +36,6 @@ public: class Consumer : public QThread //! [3] //! [4] { - Q_OBJECT public: void run() override { @@ -111,5 +63,3 @@ int main(int argc, char *argv[]) return 0; } //! [6] - -#include "semaphores.moc" diff --git a/examples/corelib/threads/waitconditions/CMakeLists.txt b/examples/corelib/threads/waitconditions/CMakeLists.txt index 233717c2aa..5fc44a079b 100644 --- a/examples/corelib/threads/waitconditions/CMakeLists.txt +++ b/examples/corelib/threads/waitconditions/CMakeLists.txt @@ -1,35 +1,34 @@ -# Generated from waitconditions.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(waitconditions LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") +if (ANDROID) + message(FATAL_ERROR "This project cannot be built on Android.") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/waitconditions") +find_package(Qt6 REQUIRED COMPONENTS Core) -find_package(Qt6 COMPONENTS Core) +qt_standard_project_setup() qt_add_executable(waitconditions waitconditions.cpp ) -set_target_properties(waitconditions PROPERTIES - WIN32_EXECUTABLE FALSE - MACOSX_BUNDLE FALSE -) -target_link_libraries(waitconditions PUBLIC - Qt::Core + +target_link_libraries(waitconditions PRIVATE + Qt6::Core ) install(TARGETS waitconditions - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET waitconditions + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/threads/waitconditions/waitconditions.cpp b/examples/corelib/threads/waitconditions/waitconditions.cpp index 963ee03a76..bdc24acd8c 100644 --- a/examples/corelib/threads/waitconditions/waitconditions.cpp +++ b/examples/corelib/threads/waitconditions/waitconditions.cpp @@ -1,68 +1,28 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples 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$ -** -****************************************************************************/ - -#include <QtCore> +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QCoreApplication> +#include <QMutex> +#include <QMutexLocker> +#include <QObject> +#include <QRandomGenerator> +#include <QThread> +#include <QWaitCondition> #include <stdio.h> #include <stdlib.h> //! [0] -const int DataSize = 100000; +constexpr int DataSize = 100000; +constexpr int BufferSize = 8192; -const int BufferSize = 8192; +QMutex mutex; // protects the buffer and the counter char buffer[BufferSize]; +int numUsedBytes; QWaitCondition bufferNotEmpty; QWaitCondition bufferNotFull; -QMutex mutex; -int numUsedBytes = 0; //! [0] //! [1] @@ -70,24 +30,28 @@ class Producer : public QThread //! [1] //! [2] { public: - Producer(QObject *parent = NULL) : QThread(parent) + explicit Producer(QObject *parent = nullptr) + : QThread(parent) { } +private: void run() override { for (int i = 0; i < DataSize; ++i) { - mutex.lock(); - if (numUsedBytes == BufferSize) - bufferNotFull.wait(&mutex); - mutex.unlock(); + { + const QMutexLocker locker(&mutex); + while (numUsedBytes == BufferSize) + bufferNotFull.wait(&mutex); + } buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)]; - mutex.lock(); - ++numUsedBytes; - bufferNotEmpty.wakeAll(); - mutex.unlock(); + { + const QMutexLocker locker(&mutex); + ++numUsedBytes; + bufferNotEmpty.wakeAll(); + } } } }; @@ -97,32 +61,32 @@ public: class Consumer : public QThread //! [3] //! [4] { - Q_OBJECT public: - Consumer(QObject *parent = NULL) : QThread(parent) + explicit Consumer(QObject *parent = nullptr) + : QThread(parent) { } +private: void run() override { for (int i = 0; i < DataSize; ++i) { - mutex.lock(); - if (numUsedBytes == 0) - bufferNotEmpty.wait(&mutex); - mutex.unlock(); + { + const QMutexLocker locker(&mutex); + while (numUsedBytes == 0) + bufferNotEmpty.wait(&mutex); + } fprintf(stderr, "%c", buffer[i % BufferSize]); - mutex.lock(); - --numUsedBytes; - bufferNotFull.wakeAll(); - mutex.unlock(); + { + const QMutexLocker locker(&mutex); + --numUsedBytes; + bufferNotFull.wakeAll(); + } } fprintf(stderr, "\n"); } - -signals: - void stringConsumed(const QString &text); }; //! [4] @@ -142,4 +106,3 @@ int main(int argc, char *argv[]) } //! [6] -#include "waitconditions.moc" |