From 4692ce356b992d78172845c05cf2e84953da3c3f Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Fri, 6 Dec 2024 03:40:22 +0100 Subject: [PATCH] Return QVariantList objects instead of QList QML doesn't have a proper type-safe generic list type, returning QList instances does technically work, however, qmlls (LSP) complains about using this as it isn't a proper QML type. Instead, return QVarianList objects, that are meant for QML. --- src/gamestate.cpp | 33 +++++++++++++++++++++++++-------- src/gamestate.h | 17 +++++++++-------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 8a30c1f..ccb7084 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,5 +1,6 @@ #include "gamestate.h" #include +#include #include GameState::GameState(QObject* parent, bool preDealCards) : QObject{parent} { @@ -525,20 +526,36 @@ void GameState::ensureColumnRevealed(int columnId) { qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId; } -QList GameState::drawPile() const { - return m_drawPile; +QVariantList GameState::drawPile() const { + QVariantList lst; + for (auto& card : m_drawPile) { + lst.append(QVariant::fromValue(card)); + } + return lst; } -QList GameState::throwawayPile() const { - return m_throwawayPile; +QVariantList GameState::throwawayPile() const { + QVariantList lst; + for (auto& card : m_throwawayPile) { + lst.append(QVariant::fromValue(card)); + } + return lst; } -QList > GameState::columns() const { - return m_columns; +QVariantList GameState::columns() const { + QVariantList lst; + for (auto& columnStack : m_columns) { + lst.append(QVariant::fromValue(columnStack)); + } + return lst; } -QList > GameState::foundation() const { - return m_foundation; +QVariantList GameState::foundation() const { + QVariantList lst; + for (auto& foundationStack : m_foundation) { + lst.append(QVariant::fromValue(foundationStack)); + } + return lst; } bool GameState::gameWon() const { diff --git a/src/gamestate.h b/src/gamestate.h index 8bd3bcd..cbeaae7 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -5,25 +5,26 @@ #include "playingcard.h" #include #include +#include class GameState : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON - Q_PROPERTY(QList drawPile READ drawPile NOTIFY drawPileChanged) - Q_PROPERTY(QList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) - Q_PROPERTY(QList> columns READ columns NOTIFY columnsChanged) - Q_PROPERTY(QList> foundation READ foundation NOTIFY foundationChanged) + Q_PROPERTY(QVariantList drawPile READ drawPile NOTIFY drawPileChanged) + Q_PROPERTY(QVariantList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) + Q_PROPERTY(QVariantList columns READ columns NOTIFY columnsChanged) + Q_PROPERTY(QVariantList foundation READ foundation NOTIFY foundationChanged) Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) public: explicit GameState(QObject* parent = nullptr); // Getters - QList drawPile() const; - QList throwawayPile() const; - QList> columns() const; - QList> foundation() const; + QVariantList drawPile() const; + QVariantList throwawayPile() const; + QVariantList columns() const; + QVariantList foundation() const; bool gameWon() const; // General functions