Important: Please read the Qt Code of Conduct -

Calling an object factory method from QML (and getting the QDView)

  • Hi,

    I am winrking in a project where I have a class A that inherits from QDeclarativeItem.
    What I want, is to have an instance of my class A (displayed somewhere on the qml window) and when I click on this one instance (lets call this instance "factory instance"), it create a new instance of the class A.
    Thus, I made a static method in the class A that looks like this:
    A* A::AFactory(QDeclarativeView* v,
    QDeclarativeItem* parent){
    QDeclarativeComponent component(v->engine(), QUrl("test.qml"));
    A *a = qobject_cast<A >(component.create());
    qDebug() << component.errors();
    return a;
    What I want to do is to call this method from QML, like this, for example (here I just want to create it when clicking on the background, not in the "facotry instance"):
    color: "blue"
    id: base
    width: 200
    height: 200
    anchors.fill: parent
    onClicked: {factory.AFactory(view,base);}
    id: factory

    In irder to get the view, I have done
    QDeclarativeView* view = new QDeclarativeView;
    QDeclarativeContext* cxt = view->rootContext();
    in my main function.
    As I result, I do have the view in QML (with a console.log(view) in QML I can display its address and it is the same than from C++) but QML does not get it really well... The rror is:
    Error: Unknown method parameter type: QDeclarativeView*

    I believe that what I am tring to do is quite complicated... Is there another way to do it more easily? Or is there a solution to my issue?

    Thank you a lot!

Log in to reply