【发布时间】:2017-07-31 13:37:46
【问题描述】:
我正在用 Qt 编写一个纸牌游戏。最初,UI 是用 Qt Widgets 用 C++ 编写的,但在移植到 Android 之后(Qt Widgets 应用程序看起来很尴尬,因为设置和颜色选择对话框几乎无法使用)我决定切换到 QML,保持游戏逻辑C++。
但是,与视觉渲染不能分离的部分游戏逻辑(例如玩家将牌放在桌子上)写在QGraphicsScene-派生类中:
class Table : public QGraphicsScene
{
// some code omitted
}
Table::Table(QObject* parent) : QGraphicsScene(parent) { /* ... */ }
我尝试通过qmlRegisterType() 将Table 注册为QML 类型,但这对我不起作用 - 将Table 对象放入QML 会导致QGraphicsScene 构造函数中的段错误。这是回溯的一部分:
#0 0x19312fa4 in std::__atomic_base<int>::load (__m=std::memory_order_relaxed, this=0xabababab) at C:/MINGW530/i686-w64-mingw32/include/c++/bits/atomic_base.h:396
__b = std::memory_order_relaxed
#1 QAtomicOps<int>::load<int> (_q_value=...) at ../../include/QtCore/../../src/corelib/arch/qatomic_cxx11.h:227
No locals.
#2 0x193e1780 in QBasicAtomicInteger<int>::load (this=0xabababab) at ../../include/QtCore/../../src/corelib/thread/qbasicatomic.h:102
No locals.
#3 0x1940f7a7 in QtPrivate::RefCount::isShared (this=0xabababab) at ../../include/QtCore/../../src/corelib/tools/qrefcount.h:101
count = 571316634
#4 0x1937f29d in QList<QGraphicsScene*>::append (this=0x139914c, t=@0x112e8cc: 0x362be1a8) at ../../include/QtCore/../../src/corelib/tools/qlist.h:580
No locals.
#5 0x192a1188 in QGraphicsScenePrivate::init (this=0x362be258) at graphicsview\qgraphicsscene.cpp:334
q = 0x362be1a8
#6 0x192a61e9 in QGraphicsScene::QGraphicsScene (this=0x362be1a8, parent=0x0) at graphicsview\qgraphicsscene.cpp:1636
No locals.
#7 0x00402f53 in Table::Table (this=0x362be1a8, parent=0x0) at ..\OpenFool\src\table.cpp:41
No locals.
#8 0x0041239a in QQmlPrivate::QQmlElement<Table>::QQmlElement (this=0x362be1a8) at J:/Qt/Qt5.8.0/5.8/mingw53_32/include/QtQml/qqmlprivate.h:99
No locals.
#9 0x0041235d in QQmlPrivate::createInto<Table> (memory=0x362be1a8) at J:/Qt/Qt5.8.0/5.8/mingw53_32/include/QtQml/qqmlprivate.h:108
No locals.
#10 0x01c7a5d5 in QQmlType::create (this=0x1398550, out=0x112ed70, memory=0x112ed4c, additionalMemory=72) at qml\qqmlmetatype.cpp:761
rv = 0x362be1a8
那么,有没有一种方法可以重用Table 的代码,而无需将其重新写入QML?
【问题讨论】: