diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-01-08 04:32:41 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-01-08 04:32:41 (GMT) |
commit | 9151c4838ac7428107246abb9c99f296ab395c3d (patch) | |
tree | c677bd3fbe24ab3cdbde2b7a5e9cd608a1c8e27d /examples/declarative | |
parent | 42ab4b839af7d02258bc40389f11ec615cf42f0c (diff) | |
download | Qt-9151c4838ac7428107246abb9c99f296ab395c3d.zip Qt-9151c4838ac7428107246abb9c99f296ab395c3d.tar.gz Qt-9151c4838ac7428107246abb9c99f296ab395c3d.tar.bz2 |
Allow QML types defined in both C++ and QML files to be in the same module.
Diffstat (limited to 'examples/declarative')
-rw-r--r-- | examples/declarative/plugins/README | 8 | ||||
-rw-r--r-- | examples/declarative/plugins/files/Clock.qml | 50 | ||||
-rw-r--r-- | examples/declarative/plugins/files/center.png | bin | 0 -> 765 bytes | |||
-rw-r--r-- | examples/declarative/plugins/files/clock.png | bin | 0 -> 20653 bytes | |||
-rw-r--r-- | examples/declarative/plugins/files/hour.png | bin | 0 -> 625 bytes | |||
-rw-r--r-- | examples/declarative/plugins/files/minute.png | bin | 0 -> 625 bytes | |||
-rw-r--r-- | examples/declarative/plugins/plugin.cpp | 82 | ||||
-rw-r--r-- | examples/declarative/plugins/plugins.pro | 4 | ||||
-rw-r--r-- | examples/declarative/plugins/plugins.qml | 22 |
9 files changed, 135 insertions, 31 deletions
diff --git a/examples/declarative/plugins/README b/examples/declarative/plugins/README index e67b1de..621f570 100644 --- a/examples/declarative/plugins/README +++ b/examples/declarative/plugins/README @@ -1,6 +1,8 @@ -This example show a C++ plugin that provides additional modules of QML types -(in this case, a single module with a single extra type). -to run: +This example shows a module "com.nokia.TimeExample" that is implelement +by a C++ plugin (providing the "Time" type), and by QML files (providing the +"Clock" type). + +To run: make install qmlviewer plugins.qml diff --git a/examples/declarative/plugins/files/Clock.qml b/examples/declarative/plugins/files/Clock.qml new file mode 100644 index 0000000..01ec686 --- /dev/null +++ b/examples/declarative/plugins/files/Clock.qml @@ -0,0 +1,50 @@ +import Qt 4.6 + +Item { + id: clock + width: 200; height: 200 + + property alias city: cityLabel.text + property var hours + property var minutes + property var shift : 0 + + Image { id: background; source: "clock.png" } + + Image { + x: 92.5; y: 27 + source: "hour.png" + smooth: true + transform: Rotation { + id: hourRotation + origin.x: 7.5; origin.y: 73; angle: 0 + angle: SpringFollow { + spring: 2; damping: 0.2; modulus: 360 + source: (clock.hours * 30) + (clock.minutes * 0.5) + } + } + } + + Image { + x: 93.5; y: 17 + source: "minute.png" + smooth: true + transform: Rotation { + id: minuteRotation + origin.x: 6.5; origin.y: 83; angle: 0 + angle: SpringFollow { + spring: 2; damping: 0.2; modulus: 360 + source: clock.minutes * 6 + } + } + } + + Image { + anchors.centerIn: background; source: "center.png" + } + + Text { + id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white" + anchors.horizontalCenter: parent.horizontalCenter + } +} diff --git a/examples/declarative/plugins/files/center.png b/examples/declarative/plugins/files/center.png Binary files differnew file mode 100644 index 0000000..7fbd802 --- /dev/null +++ b/examples/declarative/plugins/files/center.png diff --git a/examples/declarative/plugins/files/clock.png b/examples/declarative/plugins/files/clock.png Binary files differnew file mode 100644 index 0000000..462edac --- /dev/null +++ b/examples/declarative/plugins/files/clock.png diff --git a/examples/declarative/plugins/files/hour.png b/examples/declarative/plugins/files/hour.png Binary files differnew file mode 100644 index 0000000..f8061a1 --- /dev/null +++ b/examples/declarative/plugins/files/hour.png diff --git a/examples/declarative/plugins/files/minute.png b/examples/declarative/plugins/files/minute.png Binary files differnew file mode 100644 index 0000000..1297ec7 --- /dev/null +++ b/examples/declarative/plugins/files/minute.png diff --git a/examples/declarative/plugins/plugin.cpp b/examples/declarative/plugins/plugin.cpp index ac44e7e..9688caf 100644 --- a/examples/declarative/plugins/plugin.cpp +++ b/examples/declarative/plugins/plugin.cpp @@ -44,22 +44,35 @@ #include <qdebug.h> #include <qdatetime.h> #include <qbasictimer.h> +#include <qapplication.h> -class Time : public QObject +// Implements a "Time" class with hour and minute properties +// that change on-the-minute yet efficiently sleep the rest +// of the time. + +class MinuteTimer : public QObject { Q_OBJECT - Q_PROPERTY(int hour READ hour NOTIFY timeChanged) - Q_PROPERTY(int minute READ minute NOTIFY timeChanged) - public: - Time(QObject *parent=0) : QObject(parent) + MinuteTimer(QObject *parent) : QObject(parent) + { + } + + void start() { - timerEvent(0); - timer.start(30000,this); + if (!timer.isActive()) { + time = QTime::currentTime(); + timer.start(60000-time.second()*1000, this); + } } - int minute() const { return t.minute(); } - int hour() const { return t.hour(); } + void stop() + { + timer.stop(); + } + + int hour() const { return time.hour(); } + int minute() const { return time.minute(); } signals: void timeChanged(); @@ -67,18 +80,67 @@ signals: protected: void timerEvent(QTimerEvent *) { - t = QTime::currentTime(); + QTime now = QTime::currentTime(); + if (now.second() == 59 && now.minute() == time.minute() && now.hour() == time.hour()) { + // just missed time tick over, force it, wait extra 0.5 seconds + time.addSecs(60); + timer.start(60500, this); + } else { + time = now; + timer.start(60000-time.second()*1000, this); + } emit timeChanged(); } private: + QTime time; QBasicTimer timer; +}; + +class Time : public QObject +{ + Q_OBJECT + Q_PROPERTY(int hour READ hour NOTIFY timeChanged) + Q_PROPERTY(int minute READ minute NOTIFY timeChanged) + +public: + Time(QObject *parent=0) : QObject(parent) + { + if (++instances == 1) { + if (!timer) + timer = new MinuteTimer(qApp); + connect(timer, SIGNAL(timeChanged()), this, SIGNAL(timeChanged())); + timer->start(); + } + } + + ~Time() + { + if (--instances == 0) { + timer->stop(); + } + } + + int minute() const { return timer->minute(); } + int hour() const { return timer->hour(); } + +signals: + void timeChanged(); + +private: QTime t; + static MinuteTimer *timer; + static int instances; }; +int Time::instances=0; +MinuteTimer *Time::timer=0; + + QML_DECLARE_TYPE(Time); QML_DEFINE_TYPE(com.nokia.TimeExample,1,0,Time,Time); + class QExampleQmlPlugin : public QmlModulePlugin { Q_OBJECT diff --git a/examples/declarative/plugins/plugins.pro b/examples/declarative/plugins/plugins.pro index a1a94f9..4109eba 100644 --- a/examples/declarative/plugins/plugins.pro +++ b/examples/declarative/plugins/plugins.pro @@ -5,7 +5,9 @@ CONFIG += qt plugin declarative SOURCES += plugin.cpp target.path += $$[QT_INSTALL_PLUGINS]/qmlmodules -INSTALLS += target +sources.files += files/Clock.qml files/qmldir files/background.png files/center.png files/clock-night.png files/clock.png files/hour.png files/minute.png +sources.path += $$[QT_INSTALL_DATA]/qml/com/nokia/TimeExample +INSTALLS += target sources VERSION=1.0.0 diff --git a/examples/declarative/plugins/plugins.qml b/examples/declarative/plugins/plugins.qml index df6b5d4..97c1b69 100644 --- a/examples/declarative/plugins/plugins.qml +++ b/examples/declarative/plugins/plugins.qml @@ -1,23 +1,11 @@ -import Qt 4.6 import com.nokia.TimeExample 1.0 -Item { +Clock { // this class is defined in QML (files/Clock.qml) - Time { id: time } - - width: 200 - height: 100 - - Rectangle { - anchors.fill: parent - color: "blue" + Time { // this class is defined in C++ (plugins.cpp) + id: time } - Text { - text: time.hour + ":" + (time.minute < 10 ? "0" : "") + time.minute - anchors.centerIn: parent - color: "white" - font.pixelSize: 42 - font.bold: true - } + hours: time.hour + minutes: time.minute } |