简体   繁体   English

QtQuick2和C ++:使用中继器

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

I'm using Qt 5.0.2 and QtQuick 2.0 to try building a very simple QML application, displaying tiles. 我正在使用Qt 5.0.2和QtQuick 2.0尝试构建一个非常简单的QML应用程序,以显示图块。

I want the tiles to be dynamically created with a repeater, interfacing C++. 我希望使用中继器(与C ++接口)动态创建图块。

I found an example on how to do it ( MineHunt ), but this example is using QtQuick 1 and Qt 4.7. 我找到了一个有关如何执行此操作的示例( MineHunt ),但是此示例使用的是QtQuick 1和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 : 该文件导入名为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)
        }
    }
}

I also have a class which implements methods needed to provide the tiles model. 我还有一个类,实现提供tiles模型所需的方法。

It compiles fine but when I run it, I get the following error : 它编译正常,但是当我运行它时,出现以下错误:

ReferenceError: tile is not defined

What's wrong with my code ? 我的代码有什么问题?

 delegate: tile 

This is wrong because there's no "tile" name defined in the current scope. 这是错误的,因为在当前范围中没有定义“ tile”名称。 You probably want to instantiate a tile component there, so you need: 您可能想在那里实例化一个tile组件,因此您需要:

delegate: tile {}

This is also wrong for another reason: type names must start with capital letters. 由于另一个原因,这也是错误的:类型名称必须以大写字母开头。 So: 所以:

delegate: Tile {}

This is correct, but it won't work as is because QML has no idea where to find the Tile type. 这是正确的,但由于QML不知道在何处找到Tile类型,因此无法按原样工作。 You need to add a qmldir file inside your tiles subdirectory containing something like this 您需要在tiles子目录中添加一个qmldir文件,其中包含如下内容

module tiles
Tile 1.0 tile.qml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM