Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
customtypeexample.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QCoreApplication>
4#include <QDebug>
5#include <QVariant>
6
7//message.h
8
11{
12public:
13 Message() = default;
14 ~Message() = default;
15 Message(const Message &) = default;
16 Message &operator=(const Message &) = default;
17
19
20 QStringView body() const;
21 QStringList headers() const;
22
23private:
24 QString m_body;
25 QStringList m_headers;
26};
28
32
36
37// message.cpp
38
41{
42 const QList<QStringView> pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts);
43 if (pieces.isEmpty())
44 dbg.nospace() << "Message()";
45 else if (pieces.size() == 1)
46 dbg.nospace() << "Message(" << pieces.first() << ")";
47 else
48 dbg.nospace() << "Message(" << pieces.first() << " ...)";
49 return dbg;
50}
52
55{
56 return m_body;
57}
58
60{
61 return m_headers;
62}
64
65//main.cpp
66
67int main(int argc, char *argv[])
68{
69 QCoreApplication app(argc, argv);
70 QStringList headers;
71 headers << "Subject: Hello World"
72 << "From: address@example.com";
73 QString body = "This is a test.\r\n";
75 Message message(body, headers);
76 qDebug() << "Original:" << message;
79 QVariant stored;
80 stored.setValue(message);
82 qDebug() << "Stored:" << stored;
84 Message retrieved = qvariant_cast<Message>(stored);
85 qDebug() << "Retrieved:" << retrieved;
86 retrieved = qvariant_cast<Message>(stored);
87 qDebug() << "Retrieved:" << retrieved;
89 return 0;
90}
[custom type definition]
~Message()=default
Message(const QString &body, const QStringList &headers)
Message & operator=(const Message &)=default
Message(const Message &)=default
Message()=default
QStringList headers() const
QStringView body() const
[custom type streaming operator]
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
void setValue(T &&avalue)
Definition qvariant.h:493
QDebug operator<<(QDebug dbg, const Message &message)
[custom type meta-type declaration]
int main()
[0]
@ SkipEmptyParts
Definition qnamespace.h:128
#define qDebug
[1]
Definition qlogging.h:164
#define Q_DECLARE_METATYPE(TYPE)
Definition qmetatype.h:1525
GLuint GLsizei const GLchar * message
QApplication app(argc, argv)
[0]