[Qt-qml] C++ Data Models and plugin mechanism
Bartosh Wroblevksy
bartosh at live.com
Tue May 18 20:48:22 CEST 2010
Thanks, however it's not working for me. There are a few things that are eluding me. I took the plugin example and tried to merge in your comments. Not only, I am not displaying the data. I am getting the following errors to the console
QDeclarativeExpression: Expression "(function() { return myPluggedInObj.myModel })" depends on non-NOTIFYable properties: MyObj::myModel
It just does not work. What am I doing wrong?
This would be my plugin.cpp
#include <QtDeclarative/QDeclarativeExtensionPlugin>#include <QtDeclarative/qdeclarative.h>#include <qdebug.h>#include <qdatetime.h>#include <qbasictimer.h>#include <qapplication.h>
#include <QStringList>
class MyObj : public QObject { Q_OBJECT Q_PROPERTY(QStringList myModel READ myModel); QStringList myModel() const {
QStringList aStringList; aStringList.append( "Fred" ); aStringList.append( "1" ); aStringList.append( "2" ); aStringList.append( "3" ); return aStringList; }};
class QExampleQmlPlugin : public QDeclarativeExtensionPlugin{ Q_OBJECTpublic: void registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("com.nokia.TimeExample")); qmlRegisterType<MyObj>(uri, 1, 0, "MyObj"); }};
#include "plugin.moc"
Q_EXPORT_PLUGIN2(qtimeexampleqmlplugin, QExampleQmlPlugin);
this would be my qml file plugins.qml
import Qt 4.7import com.nokia.TimeExample 1.0 // import types from the plugin
Rectangle { width: 600; height: 300; color: "white"
// MyPets model is defined in dummydata/MyPetsModel.qml // The viewer automatically loads files in dummydata/* to assist // development without a real data source. // This one contains my pets. // Define a delegate component. A component will be // instantiated for each visible item in the list. Component { id: petDelegate Item { width: 200; height: 50 Column { Text { text: modelData } } } }
MyObj { id: myPluggedInObj }
ListView { id: list1 width: 200; height: parent.height model: myPluggedInObj.myModel; focus: true }
// MyObj { id: myPluggedInObj }// ListView { model: myPluggedInObj.myModel; }
}
> From: warwick.allison at nokia.com
> To: bartosh at live.com; qt-qml at trolltech.com
> Date: Tue, 18 May 2010 00:34:57 +0200
> Subject: RE: [Qt-qml] C++ Data Models and plugin mechanism
>
> > So you can only extend QML Data Model by reimplementing
> > QDeclarativeView. Am I correct?
>
> Fortunately not. setContextProperty is just ONE way of creating an identified object.
>
> To make a model as a type in a plugin, just inherit your type from QAbstractItemModel (rather than QDeclarativeItem as you would for an item type plugin), register the type in the normal plugin manner:
>
> qmlRegisterType<MyModel>(uri,1,0, "MyModel");
>
> Then you can instantiate your model from QML, giving it whatever id you like, then using it in views:
>
> import myplugin 1.0
> Item {
> ...
> MyModel { id: myPluggedInModel }
> ListView { model: myPluggedInModel; ... }
> }
>
> Plugins can provide any type that subclasses QObject, and QML code can then import that plugin and instantiate the type.
>
> If you want a more simple model, such as the QStringList example in to C++ model documentation, you could make your plugin type just a simple QObject with the stringlist as a property:
>
> class MyObj : public QObject {
> Q_OBJECT
> Q_PROPERTY(QStringList myModel READ myModel);
> QStringList myModel() const { return QStringList() << "Fred" << "Ginger" << "Skipper"; }
> };
>
> Register as normal:
>
> qmlRegisterType<MyObj>(uri,1,0, "MyObj");
>
> then:
>
> import myplugin 1.0
> Item {
> ...
> MyObjl { id: myPluggedInObj }
> ListView { model: myPluggedInObj.myModel; ... }
> }
>
>
> I'll improve the docs in this regard.
>
> --
> Warwick
_________________________________________________________________
Win a $10,000 shopping spree from Hotmail! Enter now.
http://go.microsoft.com/?linkid=9729711
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100518/43775b75/attachment.html
More information about the Qt-qml
mailing list