2012-10-29 45 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()調用。可能不值得。