我有一个由 QGraphicsView/QGraphicsScene 编写的游戏。现在我想将它嵌入到由 QQuickWindow/QQuickItem 编写的 QtQuick 应用程序中。我该怎么做?
原始游戏代码:
TileScene *scene = new TileScene(":/tileset.png", ":/test.tmx");
NPC *joe = new NPC(":/nerd.png");
scene->addCharacter(joe);
joe->setPos(15 * TILE_WIDTH, 23 * TILE_HEIGHT);
// Set up a view of our scene
view = new CharacterView(scene);
view.show();
QtQuick 应用程序:
MainContent::MainContent( QQuickItem* parent )
{
auto gridBox = new MainContentGridBox();
gridBox->addItem( new GameItem("Deck 01"), 0, 0);
...
}
//GameItem.h
class GameItem : public QQuickItem
{
Q_OBJECT
public:
GameItem(QString text, QQuickItem* parent = nullptr);
}
//GameItem.cpp
GameItem::GameItem(QString text, QQuickItem* parent)
{
TileScene *scene = new TileScene(":/tileset.png", ":/test.tmx");
NPC *joe = new NPC(":/nerd.png");
scene->addCharacter(joe);
joe->setPos(15 * TILE_WIDTH, 23 * TILE_HEIGHT);
// Set up a view of our scene
view = new CharacterView(scene);
}