2013-10-16 1 views
2

간단한 직사각형과 같은 QML ObjectModel 요소를 동적으로 만들고 ListView에 표시하려고합니다. 그러나 내 신청서를 작성할 때 아무 것도 나타나지 않습니다. 콘솔 로그에 메시지 만 표시됩니다 : "만든 그래픽 개체가 그래픽 장면에 배치되지 않았습니다". 이 접근법이나 다른 방법으로 올바르게 할 수있는 방법이 있습니까?QML ObjectModel 요소의 동적 생성

UPD : 코드

main.qml

import "imgRectsCreation.js" as ImgRectsCreationScript 
import QtQuick 2.0 
import QtQml.Models 2.1 

Rectangle { 
    id: root 

    ObjectModel{ 
     id: itemModel 
     Component.onCompleted: ImgRectsCreationScript.createImgRects(); 
    } 

    ListView { 
     id: view 
     clip: true 
     anchors { fill: root; bottomMargin: 30 } 
     model: itemModel 
     preferredHighlightBegin: 0; preferredHighlightEnd: 0 
     highlightRangeMode: ListView.StrictlyEnforceRange 
     orientation: ListView.Horizontal 
     snapMode: ListView.SnapOneItem; flickDeceleration: 2000 
     cacheBuffer: 200 
    } 

    Rectangle { 
     width: root.width; height: 30 
     x: 10 
     y: 330 
     color: "gray" 

     Row { 
      anchors.centerIn: parent 
      spacing: 20 

      Repeater { // little points at the bottom 
      model: itemModel.count 

       Rectangle { 
        width: 5; height: 5 
        radius: 3 
        color: view.currentIndex == index ? "sandybrown" : "white" 

        MouseArea { 
         width: 20; height: 20 
         anchors.centerIn: parent 
         onClicked: view.currentIndex = index 
        } 
       } 
      } 
     } 
    } 
} 

imgRectsCreation.js

var sprite; 
var component; 

function createImgRects() { 
    component = Qt.createComponent("ImgRectSprite.qml"); 
    if (component.status === Component.Ready) 
     finishCreation(); 
    else 
     component.statusChanged.connect(finishCreation); 
} 

function finishCreation() { 
    for (var i = 0; i < 3; i++) { 
     if (component.status === Component.Ready) { 
      sprite = component.createObject(itemModel, {"x": 10, "y": 10}); 
      if (sprite === null) { // Error Handling 
       console.log("Error creating object"); 
      } 
     } 
     else if (component.status === Component.Error) { // Error Handling 
      console.log("Error loading component:", component.errorString()); 
     } 
    } 
} 

그리고 마지막으로 - ImgRectSprite.qml

import QtQuick 2.0 

Rectangle { 
    width: 100; height: 100; 
    color: "red" 
    Image { 
     width: parent.width 
     height: parent.height 
     source: window.slotGetFileUrl() 
    } 
} 
+1

는 – Polentino

+0

@Polentino 좋아, 여기 당신의 조언, 내가 내 작업을 수행하려고 할 것이다 – brightside90

답변

0

나는 JS 코드에서 구성 요소 생성의 큰 팬이 아니에요 - 난 .qml 파일에 QML 코드를 넣어 경향이 있고, "무거운"코드가 내부 .js 파일 (잘은 JS가 Afterall는입니다) -.

대신 Loader 개체를 사용하여 qml 개체를 동적으로 만들려고 했습니까?

+0

덕분이다. 특히 QML과 Qt Quick에 정말 초보자이기 때문에 특히 흥미로운 코드 스 니펫이 많은데, 도움이되지 않을 것입니다. – brightside90

+0

예를 들어, 5 로더를 사용하여 이미지가 다른 직사각형? – brightside90

0

글쎄, 나 혼자서 해결했다. 그러나 나는 그것이 옳은 결정이라는 것을 완전히 확신하지 못합니다.

  1. 내가

    ListModel { 
        id: dataModel 
    
        dynamicRoles: true 
        Component.onCompleted: { 
         for (var i = 0; i < 3; i++) 
         { 
          append({ color: "orange" }) 
         } 
        } 
    } 
    
  2. 마지막의 ListModel를 내 main.qml

  3. 에서 objectModel를을 제거 ... 그리고 로 교체하기로 결정했습니다, 나는에 대리자를 추가했습니다 내 ListView

    delegate: Rectangle { 
        width: view.width 
        height: view.height 
        color: model.color 
    
        Image { 
         width: parent.width 
         height: parent.height 
         source: window.slotGetFileUrl() // includes logic to select images 
        } 
    
  4. ???

  5. 이익! 귀하의 관심

감사합니다 :) 일부 코드가 정말 감사하겠습니다