diff options
Diffstat (limited to 'examples/corelib/threads/queuedcustomtype')
6 files changed, 61 insertions, 58 deletions
diff --git a/examples/corelib/threads/queuedcustomtype/CMakeLists.txt b/examples/corelib/threads/queuedcustomtype/CMakeLists.txt index 2086b83492..725c268615 100644 --- a/examples/corelib/threads/queuedcustomtype/CMakeLists.txt +++ b/examples/corelib/threads/queuedcustomtype/CMakeLists.txt @@ -1,16 +1,13 @@ +# 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_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 @@ -23,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 |