diff options
author | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-07-28 20:05:40 +1000 |
---|---|---|
committer | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-07-28 20:05:40 +1000 |
commit | 68b2811e8ef13e423771167433b63e8fbd56ea8e (patch) | |
tree | cdbfc5ddf62dd139b33067eb5a1e610946a8d04c | |
parent | a5d037c51ccbaa3f2a71bd39db187414bd1c78fe (diff) |
Add example that uses QmlComponent to create view items in QGraphicsListView.
-rw-r--r-- | examples/qmlListView/main.cpp | 98 | ||||
-rw-r--r-- | examples/qmlListView/qmlListView.pro | 15 |
2 files changed, 113 insertions, 0 deletions
diff --git a/examples/qmlListView/main.cpp b/examples/qmlListView/main.cpp new file mode 100644 index 0000000..7c391f4 --- /dev/null +++ b/examples/qmlListView/main.cpp @@ -0,0 +1,98 @@ +#include <QtGui> +#include <QtDeclarative> + +#include <qgraphicslistview.h> + +#include <qlistwidgetng.h> +#include <qlistcontroller.h> +#include <qlistdefaultmodel.h> + +#include <qdebug.h> + +// creator + +class QmlListViewCreator : public QtGraphicsListViewItemCreatorBase +{ +public: + ~QmlListViewCreator(); + QGraphicsObject *create(int index, QtGraphicsListView *view); + QGraphicsObject *reassign(int index, QGraphicsObject *item, QtGraphicsListView *view); + void recycle(QGraphicsObject *item, QtGraphicsListView *view); + QmlComponent *m_component; + QmlEngine *m_engine; +}; + +QmlListViewCreator::~QmlListViewCreator() +{ +} + +QGraphicsObject *QmlListViewCreator::create(int index, QtGraphicsListView *view) +{ + QGraphicsObject *gobj = 0; + QmlContext *context = new QmlContext(m_engine); + + QHash<int, QByteArray> roles = view->model()->roles(); + QHash<int, QVariant> data = view->model()->data(index); + QHash<int, QVariant>::ConstIterator it = data.constBegin(); + for (;it != data.constEnd(); ++it) { + QByteArray name = roles.value(it.key()); + QVariant value = it.value(); + context->setContextProperty(name, value); + } + + if (QObject *obj = m_component->create(context)) { + gobj = static_cast<QGraphicsObject*>(obj); + gobj->setParentItem(view); + context->setParent(gobj); + } + + if (!context->parent()) + delete context; + + return gobj; +} + +QGraphicsObject *QmlListViewCreator::reassign(int index, QGraphicsObject *item, QtGraphicsListView *view) +{ + delete item; + return create(index, view); + //return item; +} + +void QmlListViewCreator::recycle(QGraphicsObject *item, QtGraphicsListView *view) +{ + Q_UNUSED(view); + delete item; +} + +// main + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QmlEngine engine; + + QString qml = "import Qt 4.6\n" + "Rect { \n" + " width: 200\n" + " height: 50\n" + " color: \"green\"\n" + " Text {\n" + " id: \"text\"\n" + " text: DisplayRole\n" + " x: 20; y: 20\n" + " }\n" + "}"; + + QmlListViewCreator *creator = new QmlListViewCreator; + creator->m_component = new QmlComponent(&engine, qml.toUtf8(), QUrl()); + creator->m_engine = &engine; + + QtListWidgetNG widget; + widget.controller()->view()->setItemCreator(creator); + for (int i = 0; i < 100; ++i) + widget.defaultModel()->appendItem(new QtListDefaultItem(QString("foo ") + QString::number(i))); + widget.show(); + + return app.exec(); +} diff --git a/examples/qmlListView/qmlListView.pro b/examples/qmlListView/qmlListView.pro new file mode 100644 index 0000000..3fbd254 --- /dev/null +++ b/examples/qmlListView/qmlListView.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Wed Jul 22 14:34:40 2009 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative +CONFIG += debug + +include(../examples.pri) + +# Input +SOURCES += main.cpp |