簡體   English   中英

QtQuick2和C ++:使用中繼器

[英]QtQuick2 and C++ : Using a Repeater

我正在使用Qt 5.0.2和QtQuick 2.0嘗試構建一個非常簡單的QML應用程序,以顯示圖塊。

我希望使用中繼器(與C ++接口)動態創建圖塊。

我找到了一個有關如何執行此操作的示例( MineHunt ),但是此示例使用的是QtQuick 1和Qt 4.7。

這是我的代碼:

import QtQuick 2.0
import "tiles"

Rectangle {
    width: 360
    height: 360
    Grid {

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        anchors.margins: 5
        columns: 3
        spacing: 10

        Repeater {
            id: repeater
            model: tiles
            delegate: tile
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}

該文件導入名為tiles的文件夾,其中包含另一個名為tile.qml QML文件,其中包含以下代碼:

import QtQuick 2.0

Rectangle {
    id: tile
    width: 100
    height: 62
    color: "#ff0303"

    MouseArea {
        anchors.fill: parent
        onClicked: {
            var row = Math.floor(index / 3)
            var col = index - (Math.floor(index / 3) * 3)
            play(row, col)
        }
    }
}

我還有一個類,實現提供tiles模型所需的方法。

它編譯正常,但是當我運行它時,出現以下錯誤:

ReferenceError: tile is not defined

我的代碼有什么問題?

 delegate: tile 

這是錯誤的,因為在當前范圍中沒有定義“ tile”名稱。 您可能想在那里實例化一個tile組件,因此您需要:

delegate: tile {}

由於另一個原因,這也是錯誤的:類型名稱必須以大寫字母開頭。 所以:

delegate: Tile {}

這是正確的,但由於QML不知道在何處找到Tile類型,因此無法按原樣工作。 您需要在tiles子目錄中添加一個qmldir文件,其中包含如下內容

module tiles
Tile 1.0 tile.qml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM