diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/tools/customtype | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/tools/customtype')
-rw-r--r-- | examples/tools/customtype/customtype.pro | 3 | ||||
-rw-r--r-- | examples/tools/customtype/main.cpp | 74 | ||||
-rw-r--r-- | examples/tools/customtype/message.cpp | 90 | ||||
-rw-r--r-- | examples/tools/customtype/message.h | 76 |
4 files changed, 243 insertions, 0 deletions
diff --git a/examples/tools/customtype/customtype.pro b/examples/tools/customtype/customtype.pro new file mode 100644 index 0000000..3079964 --- /dev/null +++ b/examples/tools/customtype/customtype.pro @@ -0,0 +1,3 @@ +HEADERS = message.h +SOURCES = main.cpp \ + message.cpp diff --git a/examples/tools/customtype/main.cpp b/examples/tools/customtype/main.cpp new file mode 100644 index 0000000..e8f4265 --- /dev/null +++ b/examples/tools/customtype/main.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QCoreApplication> +#include <QVariant> +#include "message.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QStringList headers; + headers << "Subject: Hello World" + << "From: qt-info@nokia.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 = stored.value<Message>(); + qDebug() << "Retrieved:" << retrieved; + retrieved = qVariantValue<Message>(stored); + qDebug() << "Retrieved:" << retrieved; +//! [retrieving a custom value] + + return 0; +} diff --git a/examples/tools/customtype/message.cpp b/examples/tools/customtype/message.cpp new file mode 100644 index 0000000..b6fd536 --- /dev/null +++ b/examples/tools/customtype/message.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "message.h" + +//! [Message class implementation] +Message::Message() +{ +} + +Message::Message(const Message &other) +{ + m_body = other.m_body; + m_headers = other.m_headers; +} + +Message::~Message() +{ +} +//! [Message class implementation] + +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) +{ + QStringList pieces = message.body().split("\r\n", QString::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.maybeSpace(); +} +//! [custom type streaming operator] + +//! [getter functions] +QString Message::body() const +{ + return m_body; +} + +QStringList Message::headers() const +{ + return m_headers; +} +//! [getter functions] diff --git a/examples/tools/customtype/message.h b/examples/tools/customtype/message.h new file mode 100644 index 0000000..2620557 --- /dev/null +++ b/examples/tools/customtype/message.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MESSAGE_H +#define MESSAGE_H + +#include <QDebug> +#include <QMetaType> +#include <QStringList> + +//! [custom type definition] +class Message +{ +public: + Message(); + Message(const Message &other); + ~Message(); + + Message(const QString &body, const QStringList &headers); + + QString 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 |