mariomunera
mariomunera

Reputation: 323

Add/Insert dynamically QML element into QML view with C++

I'mt trying to add a custom QML element that I create into another QML view already loaded from C++.

The context is the following: I'm loading a QML view from C++, and I need to inject another QML custom component that I build into this QML view. All in C++.

I've been searching for 4 hours and I haven't found a way to acomplish this.

Here's some code to give you a better perspective:

QmlDocument *qml = QmlDocument::create("asset:///PosicionConsolidad.qml").parent(this);
qml->setContextProperty("pos", this);

Page *page = qml->createRootObject<Page>();
myST = GlobalST::getInstance();
LoadInfo();

_mRoot->push(page);
_app->setScene(_mRoot);

void Project::LoadInfo() {
    QmlDocument *qml = QmlDocument::create("asset:///customComponents/TableRow.qml").parent(this);
    //Here's where I need to append this new QML custom element to the 
    //page previously loaded.
    //I don't know if I can just inject it or I need to make a find child to 
    //maybe a parent container in the QML view and then add it there. But I 
    //also tried that and didn't work out.
}

Please help. Regards.

Upvotes: 3

Views: 4945

Answers (2)

torpesco
torpesco

Reputation: 21

You could create the Page and root container in C++ and then add everything else from the two QML files. Really, though, that replaces the findChild() call with code for creating the page and container. Probably not worth it.

Upvotes: 2

mariomunera
mariomunera

Reputation: 323

Well, I finally found a way through it that isn't exactly the cleaner or most beauty of all. I used the Find Child function to get a Container that belongs to the QML loaded view and then add my QML custom component as many times as I need it to.

Some code below:

Class::Constuctor(bb::cascades::Application *app,
        NavigationPane* mRoot) :
        QObject(app) {

    _app = app;
    _mRoot = mRoot;

    QmlDocument *qml =
            QmlDocument::create("asset:///PosicionConsolidad.qml").parent(this);
    qml->setContextProperty("pos", this);

    posicionConsolidadaPage = qml->createRootObject<Page>();
    _mRootContainer = posicionConsolidadaPage->findChild<Container*>("posicion_consolidadad");

    LoadInfo();

    _mRoot->push(posicionConsolidadaPage);
    _app->setScene(_mRoot);
}

void Class::LoadInfo() {

        QmlDocument *qml = QmlDocument::create(
                "asset:///customComponents/TableRow.qml").parent(this);
        Container *activesHeader = qml->createRootObject<Container>();

        AbsoluteLayout *pAbsoluteLayout = new AbsoluteLayout();
        activesHeader->setLayout(pAbsoluteLayout);

        AbsoluteLayoutProperties* pProperties = AbsoluteLayoutProperties::create();
        pProperties->setPositionX(0);
        pProperties->setPositionY(155);
        activesHeader->setLayoutProperties(pProperties);

        _mRootContainer->add(activesHeader);
}

Hope it helps. If anybody knows how to add the new component directly to the Page object or something like that please post it :)

Upvotes: 1

Related Questions