2012-02-02 1 views
4

글쎄, 나는 QML로 일하는 법을 배우고있어 의심 스럽다. 내 예제에서는 ListMleel있는 QML 및 QML 주 파일에 사각형, PathView 등이 있습니다.C++을 사용하여 ListElement를 QML ListModel에 삽입하는 방법은 무엇입니까?

QWidget 또한 내 주 창보다 있습니다. 이 QWidget에는 구성 요소와 같은 QML UI가 포함되어 있습니다. 승인!

C++을 사용하여 QML ListElements를 처리하려면 어떻게해야합니까?
참고 : "처리"라고 말하면, 예를 들어 요소를 포함한다고 말하고 싶습니다. 다음은

은 ... 내 코드의 일부이다 "메뉴"라고 내 ListElement 포함

QML :

import QtQuick 1.0 

ListModel { 
    id: listMovieModel 
    ListElement { name: "Image 1"; iconSource: "pics/image_1.jpg" } 
    ListElement { name: "Image 2"; iconSource: "pics/image_2.jpg" } 
    ListElement { name: "Image 3"; iconSource: "pics/image_3.jpg" } 
    ListElement { name: "Image 4"; iconSource: "pics/image_4.jpg" } 
    ListElement { name: "Image 5"; iconSource: "pics/image_5.jpg" } 
    ListElement { name: "Image 6"; iconSource: "pics/image_6.jpg" } 
} 

내 주요 QML :

Rectangle { 
    width: 500 
    height: 600 
    color: "transparent" 

    PathView { 
     id: view 
     focus: true 
     width: parent.width 
     height: parent.height + y 
     y: -150 
     model: Menu1 {} //First QML showed 
     delegate: Image { 
      source: iconSource 
      width: 64 
      height: 90 
      scale: PathView.isCurrentItem ? 3.5 * y/parent.height : 2.0 * y/parent.height 
      z: y 
      smooth: true 
     } 
     path: MyGeometricFigure { //This a another file, but is confidential 
      width: view.width 
      height: view.height 
     } 
     preferredHighlightBegin: 0 
     preferredHighlightEnd: 0 
     highlightRangeMode: PathView.StrictlyEnforceRange 
     Keys.onLeftPressed: decrementCurrentIndex() 
     Keys.onRightPressed: incrementCurrentIndex() 
    } 
} 

등을 I QWidget의 구성 요소처럼 QML을 사용하십시오.

MainForm::MainForm(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::MainForm) 
{ 
    ui->setupUi(this); 
    this->resize(1024, 576); 

    QDeclarativeView *myQMLTest = new QDeclarativeView(QUrl::fromLocalFile("myMainQML.qml")); 
    myQMLTest->setStyleSheet(QString("background: transparent; width: 600px")); 

    this->ui->frameListVideoGallery->layout()->addWidget(myQMLTest); 
    myQMLTest->setFocus(); 
    myQMLTest->installEventFilter(this); 
} 

이 기사를 보았지만 C++을 사용하여 LisModel을 변경할 수 없습니다. 여기서 봤어요 http://doc.qt.nokia.com/4.7/qdeclarativemodels.html#c-data-models PathView를 사용하는 예제에서 http://doc.qt.nokia.com/4.7/qdeclarativeexamples.html

나를 도울 수있는 사람이 있습니까?

감사합니다.

답변

8

좋아. 나는 당신이 다음과 같은 것을 할 수 있다고 생각합니다 :

기본 QML 파일에 간단한 기능을 추가하십시오.

Rectangle { 
    // ... 

    function append(newElement) { 
     view.model.append(newElement) 
    } 

    PathView { 
     id: view 

     // ... 

     model: Menu1 {} //First QML showed 

     // ... 
    } 
} 

이 메서드는 ListModel에서 상응하는 메서드를 호출합니다. 더 많이 지원되는 방법은 find there입니다.

그런 다음 C++ 코드에서이 메서드를 호출하면됩니다. 다음과 같은 방법으로이 작업을 수행 할 수 있습니다.

MainForm::MainForm(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::MainForm) 
{ 
    ui->setupUi(this); 
    this->resize(1024, 576); 

    QDeclarativeView *myQMLTest = new QDeclarativeView(QUrl::fromLocalFile ("myMainQML.qml")); 
    myQMLTest->setStyleSheet(QString("background: transparent; width: 600px")); 

    QVariantMap newElement; // QVariantMap will implicitly translates into JS-object 
    newElement.insert("name",  "Image 13"  ); 
    newElement.insert("iconSource", "pics/image_13.jpg"); 

    QMetaObject::invokeMethod(
     myQMLTest->rootObject(),       // for this object we will call method 
     "append",           // actually, name of the method to call 
     Q_ARG(QVariant, QVariant::fromValue(newElement)) // method parameter 
    ); 

    this->ui->frameListVideoGallery->layout()->addWidget(myQMLTest); 
    myQMLTest->setFocus(); 
    myQMLTest->installEventFilter(this); 
} 

자세한 내용은 this을 확인해야합니다.

또한 QDeclarativeEngine 및 QDeclarativeContext를 사용하여 일부 데이터를 qml 속성을 통해 전달한 다음이 데이터와 목록 모델을 JavaScript 코드로 처리 할 수 ​​있습니다.

+0

헤이 @GooRoo, 귀하의 답변을 주셔서 감사합니다. 그러나 나는 다른 방법을 사용했다 ... 사실, 여기에이 예제를 보았다. [link] (http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html로), 그의 예를 변경하고 "model : Menu1 {}"을 "model : adrianoTest"에 대한 PathView에서 변경 했으므로 main.cpp에 사용 된 예제를 기반으로 : this-> m_ImageCarousel-> rootContext() - > setContextProperty ("testeAdriano", this-> m_Model); 그리고 일했습니다! 고마워요 ... –

+0

방금 ​​질문에 대답하려고했습니다 :) 당신이 원하는 것을하기 위해 다른 방법을 찾았 기 때문에 다행입니다. – GooRoo

+0

@AdrianoLeal 링크가 죽었습니다. C++에서 목록을 채우는 방법을 보여주는 방법은 무엇입니까? – Boy

1

@GooRoo의 답변을 Qt 초보자에게 더 완전하게/유용하게 만들고 싶습니다. Qt Creator를 사용하는 경우 QmlApplicationViewer를 사용하여 템플릿을 시작할 것입니다.

CPP :

Q_DECL_EXPORT int main(int argc, char *argv[]) 
{ 
    QScopedPointer<QApplication> app(createApplication(argc, argv)); 
    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create()); 

    viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto); 
    viewer->setMainQmlFile(QLatin1String("qml/mydemo/main.qml")); 
    viewer->showExpanded(); 

    QMetaObject::invokeMethod(
     (QObject *)viewer->rootObject()->findChild<QObject *>("myModel"), // QML item 
     "test"       // name of the method to call 
             // without parameters 
    ); 

    return app->exec(); 
} 

QML : 당신이 (http://www.lothlorien.com/kf6gpe/?p=309에 감사) 같은 것을 할 필요가 GooRoo에서 답을 적용하려면

PageStackWindow { 
    id: mainApp 
    ... 

    ListModel { 
     id: myModel 
     objectName: myModel //THIS IS NEEDED BY findChild() 
     ... 
     function test() { 
      console.log("TEST OK"); 
     } 
    } 

    Page { 
     ... 
    } 
} 
관련 문제