diff options
Diffstat (limited to 'tests/manual/corelib/tools/customtype')
-rw-r--r-- | tests/manual/corelib/tools/customtype/CMakeLists.txt | 15 | ||||
-rw-r--r-- | tests/manual/corelib/tools/customtype/customtype.pro | 7 | ||||
-rw-r--r-- | tests/manual/corelib/tools/customtype/main.cpp | 37 | ||||
-rw-r--r-- | tests/manual/corelib/tools/customtype/message.cpp | 38 | ||||
-rw-r--r-- | tests/manual/corelib/tools/customtype/message.h | 38 |
5 files changed, 135 insertions, 0 deletions
diff --git a/tests/manual/corelib/tools/customtype/CMakeLists.txt b/tests/manual/corelib/tools/customtype/CMakeLists.txt new file mode 100644 index 0000000000..4cb1c024b3 --- /dev/null +++ b/tests/manual/corelib/tools/customtype/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) + +qt_internal_add_manual_test(customtype + GUI + SOURCES + main.cpp + message.cpp message.h + LIBRARIES + Qt6::Core + Qt6::Gui + Qt6::Widgets +) diff --git a/tests/manual/corelib/tools/customtype/customtype.pro b/tests/manual/corelib/tools/customtype/customtype.pro new file mode 100644 index 0000000000..0a5a90f541 --- /dev/null +++ b/tests/manual/corelib/tools/customtype/customtype.pro @@ -0,0 +1,7 @@ +HEADERS = message.h +SOURCES = main.cpp \ + message.cpp +QT += widgets +INCLUDEPATH += . +TARGET = customtype + diff --git a/tests/manual/corelib/tools/customtype/main.cpp b/tests/manual/corelib/tools/customtype/main.cpp new file mode 100644 index 0000000000..2368db6ec0 --- /dev/null +++ b/tests/manual/corelib/tools/customtype/main.cpp @@ -0,0 +1,37 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QCoreApplication> +#include <QDebug> +#include <QVariant> +#include "message.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QStringList headers; + headers << "Subject: Hello World" + << "From: address@example.com"; + QString body = "This is a test.\r\n"; + +//! [printing a custom type] + Message message(body, headers); + qDebug() << "Original:" << message; +//! [printing a custom type] + +//! [storing a custom value] + QVariant stored; + stored.setValue(message); +//! [storing a custom value] + + qDebug() << "Stored:" << stored; + +//! [retrieving a custom value] + Message retrieved = qvariant_cast<Message>(stored); + qDebug() << "Retrieved:" << retrieved; + retrieved = qvariant_cast<Message>(stored); + qDebug() << "Retrieved:" << retrieved; +//! [retrieving a custom value] + + return 0; +} diff --git a/tests/manual/corelib/tools/customtype/message.cpp b/tests/manual/corelib/tools/customtype/message.cpp new file mode 100644 index 0000000000..221480d56f --- /dev/null +++ b/tests/manual/corelib/tools/customtype/message.cpp @@ -0,0 +1,38 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "message.h" + +#include <QDebug> + +Message::Message(const QString &body, const QStringList &headers) + : m_body(body), m_headers(headers) +{ +} + +//! [custom type streaming operator] +QDebug operator<<(QDebug dbg, const Message &message) +{ + QDebugStateSaver saver(dbg); + QList<QStringView> pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts); + if (pieces.isEmpty()) + dbg.nospace() << "Message()"; + else if (pieces.size() == 1) + dbg.nospace() << "Message(" << pieces.first() << ")"; + else + dbg.nospace() << "Message(" << pieces.first() << " ...)"; + return dbg; +} +//! [custom type streaming operator] + +//! [getter functions] +QStringView Message::body() const +{ + return m_body; +} + +QStringList Message::headers() const +{ + return m_headers; +} +//! [getter functions] diff --git a/tests/manual/corelib/tools/customtype/message.h b/tests/manual/corelib/tools/customtype/message.h new file mode 100644 index 0000000000..3b91938eb7 --- /dev/null +++ b/tests/manual/corelib/tools/customtype/message.h @@ -0,0 +1,38 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef MESSAGE_H +#define MESSAGE_H + +#include <QMetaType> +#include <QStringList> + +//! [custom type definition] +class Message +{ +public: + Message() = default; + ~Message() = default; + Message(const Message &) = default; + Message &operator=(const Message &) = default; + + Message(const QString &body, const QStringList &headers); + + QStringView body() const; + QStringList headers() const; + +private: + QString m_body; + QStringList m_headers; +}; +//! [custom type definition] + +//! [custom type meta-type declaration] +Q_DECLARE_METATYPE(Message); +//! [custom type meta-type declaration] + +//! [custom type streaming operator] +QDebug operator<<(QDebug dbg, const Message &message); +//! [custom type streaming operator] + +#endif |