Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling an object factory method from QML (and getting the QDView)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    excalibur1491
    wrote on last edited by
    #1

    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();
    a->setParentItem(parent);
    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"):
    @
    Rectangle{
    color: "blue"
    id: base
    width: 200
    height: 200
    MouseArea{
    anchors.fill: parent
    onClicked: {factory.AFactory(view,base);}
    }
    A{
    id: factory
    }
    }
    @

    In irder to get the view, I have done
    @
    QDeclarativeView* view = new QDeclarativeView;
    view->setSource(QUrl::fromLocalFile("Base.qml"));
    QDeclarativeContext* cxt = view->rootContext();
    cxt->setContextProperty("view",view);
    @
    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!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved