1

I'm using Qt 5.0.2 and QtQuick 2.0 to try building a very simple QML application, displaying tiles.

I want the tiles to be dynamically created with a repeater, interfacing C++.

I found an example on how to do it (MineHunt), but this example is using QtQuick 1 and Qt 4.7.

Here is my code :

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

This file imports a folder named tiles containing another QML file named tile.qml which contains the following code :

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

I also have a class which implements methods needed to provide the tiles model.

It compiles fine but when I run it, I get the following error :

ReferenceError: tile is not defined

What's wrong with my code ?

4

1 回答 1

1
delegate: tile

这是错误的,因为当前范围内没有定义“平铺”名称。您可能想在那里实例化一个 tile 组件,因此您需要:

delegate: tile {}

也是错误的另一个原因:类型名称必须以大写字母开头。所以:

delegate: Tile {}

这是正确的,但它不会按原样工作,因为 QML 不知道在哪里可以找到Tile类型。您需要qmldir在子目录中添加一个文件,tiles其中包含类似这样的内容

module tiles
Tile 1.0 tile.qml
于 2013-06-25T12:46:38.073 回答