- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在用 C++ 中的游戏板模型编写一个简单的贪吃蛇游戏,其中包含一个二维状态 vector ( std::vector<std::vector<board::state>>
)。现在我想将这个棋盘暴露给 QML,这样它基本上就是某种可以访问模型状态的网格/棋盘。我已经阅读了很多关于这个主题的文章,但仍然无法理解足够的机制来解决我的问题。将文档、教程和博客条目应用到我的问题是我在这里的障碍。
我子类化了QAbstractItemModel
对于我的游戏板模型并实现了必要的功能。现在我想进入 QML 并在那里使用/显示我的模型的内容。
这是我的代码:
board.h
#pragma once
#include <vector>
#include <QAbstractItemModel>
class board : public QAbstractItemModel
{
Q_OBJECT
public:
enum class state
{
empty,
snake,
fruit
};
board(int x, int y);
state get_state(int x, int y) const;
void set_state(int x, int y, state state);
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex& index) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
private:
std::vector<std::vector<state>> m_board;
};
Q_DECLARE_METATYPE(board::state)
board.cpp
#include "board.h"
board::board(int x, int y) :
m_board(x, std::vector<board::state>(y, board::state::empty))
{
}
//----------------------------------------------------------------------------------------------------------------------
board::state board::get_state(int x, int y) const
{
if((size_t) x >= m_board.size() || x < 0)
return board::state::empty;
if((size_t) y >= m_board.at(0).size() || y < 0)
return board::state::empty;
return m_board.at(x).at(y);
}
//----------------------------------------------------------------------------------------------------------------------
void board::set_state(int x, int y, state state)
{
if(get_state(x, y) == state)
return;
m_board.at(x).at(y) = state;
}
//----------------------------------------------------------------------------------------------------------------------
QModelIndex board::index(int row, int column, const QModelIndex&) const
{
if((size_t) row >= m_board.size() || row < 0)
return QModelIndex();
if((size_t) column >= m_board.at(0).size() || column < 0)
return QModelIndex();
return createIndex(row, column);
}
//----------------------------------------------------------------------------------------------------------------------
QModelIndex board::parent(const QModelIndex& index) const
{
if((size_t) index.row() >= m_board.size() || index.row() < 0)
return QModelIndex();
if((size_t) index.column() >= m_board.at(0).size() || index.column() < 0)
return QModelIndex();
return createIndex(index.row(), index.column());
}
//----------------------------------------------------------------------------------------------------------------------
int board::rowCount(const QModelIndex&) const
{
return m_board.size();
}
//----------------------------------------------------------------------------------------------------------------------
int board::columnCount(const QModelIndex&) const
{
return m_board.at(0).size();
}
//----------------------------------------------------------------------------------------------------------------------
QVariant board::data(const QModelIndex& index, int role) const
{
if(!index.isValid())
return QVariant();
if(role != Qt::DisplayRole)
return QVariant();
if((size_t) index.row() >= m_board.size() || index.row() < 0)
return QVariant();
if((size_t) index.column() >= m_board.at(0).size() || index.column() < 0)
return QVariant();
return qVariantFromValue(get_state(index.row(), index.column()));
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickView>
#include <board.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
board game_board(10, 10);
game_board.set_state(4, 9, board::state::snake);
game_board.set_state(3, 10, board::state::fruit);
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("myGameBoard", &game_board);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
GridView {
model: myGameBoard
delegate: Rectangle {
width: 30
height: 30
color: blue
}
}
}
我确定有很多东西是我遗漏的或者是明显错误的。我也知道将 C++ 模型暴露给 QML 是经常被问及的问题,但我仍然无法做到。
陈述几个更具体的问题:
感谢您的关注!
最佳答案
QtQuick 是使用 QML 的主要 UI 框架,基本上只处理列表模型,因此使用角色来模拟表格,例如使用 TableView
parent()
方法是错误的,因为它基本上再次返回相同的索引。这不会对您的情况造成任何问题,因为您有一张 table 而不是一棵树。
建议:如果你只需要一个表模型,派生自QAbstractTableModel
并让它处理index()
和parent()
如果是自定义项,您可以构建一个适用于您的模型的项,甚至可以直接处理数据,而无需模型。
如果您使用该模型并希望从 QML 实例化该模型,那么您需要一个“指向您的模型类的指针”或“指向 QAbstractItemModel
的指针”的属性。
如果您不想使用模型或不需要从 QML 实例化它,那么您根本不需要任何特定属性。
无论哪种情况,您的自定义项都可以使用以下方法之一:
ListView
允许为列表元素、页眉、页脚等设置委托(delegate)关于c++ - 使用 QAbstractItemModel 将 2D C++ 游戏板暴露给 QML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707951/
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我试图用这种形式简单地获取数字 28 integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s 但我得到了两个数字11 和
最近一直在学习D语言。我一直对运行时感到困惑。 从我能收集到的关于它的信息中,(这不是很多)我知道它是一种有助于 D 的一些特性的运行时。像垃圾收集一样,它与您自己的程序一起运行。但是既然 D 是编译
想问一下这两个正则表达式有区别吗? \d\d\d 与 \d{3} 我已经在我的本地机器上使用 Java 和 Windows 操作系统对此进行了测试,两者都工作正常并且结果相同。但是,当在 linux
我正在学习 Go,而且我坚持使用 Go 之旅(exercise-stringer.go:https://tour.golang.org/methods/7)。 这是一些代码: type IPAddr
我在Java正则表达式中发现了一段令我困惑的代码: Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); 要编译的字符串是: String string
我在 ruby 代码上偶然发现了这个。我知道\d{4})\/(\d\d)\/(\d\d)\/(.*)/是什么意思,但是\1-\2-\3-\4 是什么意思? 最佳答案 \1-\2-\3-\4 是 b
我一直在努力解决这个问题,这让我很恼火。我了解 D 运行时库。它是什么,它做什么。我也明白你可以在没有它的情况下编译 D 应用程序。就像 XoMB 所做的那样。好吧,XoMB 定义了自己的运行时,但是
我有两个列表列表,子列表代表路径。我想找到所有路径。 List> pathList1 List> pathList2 当然是天真的解决方案: List> result = new ArrayList>
我需要使用 Regex 格式化一个字符串,该字符串包含数字、字母 a-z 和 A-Z,同时还包含破折号和空格。 从用户输入我有02-219 8 53 24 输出应该是022 198 53 24 我正在
目标是达到与this C++ example相同的效果: 避免创建临时文件。我曾尝试将 C++ 示例翻译为 D,但没有成功。我也尝试过不同的方法。 import std.datetime : benc
tl;dr:你好吗perfect forwarding在 D? 该链接有一个很好的解释,但例如,假设我有这个方法: void foo(T)(in int a, out int b, ref int c
有什么方法可以在 D 中使用abstract auto 函数吗? 如果我声明一个类如下: class MyClass { abstract auto foo(); } 我收到以下错误: mai
有没有人为内存中重叠的数组切片实现交集?算法在没有重叠时返回 []。 当 pretty-print (使用重叠缩进)内存中重叠的数组切片时,我想要这个。 最佳答案 如果您确定它们是数组,那么只需取 p
我已经开始学习 D,但我在使用 Andrei Alexandrescu 所著的 The D Programming Language 一书中提供的示例时遇到了一些麻烦。由于 int 和 ulong 类
如何创建一个不可变的类? 我的目标是创建一个实例始终不可变的类。现在我只是用不可变的方法和构造函数创建了一个“可变”类。我将其称为 mData,m 表示可变。然后我创建一个别名 alias immut
不久前我买了《The D Programming Language》。好书,很有教育意义。但是,我在尝试编译书中列出的语言功能时遇到了麻烦:扩展函数。 在这本书中,Andrei 写了任何可以像这样调用
我在 D http://www.digitalmars.com/d/2.0/lazy-evaluation.html 中找到了函数参数的惰性求值示例 我想知道如何在 D 中实现可能的无限数据结构,就像
这个问题在这里已经有了答案: 12 年前关闭。 Possible Duplicate: Could anyone explain these undefined behaviors (i = i++
当前是否可以跨模块扫描/查询/迭代具有某些属性的所有函数(或类)? 例如: source/packageA/something.d: @sillyWalk(10) void doSomething()
我是一名优秀的程序员,十分优秀!