diff options
Diffstat (limited to 'examples/corelib/threads')
21 files changed, 225 insertions, 209 deletions
diff --git a/examples/corelib/threads/CMakeLists.txt b/examples/corelib/threads/CMakeLists.txt index b5d42044fc..670821a77d 100644 --- a/examples/corelib/threads/CMakeLists.txt +++ b/examples/corelib/threads/CMakeLists.txt @@ -1,9 +1,11 @@ # 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 b8a5930d8c..6081912e69 100644 --- a/examples/corelib/threads/doc/src/mandelbrot.qdoc +++ b/examples/corelib/threads/doc/src/mandelbrot.qdoc @@ -3,7 +3,8 @@ /*! \example threads/mandelbrot - \title Mandelbrot Example + \examplecategory {Data Processing & I/O} + \title Mandelbrot \ingroup qtconcurrent-mtexamples \brief The Mandelbrot example demonstrates multi-thread programming @@ -13,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 @@ -151,7 +152,7 @@ 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 diff --git a/examples/corelib/threads/doc/src/queuedcustomtype.qdoc b/examples/corelib/threads/doc/src/queuedcustomtype.qdoc index df904cc438..8d56695c79 100644 --- a/examples/corelib/threads/doc/src/queuedcustomtype.qdoc +++ b/examples/corelib/threads/doc/src/queuedcustomtype.qdoc @@ -3,8 +3,8 @@ /*! \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 @@ -18,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 @@ -127,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 b8e1ab1b52..f5ff90b014 100644 --- a/examples/corelib/threads/doc/src/semaphores.qdoc +++ b/examples/corelib/threads/doc/src/semaphores.qdoc @@ -3,13 +3,13 @@ /*! \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, @@ -30,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 diff --git a/examples/corelib/threads/doc/src/waitconditions.qdoc b/examples/corelib/threads/doc/src/waitconditions.qdoc index 90f911aab3..d46442d079 100644 --- a/examples/corelib/threads/doc/src/waitconditions.qdoc +++ b/examples/corelib/threads/doc/src/waitconditions.qdoc @@ -3,13 +3,13 @@ /*! \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, @@ -30,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 @@ -109,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 7d9b78932c..be296918cc 100644 --- a/examples/corelib/threads/mandelbrot/CMakeLists.txt +++ b/examples/corelib/threads/mandelbrot/CMakeLists.txt @@ -4,16 +4,10 @@ cmake_minimum_required(VERSION 3.16) project(mandelbrot LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/mandelbrot") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) +qt_standard_project_setup() + qt_add_executable(mandelbrot main.cpp mandelbrotwidget.cpp mandelbrotwidget.h @@ -25,14 +19,21 @@ set_target_properties(mandelbrot PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(mandelbrot PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets +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 d0d4680978..8aafebf7d7 100644 --- a/examples/corelib/threads/mandelbrot/main.cpp +++ b/examples/corelib/threads/mandelbrot/main.cpp @@ -2,15 +2,14 @@ // 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[]) @@ -18,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); diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index e0f33a2b0b..bbe694831d 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -4,19 +4,20 @@ #include "mandelbrotwidget.h" #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] @@ -46,7 +47,8 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) if (pixmap.isNull()) { painter.setPen(Qt::white); - painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, tr("Rendering initial image, please wait...")); + painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, + tr("Rendering initial image, please wait...")); //! [2] //! [3] return; //! [3] //! [4] @@ -60,47 +62,47 @@ 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] - QFontMetrics metrics = painter.fontMetrics(); + const QFontMetrics metrics = painter.fontMetrics(); if (!info.isEmpty()){ - int infoWidth = metrics.horizontalAdvance(info); - int infoHeight = metrics.height(); + 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)); - infoHeight = (infoWidth/width()+1) * (infoHeight + 5); painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight); painter.setPen(Qt::white); painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info); } - int helpWidth = metrics.horizontalAdvance(help); - int helpHeight = metrics.height(); + 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)); - helpHeight = (helpWidth/width()+1) * (helpHeight + 5); - painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, helpHeight); + painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, + helpHeight); painter.setPen(Qt::white); painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignBottom|Qt::TextWordWrap, help); @@ -184,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); } } diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h index 23c3a2bf40..0d0fce56b3 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h @@ -4,16 +4,20 @@ #ifndef MANDELBROTWIDGET_H #define MANDELBROTWIDGET_H -#include <QGestureEvent> +#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); @@ -33,11 +37,9 @@ protected: 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); diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp index 9e6c884f91..77a14a6ac1 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.cpp +++ b/examples/corelib/threads/mandelbrot/renderthread.cpp @@ -4,7 +4,6 @@ #include "renderthread.h" #include <QImage> - #include <QElapsedTimer> #include <QTextStream> @@ -70,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 6e509eee94..ecd8ae084d 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.h +++ b/examples/corelib/threads/mandelbrot/renderthread.h @@ -49,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 2a5638eeec..725c268615 100644 --- a/examples/corelib/threads/queuedcustomtype/CMakeLists.txt +++ b/examples/corelib/threads/queuedcustomtype/CMakeLists.txt @@ -4,16 +4,10 @@ cmake_minimum_required(VERSION 3.16) project(queuedcustomtype LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/queuedcustomtype") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) +qt_standard_project_setup() + qt_add_executable(queuedcustomtype block.cpp block.h main.cpp @@ -26,14 +20,21 @@ set_target_properties(queuedcustomtype PROPERTIES 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/main.cpp b/examples/corelib/threads/queuedcustomtype/main.cpp index 0cf019990a..0a14cb48e1 100644 --- a/examples/corelib/threads/queuedcustomtype/main.cpp +++ b/examples/corelib/threads/queuedcustomtype/main.cpp @@ -1,12 +1,17 @@ // 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); @@ -43,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 d7b2172608..34a439f3fe 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.cpp +++ b/examples/corelib/threads/queuedcustomtype/renderthread.cpp @@ -1,22 +1,19 @@ // 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(); } @@ -27,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); @@ -55,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 afbc70ec75..c3152e4081 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.h +++ b/examples/corelib/threads/queuedcustomtype/renderthread.h @@ -5,9 +5,9 @@ #define RENDERTHREAD_H #include <QImage> -#include <QMutex> #include <QThread> -#include "block.h" + +class Block; //! [RenderThread class definition] class RenderThread : public QThread @@ -23,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 1adfe7ed38..db12133206 100644 --- a/examples/corelib/threads/queuedcustomtype/window.cpp +++ b/examples/corelib/threads/queuedcustomtype/window.cpp @@ -1,8 +1,17 @@ // 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) @@ -20,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] @@ -51,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; @@ -67,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 b1ab4f6c24..a4f995696e 100644 --- a/examples/corelib/threads/queuedcustomtype/window.h +++ b/examples/corelib/threads/queuedcustomtype/window.h @@ -4,13 +4,14 @@ #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 2c17d2d2e0..a096616e5a 100644 --- a/examples/corelib/threads/semaphores/CMakeLists.txt +++ b/examples/corelib/threads/semaphores/CMakeLists.txt @@ -4,26 +4,31 @@ cmake_minimum_required(VERSION 3.16) project(semaphores LANGUAGES CXX) -set(CMAKE_AUTOMOC 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) +qt_standard_project_setup() + qt_add_executable(semaphores semaphores.cpp ) -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 2d9d169138..103f331b0c 100644 --- a/examples/corelib/threads/semaphores/semaphores.cpp +++ b/examples/corelib/threads/semaphores/semaphores.cpp @@ -7,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); @@ -36,7 +36,6 @@ public: class Consumer : public QThread //! [3] //! [4] { - Q_OBJECT public: void run() override { @@ -64,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 4d6ce268b7..5fc44a079b 100644 --- a/examples/corelib/threads/waitconditions/CMakeLists.txt +++ b/examples/corelib/threads/waitconditions/CMakeLists.txt @@ -4,26 +4,31 @@ cmake_minimum_required(VERSION 3.16) project(waitconditions LANGUAGES CXX) -set(CMAKE_AUTOMOC 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) +qt_standard_project_setup() + qt_add_executable(waitconditions waitconditions.cpp ) -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 72b9ed39a5..bdc24acd8c 100644 --- a/examples/corelib/threads/waitconditions/waitconditions.cpp +++ b/examples/corelib/threads/waitconditions/waitconditions.cpp @@ -1,21 +1,28 @@ // 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 <QtCore> +#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] @@ -23,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(); + } } } }; @@ -50,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] @@ -95,4 +106,3 @@ int main(int argc, char *argv[]) } //! [6] -#include "waitconditions.moc" |