Important: Please read the Qt Code of Conduct -

QML Loader Object

  • We always us QML to make application having a beautiful GUI, and can be controlled by mouse on the elements on it.

    But this time I have got a problem.
    I make a application with two windows, one is a QML window, and the other is a common dialog window.
    The QML window is not controlled directly by mouse, but rather by the dialog window.

    The QML window have many pages, When I want the page content to be changed, I use a "Loader" in QML item to load some other qml file to screen.

    Then it is the problem.
    If I want to control the elements on some other qml, for example Text, or Image object.
    I cannot access them in cpp file.

    Would you please give me some method on my problem?
    I cost one day on this, but I did not have any solution.
    Thank you.

  • Moderators

    In QML, subobjects of the file loaded by Loader are accessible through the "item" property. Does it not work with C++, or you have not tried?

  • But I need to use it in cpp file.

    “item” property
    In cpp file, how I can use it?
    I think it is just a property.
    If I can change it into a object pointer by type cast?
    like below
    QGraphicsObject *subobject =
    qobject_cast<QGraphicsObject *>(v->rootObject()->findChild<QObject *>("mainloader")->property("item"));

  • And what is more,
    the sentences
    (v->rootObject()->findChild<QObject >(“mainloader”)->property(“item”))
    returns a QVariant(QGraphicsObject
    , ) value.
    But I cannot convert it to a QGraphicsObject* pointer.

  • a property in accessible in QML coul be of multiple kind int, string, .... see "here":

    But in C++you had to create some code like"int this documentation.":

  • Moderators

    Please wrap your code in '@' tags.

    Try this:
    (v->rootObject()->findChild<QObject *>(“mainloader”)->property(“item”)).value<QGraphicsObject>();

  • @ (v->rootObject()->findChild<QObject *>(“mainloader”)->property(“item”)).value<QGraphicsObject>();@

    This may not work.
    I tried similar method today.

    I will confirm it again tomorrow.

Log in to reply