PDA

View Full Version : MyModelClass as QVariant, exposed and used in qml file - (it blocker me)



moti.lahiani
23rd November 2011, 10:07
Hello all

i have cpp model class "MyModel" inherits QAbstractListModel (or other abstract model) and implements some method. including default and copy constructor and destructor
class MyModel : public QAbstractListModel
{
......
........
};
i want to register this class to MetaType so i can use it as QVariant i add the following line at the end of MyModel.h file
Q_DECLARE_METATYPE(MyModel)
Q_DECLARE_METATYPE(MyModel*)

in some place of my code i create an instance of MyModel as:
MyModel *my_model = new MyModel();
and i want to store a copy of my_model in QVariant

1) How can i do it?

the reason i want my_model as variant is to use it in qml file named named "MyList.qml" as follow:
Item{
ListView{
id: myList
delegate: some_delegate
}
function setListModel(mModel)
{
myList.model = mModel
}
}

and i want to set the model to the "myList" using the setListModel function
so i use QDeclarativeComponent to create qml item from file "MyList.qml" as following:

QDeclarativeComponent *mpComponent = new QDeclarativeComponent(engine, QUrl::fromLocalFile( "MyList.qml" ));
QObject *myObject = mpComponent->create();
QDeclarativeItem *mpItem = qobject_cast<QDeclarativeItem*>(myObject);

and try to pass the variant of my_model to set myList model as follow:
QMetaObject::invokeMethod(mpItem, "setListModel", Qt::DirectConnection, Q_ARG(QVariant, QVariant::fromValue<MyModel*>( my_model )));

but it doesn't work it show error of Unknown role

can some one help me to understand what is the problem?
or how can i do it