From 3a5040ebacb177e883e2f79660194c1034fa79c2 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 23 Apr 2010 16:42:16 +1000 Subject: Add tutorial for writing QML extensions --- doc/src/declarative/declarativeui.qdoc | 11 +- doc/src/declarative/extending-tutorial.qdoc | 419 +++++++++++++++++++++ .../tutorials/extending/chapter1-basics/app.qml | 17 + .../extending/chapter1-basics/chapter1-basics.pro | 5 + .../tutorials/extending/chapter1-basics/main.cpp | 58 +++ .../extending/chapter1-basics/musician.cpp | 67 ++++ .../tutorials/extending/chapter1-basics/musician.h | 69 ++++ .../tutorials/extending/chapter2-methods/app.qml | 19 + .../chapter2-methods/chapter2-methods.pro | 5 + .../tutorials/extending/chapter2-methods/main.cpp | 58 +++ .../extending/chapter2-methods/musician.cpp | 75 ++++ .../extending/chapter2-methods/musician.h | 82 ++++ .../tutorials/extending/chapter3-bindings/app.qml | 31 ++ .../chapter3-bindings/chapter3-bindings.pro | 5 + .../tutorials/extending/chapter3-bindings/main.cpp | 58 +++ .../extending/chapter3-bindings/musician.cpp | 77 ++++ .../extending/chapter3-bindings/musician.h | 83 ++++ .../extending/chapter4-customPropertyTypes/app.qml | 13 + .../chapter4-customPropertyTypes.pro | 7 + .../chapter4-customPropertyTypes/instrument.cpp | 57 +++ .../chapter4-customPropertyTypes/instrument.h | 64 ++++ .../chapter4-customPropertyTypes/main.cpp | 67 ++++ .../chapter4-customPropertyTypes/musician.cpp | 68 ++++ .../chapter4-customPropertyTypes/musician.h | 79 ++++ .../tutorials/extending/chapter5-plugins/app.qml | 13 + .../chapter5-plugins/chapter5-plugins.pro | 15 + .../extending/chapter5-plugins/instrument.cpp | 57 +++ .../extending/chapter5-plugins/instrument.h | 62 +++ .../extending/chapter5-plugins/musician.cpp | 68 ++++ .../extending/chapter5-plugins/musician.h | 69 ++++ .../extending/chapter5-plugins/musicplugin.cpp | 55 +++ .../extending/chapter5-plugins/musicplugin.h | 56 +++ .../tutorials/extending/chapter5-plugins/qmldir | 1 + 33 files changed, 1887 insertions(+), 3 deletions(-) create mode 100644 doc/src/declarative/extending-tutorial.qdoc create mode 100644 examples/declarative/tutorials/extending/chapter1-basics/app.qml create mode 100644 examples/declarative/tutorials/extending/chapter1-basics/chapter1-basics.pro create mode 100644 examples/declarative/tutorials/extending/chapter1-basics/main.cpp create mode 100644 examples/declarative/tutorials/extending/chapter1-basics/musician.cpp create mode 100644 examples/declarative/tutorials/extending/chapter1-basics/musician.h create mode 100644 examples/declarative/tutorials/extending/chapter2-methods/app.qml create mode 100644 examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro create mode 100644 examples/declarative/tutorials/extending/chapter2-methods/main.cpp create mode 100644 examples/declarative/tutorials/extending/chapter2-methods/musician.cpp create mode 100644 examples/declarative/tutorials/extending/chapter2-methods/musician.h create mode 100644 examples/declarative/tutorials/extending/chapter3-bindings/app.qml create mode 100644 examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro create mode 100644 examples/declarative/tutorials/extending/chapter3-bindings/main.cpp create mode 100644 examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp create mode 100644 examples/declarative/tutorials/extending/chapter3-bindings/musician.h create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/app.qml create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/chapter4-customPropertyTypes.pro create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.cpp create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.h create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.cpp create mode 100644 examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/app.qml create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/chapter5-plugins.pro create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/instrument.cpp create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/instrument.h create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/musician.cpp create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/musician.h create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.cpp create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.h create mode 100644 examples/declarative/tutorials/extending/chapter5-plugins/qmldir diff --git a/doc/src/declarative/declarativeui.qdoc b/doc/src/declarative/declarativeui.qdoc index 55945e6..8f36582 100644 --- a/doc/src/declarative/declarativeui.qdoc +++ b/doc/src/declarative/declarativeui.qdoc @@ -76,7 +76,6 @@ completely new applications. QML is fully \l {Extending QML in C++}{extensible \o \l {QML Tutorial}{Tutorial: 'Hello World'} \o \l {QML Advanced Tutorial}{Tutorial: 'Same Game'} \o \l {QML Examples and Demos} -\o \l {Using QML in C++ Applications} \o \l {QML for Qt programmers} \endlist @@ -98,12 +97,18 @@ completely new applications. QML is fully \l {Extending QML in C++}{extensible \o \l {qmlruntime.html}{The Qt Declarative Runtime} \endlist +\section1 Using QML with C++: +\list +\o \l {Tutorial: Writing QML extensions with C++} +\o \l {Extending QML in C++} +\o \l {Using QML in C++ Applications} +\o \l {Integrating QML with existing Qt UI code} +\endlist + \section1 Reference: \list \o \l {QML Elements} \o \l {QML Global Object} -\o \l {Extending QML in C++} -\o \l {Integrating QML with existing Qt UI code} \o \l {QML Internationalization} \o \l {QML Security} \o \l {QtDeclarative Module} diff --git a/doc/src/declarative/extending-tutorial.qdoc b/doc/src/declarative/extending-tutorial.qdoc new file mode 100644 index 0000000..a139616 --- /dev/null +++ b/doc/src/declarative/extending-tutorial.qdoc @@ -0,0 +1,419 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + +\page qml-extending-tutorial-index.html +\title Tutorial: Writing QML extensions with C++ + +The QtDeclarative module provides a set of APIs for extending QML through +C++ extensions. You can write extensions to add your own QML types, extend existing +Qt types, or call C/C++ functions that are not accessible from ordinary QML code. + +This tutorial shows how to write a QML extension using C++ that includes +core QML features, including properties, signals and bindings. It also shows how +extensions can be deployed through plugins. + +You can find the source code for this tutorial in \c Qt's +examples/declarative/tutorials/extending directory. + +Tutorial chapters: + +\list 1 +\o \l{declarative/tutorials/extending/chapter1-basics}{Creating a New Type} +\o \l{declarative/tutorials/extending/chapter2-methods}{Connecting to C++ Methods and Signals} +\o \l{declarative/tutorials/extending/chapter3-bindings}{Adding Property Bindings} +\o \l{declarative/tutorials/extending/chapter4-customPropertyTypes}{Using Custom Property Types} +\o \l{declarative/tutorials/extending/chapter5-plugins}{Deploying Your Extension} +\o \l{qml-extending-tutorial6.html}{In Summary} +\endlist + +*/ + +/*! +\title Chapter 1: Creating a New Type + +\example declarative/tutorials/extending/chapter1-basics + +Let's create a new QML type called "Musician" that has two properties: a name +and an instrument. We will make it available in a \l {Modules}{module} called "Music", with +a module version of 1.0. +We want this \c Musician type to be usable from QML like this: + +\code + import Music 1.0 + + Musician { + name: "Reddy the Rocker" + instrument: "Guitar" + } +\endcode + +To do this, we need a C++ class that encapsulates this \c Musician type and its two +properties. Since QML relies heavily on Qt's \l{Meta-Object System}{meta object system}, +this new class must: + +\list +\o inherit from QObject +\o declare its properties using the Q_PROPERTY() macro +\endlist + +Here is our \c Musician class, defined in \c musician.h: + +\snippet declarative/tutorials/extending/chapter1-basics/musician.h 0 + +It defines the two properties, \c name and \c instrument, with the Q_PROPERTY() macro. +The class implementation in \c musician.cpp simply sets and returns the \c m_name and +\c m_instrument values as appropriate. + +Our QML file, \c app.qml, creates a \c Musician item and display the musician's details +using a standard QML \l Text item: + +\quotefile declarative/tutorials/extending/chapter1-basics/app.qml + +We'll also create a C++ application that uses a QDeclarativeView to run and +display \c app.qml. The application must register the \c Musician type +using the qmlRegisterType() function, to allow it to be used from QML. If +you don't register the type, \c app.qml won't be able to create a \c Musician. + +Here is the application \c main.cpp: + +\snippet declarative/tutorials/extending/chapter1-basics/main.cpp 0 + +This call to qmlRegisterType() registers the \c Musician type as a type called "Musician", in a module named "Music", +with a module version of 1.0. + +Lastly, we write a \c .pro project file that includes the files and the \c declarative library: + +\quotefile declarative/tutorials/extending/chapter1-basics/chapter1-basics.pro + +Now we can build and run the application. Try it yourself with the code in Qt's \c examples/tutorials/extending/chapter1-basics directory. + +\example declarative/tutorials/extending/chapter1-basics + +At the moment, the \c app.qml is run from within a C++ application. +This may seem odd if you're used to running QML files with the standard \c qml tool. +Later on, we'll show how to create a plugin so that you can run \c app.qml using the +\c qml tool instead. + +*/ + + +/*! +\title Chapter 2: Connecting to C++ Methods and Signals + +\example declarative/tutorials/extending/chapter2-methods + +Suppose we want \c Musician to have a "perform" method that prints a message +to the console and then emits a "performanceEnded" signal. +Other elements would be able to call \c perform() and receive +\c performanceEnded() signals like this: + +\quotefile declarative/tutorials/extending/chapter2-methods/app.qml + +To do this, we add a \c perform() method and a \c performanceEnded() signal +to our C++ class: + +\snippet declarative/tutorials/extending/chapter2-methods/musician.h 0 +\dots +\snippet declarative/tutorials/extending/chapter2-methods/musician.h 1 +\dots +\snippet declarative/tutorials/extending/chapter2-methods/musician.h 2 +\dots +\snippet declarative/tutorials/extending/chapter2-methods/musician.h 3 + +The use of Q_INVOKABLE makes the \c perform() method available to the +Qt Meta-Object system, and in turn, to QML. Note that it could have +been declared as as a Qt slot instead of using Q_INVOKABLE, as +slots are also callable from QML. Both of these approaches are valid. + +The \c perform() method simply prints a message to the console and +then emits \c performanceEnded(): + +\snippet declarative/tutorials/extending/chapter2-methods/musician.cpp 0 + +Now when we run the application and click the window, the application outputs: + +\code + "Reddy the Rocker" is playing the "Guitar" + The performance has now ended +\endcode + +Try out the example yourself with the updated code in Qt's \c examples/tutorials/extending/chapter2-methods directory. + +*/ + +/*! +\title Chapter 3: Adding Property Bindings + +\example declarative/tutorials/extending/chapter3-bindings + +Property bindings is a powerful feature of QML that allows values of different +elements to be synchronized automatically. It uses signals to notify and update +other elements' values when property values change. + +Let's enable property bindings for the \c instrument property. That means +if we have code like this: + +\quotefile declarative/tutorials/extending/chapter3-bindings/app.qml + +The "instrument: reddy.instrument" statement binds the \c instrument value of +\c craig to the \c instrument of \c reddy. +Whenever \c reddy's \c instrument value changes, \c craig's \c instrument value +updates to the same value. When the window is clicked, the application outputs: + +\code + "Reddy the Rocker" is playing the "Guitar" + "Craig the Copycat" is playing the "Guitar" + "Reddy the Rocker" is playing the "Drums" + "Craig the Copycat" is playing the "Drums" +\endcode + +It's easy to enable property binding for the \c instrument property. +We add a \l{Qt's Property System}{NOTIFY} feature to its Q_PROPERTY() declaration to indicate that a "instrumentChanged" signal +is emitted whenever the value changes. + +\snippet declarative/tutorials/extending/chapter3-bindings/musician.h 0 +\dots +\snippet declarative/tutorials/extending/chapter3-bindings/musician.h 1 +\dots +\snippet declarative/tutorials/extending/chapter3-bindings/musician.h 2 +\dots +\snippet declarative/tutorials/extending/chapter3-bindings/musician.h 3 + +Then, we emit this signal in \c setInstrument(): + +\snippet declarative/tutorials/extending/chapter3-bindings/musician.cpp 0 + +It's important for \c setInstrument() to check that the instrument value has actually changed +before emitting \c instrumentChanged(). This ensures the signal is not emitted unnecessarily and +also prevents loops when other elements respond to the value change. + +*/ + +/*! +\title Chapter 4: Using Custom Property Types + +\example declarative/tutorials/extending/chapter4-customPropertyTypes + +The \c Musician type currently has two properties that are both strings. +It could have all sorts of other properties. For example, we could add an +integer-type property to store the age of each musician: + +\code + class Musician : public QObject + { + ... + Q_PROPERTY(int age READ age WRITE setAge) + public: + ... + int age() const; + void setAge(int age); + ... + }; +\endcode + +We can also use various other property types. QML has built-in support for the following +types: + +\list +\o bool +\o unsigned int, int +\o float, double, qreal +\o QString +\o QUrl +\o QColor +\o QDate, QTime, QDateTime +\o QPoint, QPointF +\o QSize, QSizeF +\o QRect, QRectF +\o QVariant +\endlist + +If we want to create a property whose type is not supported by QML by default, +we need to register the type with QML. + +For example, let's change the type of the \c instrument property from a string to a +new type called "Instrument". Instead of assigning a string value to \c instrument, +we assign an \c Instrument value: + +\quotefile declarative/tutorials/extending/chapter4-customPropertyTypes/app.qml + +Like \c Musician, this new \c Instrument type has to inherit from QObject and declare +its properties with Q_PROPERTY(): + +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.h 0 + +To use it from \c Musician, we modify the \c instrument property declaration +and associated method signatures: + +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h 0 +\dots +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h 1 +\dots +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h 2 +\dots +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h 3 + +Like the \c Musician type, the \c Instrument type has to be registered +using qmlRegisterType() to be used from QML. As with \c Musician, we'll add the +type to the "Music" module, version 1.0: + +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp 0 +\dots +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp 1 +\dots +\snippet declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp 2 + +Try it out with the code in Qt's \c examples/tutorials/extending/chapter4-customPropertyTypes directory. + +*/ + +/*! +\title Chapter 5: Deploying Your Extension + +\example declarative/tutorials/extending/chapter5-plugins + +Currently the \c Musician and \c Instrument types are used by \c app.qml, +which is displayed using a QDeclarativeView in a C++ application. An alternative +way to use our QML extension is to create a plugin library to make it available +to the QML engine. This means we could load \c app.qml using the standard \c qml tool +instead of writing a \c main.cpp file and loading our own C++ application. + +To create a plugin library, we need: + +\list +\o A plugin class that registers our QML types +\o A project file that describes the plugin +\o A "qmldir" file that tells the QML engine to load the plugin +\endlist + +First, we create a plugin class named \c MusicPlugin. It subclasses QDeclarativeExtensionPlugin +and registers our QML types in the inherited \l{QDeclarativeExtensionPlugin::}{registerTypes()} method. It also calls +Q_EXPORT_PLUGIN2 for Qt's \l{How to Create Qt Plugins}{plugin system}. + +Here is the \c MusicPlugin definition in \c musicplugin.h: + +\snippet declarative/tutorials/extending/chapter5-plugins/musicplugin.h 0 + +And its implementation in \c musicplugin.cpp: + +\snippet declarative/tutorials/extending/chapter5-plugins/musicplugin.cpp 0 + +Then, we write a \c .pro project file that defines the project as a plugin library +and specifies with DESTDIR that library files should be built into a "lib" subdirectory: + +\quotefile declarative/tutorials/extending/chapter5-plugins/chapter5-plugins.pro + +Finally, we add a \c qmldir file that is automatically parsed by the QML engine. +Here, we specify that a plugin named "chapter5-plugin" (the name +of the example project) can be found in the "lib" subdirectory: + +\quotefile declarative/tutorials/extending/chapter5-plugins/qmldir + +Now we have a plugin, and instead of having a main.cpp and an executable, we can build +the project and then run the QML file directly using the \c qml tool: + +\code + qml app.qml +\endcode + +Notice the "import Music 1.0" statement has disappeared from \c app.qml. This is +because the \c qmldir file is in the same directory as \c app.qml: this is equivalent to +having Musician.qml and Instrument.qml files inside the project directory, which could both +be used by \c app.qml without import statements. +*/ + +/*! +\page qml-extending-tutorial6.html +\title Chapter 6: In Summary + +In this tutorial, we've shown the basic steps for creating a QML extension: + +\list +\o Define new QML types by subclassing QObject and registering them with qmlRegisterType() +\o Add callable methods using Q_INVOKABLE or Qt slots, and connect to Qt signals with an \c onSignal syntax +\o Add property bindings by defining \l{Qt's Property System}{NOTIFY} signals +\o Define custom property types if the built-in types are not sufficient +\o Create a plugin library by defining a Qt plugin and writing a \c qmldir file +\endlist + + +The \l {Extending QML in C++} reference documentation shows other useful features that can be added to +QML extensions. For example, we could use \l{Object and List Property Types}{list properties} to allow multiple instruments for a \c Musician: + +\code + Musician { + instruments: [ + Instrument { type: "Guitar" } + Instrument { type: "Drums" } + Instrument { type: "Keyboard" } + ] + } +\endcode + +Or use \l{Default Property}{default properties} and avoid an +\c instruments property altogether: + +\code + Musician { + Instrument { type: "Guitar" } + Instrument { type: "Drums" } + Instrument { type: "Keyboard" } + } +\endcode + +Or even change the \c instrument of a \c Musician from time to time using \l{Property Value Sources}{property value sources}: + +\code + Musician { + InstrumentRandomizer on instrument {} + } +\endcode + + +See the \l{Extending QML in C++}{reference documentation} for more information. + +Additionally, \l {Integrating QML with existing Qt UI code} shows how to create +and integrate with QML extensions that have drawing and graphical capabilities (through QGraphicsWidget). + +*/ + diff --git a/examples/declarative/tutorials/extending/chapter1-basics/app.qml b/examples/declarative/tutorials/extending/chapter1-basics/app.qml new file mode 100644 index 0000000..15dcd2d --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter1-basics/app.qml @@ -0,0 +1,17 @@ +import Music 1.0 +import Qt 4.7 + +Rectangle { + width: 300; height: 200 + + Musician { + id: aMusician + name: "Reddy the Rocker" + instrument: "Guitar" + } + + Text { + anchors.fill: parent + text: aMusician.name + " plays the " + aMusician.instrument + } +} diff --git a/examples/declarative/tutorials/extending/chapter1-basics/chapter1-basics.pro b/examples/declarative/tutorials/extending/chapter1-basics/chapter1-basics.pro new file mode 100644 index 0000000..bd05ebe --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter1-basics/chapter1-basics.pro @@ -0,0 +1,5 @@ +QT += declarative + +HEADERS += musician.h +SOURCES += musician.cpp \ + main.cpp diff --git a/examples/declarative/tutorials/extending/chapter1-basics/main.cpp b/examples/declarative/tutorials/extending/chapter1-basics/main.cpp new file mode 100644 index 0000000..0e71bb0 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter1-basics/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +#include "musician.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + qmlRegisterType("Music", 1, 0, "Musician"); + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("app.qml")); + view.show(); + return app.exec(); +} +//![0] diff --git a/examples/declarative/tutorials/extending/chapter1-basics/musician.cpp b/examples/declarative/tutorials/extending/chapter1-basics/musician.cpp new file mode 100644 index 0000000..b545f84 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter1-basics/musician.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" + +Musician::Musician(QObject *parent) + : QObject(parent) +{ +} + +QString Musician::name() const +{ + return m_name; +} + +void Musician::setName(const QString &name) +{ + m_name = name; +} + +QString Musician::instrument() const +{ + return m_instrument; +} + +void Musician::setInstrument(const QString &instrument) +{ + m_instrument = instrument; +} + diff --git a/examples/declarative/tutorials/extending/chapter1-basics/musician.h b/examples/declarative/tutorials/extending/chapter1-basics/musician.h new file mode 100644 index 0000000..70ade82 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter1-basics/musician.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICIAN_H +#define MUSICIAN_H + +//![0] +#include + +class Musician : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString instrument READ instrument WRITE setInstrument) + +public: + Musician(QObject *parent = 0); + + QString name() const; + void setName(const QString &name); + + QString instrument() const; + void setInstrument(const QString &instrument); + +private: + QString m_name; + QString m_instrument; +}; +//![0] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter2-methods/app.qml b/examples/declarative/tutorials/extending/chapter2-methods/app.qml new file mode 100644 index 0000000..35e083e --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter2-methods/app.qml @@ -0,0 +1,19 @@ +import Music 1.0 +import Qt 4.7 + +Rectangle { + width: 200; height: 200 + + Musician { + id: aMusician + name: "Reddy the Rocker" + instrument: "Guitar" + + onPerformanceEnded: console.log("The performance has now ended") + } + + MouseArea { + anchors.fill: parent + onClicked: aMusician.perform() + } +} diff --git a/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro b/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro new file mode 100644 index 0000000..bd05ebe --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro @@ -0,0 +1,5 @@ +QT += declarative + +HEADERS += musician.h +SOURCES += musician.cpp \ + main.cpp diff --git a/examples/declarative/tutorials/extending/chapter2-methods/main.cpp b/examples/declarative/tutorials/extending/chapter2-methods/main.cpp new file mode 100644 index 0000000..0e71bb0 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter2-methods/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +#include "musician.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + qmlRegisterType("Music", 1, 0, "Musician"); + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("app.qml")); + view.show(); + return app.exec(); +} +//![0] diff --git a/examples/declarative/tutorials/extending/chapter2-methods/musician.cpp b/examples/declarative/tutorials/extending/chapter2-methods/musician.cpp new file mode 100644 index 0000000..3df19e3 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter2-methods/musician.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" +#include + +Musician::Musician(QObject *parent) + : QObject(parent) +{ +} + +QString Musician::name() const +{ + return m_name; +} + +void Musician::setName(const QString &name) +{ + m_name = name; +} + +QString Musician::instrument() const +{ + return m_instrument; +} + +void Musician::setInstrument(const QString &instrument) +{ + m_instrument = instrument; +} + +//![0] +void Musician::perform() +{ + qWarning() << m_name << "is playing the" << m_instrument; + emit performanceEnded(); +} +//![0] diff --git a/examples/declarative/tutorials/extending/chapter2-methods/musician.h b/examples/declarative/tutorials/extending/chapter2-methods/musician.h new file mode 100644 index 0000000..dc48759 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter2-methods/musician.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICIAN_H +#define MUSICIAN_H + +#include + +//![0] +class Musician : public QObject +{ +//![0] + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString instrument READ instrument WRITE setInstrument) + +//![1] +public: +//![1] + + Musician(QObject *parent = 0); + + QString name() const; + void setName(const QString &name); + + QString instrument() const; + void setInstrument(const QString &instrument); + +//![2] + Q_INVOKABLE void perform(); + +signals: + void performanceEnded(); +//![2] + +private: + QString m_name; + QString m_instrument; + +//![3] +}; +//![3] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/app.qml b/examples/declarative/tutorials/extending/chapter3-bindings/app.qml new file mode 100644 index 0000000..0460b0b --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter3-bindings/app.qml @@ -0,0 +1,31 @@ +import Music 1.0 +import Qt 4.7 + +Rectangle { + width: 200; height: 200 + + Musician { + id: reddy + name: "Reddy the Rocker" + instrument: "Guitar" + } + + Musician { + id: craig + name: "Craig the Copycat" + instrument: reddy.instrument + } + + MouseArea { + anchors.fill: parent + onClicked: { + reddy.perform() + craig.perform() + + reddy.instrument = "Drums" + + reddy.perform() + craig.perform() + } + } +} diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro b/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro new file mode 100644 index 0000000..bd05ebe --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro @@ -0,0 +1,5 @@ +QT += declarative + +HEADERS += musician.h +SOURCES += musician.cpp \ + main.cpp diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp b/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp new file mode 100644 index 0000000..0e71bb0 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +#include "musician.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + qmlRegisterType("Music", 1, 0, "Musician"); + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("app.qml")); + view.show(); + return app.exec(); +} +//![0] diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp b/examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp new file mode 100644 index 0000000..eb615f6 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" +#include + +Musician::Musician(QObject *parent) + : QObject(parent) +{ +} + +QString Musician::name() const +{ + return m_name; +} + +void Musician::setName(const QString &name) +{ + m_name = name; +} + +QString Musician::instrument() const +{ + return m_instrument; +} + +//![0] +void Musician::setInstrument(const QString &instrument) +{ + if (instrument != m_instrument) { + m_instrument = instrument; + emit instrumentChanged(); + } +} +//![0] + +void Musician::perform() +{ + qWarning() << m_name << "is playing the" << m_instrument; +} diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/musician.h b/examples/declarative/tutorials/extending/chapter3-bindings/musician.h new file mode 100644 index 0000000..39887be --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter3-bindings/musician.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICIAN_H +#define MUSICIAN_H + +#include + +//![0] +class Musician : public QObject +{ +//![0] + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString instrument READ instrument WRITE setInstrument) + +//![1] + Q_PROPERTY(QString instrument READ instrument WRITE setInstrument NOTIFY instrumentChanged) +public: +//![1] + + Musician(QObject *parent = 0); + + QString name() const; + void setName(const QString &name); + + QString instrument() const; + void setInstrument(const QString &instrument); + + Q_INVOKABLE void perform(); + +//![2] +signals: + void instrumentChanged(); +//![2] + +private: + QString m_name; + QString m_instrument; + +//![3] +}; +//![3] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/app.qml b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/app.qml new file mode 100644 index 0000000..ae9272e --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/app.qml @@ -0,0 +1,13 @@ +import Music 1.0 +import Qt 4.7 + +Item { + + Musician { + id: reddy + name: "Reddy the Rocker" + instrument: Instrument { type: "Guitar" } + } + + Component.onCompleted: console.log("Reddy plays the " + reddy.instrument.type) +} diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/chapter4-customPropertyTypes.pro b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/chapter4-customPropertyTypes.pro new file mode 100644 index 0000000..aea07a0 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/chapter4-customPropertyTypes.pro @@ -0,0 +1,7 @@ +QT += declarative + +HEADERS += musician.h \ + instrument.h +SOURCES += musician.cpp \ + instrument.cpp \ + main.cpp diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.cpp b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.cpp new file mode 100644 index 0000000..13cd2fe --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "instrument.h" + +Instrument::Instrument(QObject *parent) + : QObject(parent) +{ +} + +QString Instrument::type() const +{ + return m_type; +} + +void Instrument::setType(const QString &type) +{ + m_type = type; +} + diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.h b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.h new file mode 100644 index 0000000..e2f09be --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/instrument.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef INSTRUMENT_H +#define INSTRUMENT_H + +#include + +//![0] +class Instrument : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString type READ type WRITE setType) + +public: + Instrument(QObject *parent = 0); + + QString type() const; + void setType(const QString &type); + +private: + QString m_type; +}; +//![0] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp new file mode 100644 index 0000000..cbbbc0f --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/main.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" +#include "instrument.h" + +#include +#include +#include + +//![0] +int main(int argc, char *argv[]) +{ +//![0] + QApplication app(argc, argv); + + qmlRegisterType("Music", 1, 0, "Musician"); + +//![1] + qmlRegisterType("Music", 1, 0, "Instrument"); +//![1] + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("app.qml")); + view.show(); + return app.exec(); + +//![2] +} +//![2] diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.cpp b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.cpp new file mode 100644 index 0000000..6b66d5d --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" +#include "instrument.h" + +Musician::Musician(QObject *parent) + : QObject(parent) +{ +} + +QString Musician::name() const +{ + return m_name; +} + +void Musician::setName(const QString &name) +{ + m_name = name; +} + +Instrument *Musician::instrument() const +{ + return m_instrument; +} + +void Musician::setInstrument(Instrument *instrument) +{ + m_instrument = instrument; +} + diff --git a/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h new file mode 100644 index 0000000..d0aa118 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter4-customPropertyTypes/musician.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICIAN_H +#define MUSICIAN_H + +#include + +class Instrument; + +//![0] +class Musician : public QObject +{ + Q_OBJECT + Q_PROPERTY(Instrument* instrument READ instrument WRITE setInstrument) +//![0] + Q_PROPERTY(QString name READ name WRITE setName) + +//![1] +public: +//![1] + + Musician(QObject *parent = 0); + + QString name() const; + void setName(const QString &name); + +//![2] + Instrument *instrument() const; + void setInstrument(Instrument *instrument); +//![2] + +private: + QString m_name; + Instrument *m_instrument; + +//![3] +}; +//![3] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/app.qml b/examples/declarative/tutorials/extending/chapter5-plugins/app.qml new file mode 100644 index 0000000..51c1232 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/app.qml @@ -0,0 +1,13 @@ +import Qt 4.7 + +Item { + + Musician { + id: reddy + name: "Reddy the Rocker" + instrument: Instrument { type: "Guitar" } + } + + Component.onCompleted: console.log("Reddy plays the " + reddy.instrument.type) +} + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/chapter5-plugins.pro b/examples/declarative/tutorials/extending/chapter5-plugins/chapter5-plugins.pro new file mode 100644 index 0000000..7ec68e9 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/chapter5-plugins.pro @@ -0,0 +1,15 @@ +TEMPLATE = lib +CONFIG += qt plugin +QT += declarative + +HEADERS += musician.h \ + instrument.h \ + musicplugin.h + +SOURCES += musician.cpp \ + instrument.cpp \ + musicplugin.cpp + +DESTDIR = lib +OBJECTS_DIR = tmp +MOC_DIR = tmp diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/instrument.cpp b/examples/declarative/tutorials/extending/chapter5-plugins/instrument.cpp new file mode 100644 index 0000000..13cd2fe --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/instrument.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "instrument.h" + +Instrument::Instrument(QObject *parent) + : QObject(parent) +{ +} + +QString Instrument::type() const +{ + return m_type; +} + +void Instrument::setType(const QString &type) +{ + m_type = type; +} + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/instrument.h b/examples/declarative/tutorials/extending/chapter5-plugins/instrument.h new file mode 100644 index 0000000..15f9fae --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/instrument.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef INSTRUMENT_H +#define INSTRUMENT_H + +#include + +class Instrument : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString type READ type WRITE setType) + +public: + Instrument(QObject *parent = 0); + + QString type() const; + void setType(const QString &type); + +private: + QString m_type; +}; + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/musician.cpp b/examples/declarative/tutorials/extending/chapter5-plugins/musician.cpp new file mode 100644 index 0000000..6b66d5d --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/musician.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musician.h" +#include "instrument.h" + +Musician::Musician(QObject *parent) + : QObject(parent) +{ +} + +QString Musician::name() const +{ + return m_name; +} + +void Musician::setName(const QString &name) +{ + m_name = name; +} + +Instrument *Musician::instrument() const +{ + return m_instrument; +} + +void Musician::setInstrument(Instrument *instrument) +{ + m_instrument = instrument; +} + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/musician.h b/examples/declarative/tutorials/extending/chapter5-plugins/musician.h new file mode 100644 index 0000000..3c3eb2e --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/musician.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICIAN_H +#define MUSICIAN_H + +#include + +class Instrument; + +class Musician : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(Instrument* instrument READ instrument WRITE setInstrument) + +public: + Musician(QObject *parent = 0); + + QString name() const; + void setName(const QString &name); + + Instrument *instrument() const; + void setInstrument(Instrument *instrument); + +private: + QString m_name; + Instrument *m_instrument; +}; + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.cpp b/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.cpp new file mode 100644 index 0000000..e2f6244 --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "musicplugin.h" +//![0] +#include "musician.h" +#include "instrument.h" +#include + +void MusicPlugin::registerTypes(const char *uri) +{ + qmlRegisterType(uri, 1, 0, "Musician"); + qmlRegisterType(uri, 1, 0, "Instrument"); +} + +Q_EXPORT_PLUGIN2(musicplugin, MusicPlugin); +//![0] + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.h b/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.h new file mode 100644 index 0000000..df8c9ab --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/musicplugin.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation 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 Technology Preview License Agreement accompanying +** this package. +** +** 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.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MUSICPLUGIN_H +#define MUSICPLUGIN_H + +//![0] +#include + +class MusicPlugin : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + void registerTypes(const char *uri); +}; +//![0] + +#endif + diff --git a/examples/declarative/tutorials/extending/chapter5-plugins/qmldir b/examples/declarative/tutorials/extending/chapter5-plugins/qmldir new file mode 100644 index 0000000..c3afd6b --- /dev/null +++ b/examples/declarative/tutorials/extending/chapter5-plugins/qmldir @@ -0,0 +1 @@ +plugin chapter5-plugins lib -- cgit v0.12