2012-10-29 50 views
1

I'mt想补充一点,我将创建由C已经加载另一个QML视图++的自定义QML元素。添加/插入动态QML元素QML将与视图C++

上下文如下:我加载从C++一个QML视图,我需要注入我建立到这个QML视图另一QML定制组件。全部用C++。

我一直在寻找4小时,我还没有找到一种方法来acomplish这一点。

下面是一些代码给你一个更好的视角:

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. 
} 

请帮助。问候。

回答

1

好了,我终于找到了通过它的方式,是不完全的清洁剂或所有最美丽。我使用Find Child函数获取属于QML加载视图的Container,然后根据需要多次添加我的QML自定义组件。

一些下面的代码:

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); 
} 

希望它能帮助。如果有人知道如何直接添加新的组件到页面对象或类似的东西,请张贴:)

+1

我认为你找到的方法是正确的方法。我们绝不会将组件添加到页面中,而是添加到Container中。 – Benoit

2

你可以在C++中创建的页面和根容器,然后添加一切从两个QML文件别的。实际上,它用代码创建页面和容器来代替findChild()调用。可能不值得。