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.
This commit is contained in:
ItsDrike 2024-12-06 03:40:22 +01:00
parent 84a8aa57d6
commit 4692ce356b
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
2 changed files with 34 additions and 16 deletions

View file

@ -1,5 +1,6 @@
#include "gamestate.h" #include "gamestate.h"
#include <QDebug> #include <QDebug>
#include <qqmllist.h>
#include <random> #include <random>
GameState::GameState(QObject* parent, bool preDealCards) : QObject{parent} { 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; qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId;
} }
QList<PlayingCard*> GameState::drawPile() const { QVariantList GameState::drawPile() const {
return m_drawPile; QVariantList lst;
for (auto& card : m_drawPile) {
lst.append(QVariant::fromValue(card));
}
return lst;
} }
QList<PlayingCard*> GameState::throwawayPile() const { QVariantList GameState::throwawayPile() const {
return m_throwawayPile; QVariantList lst;
for (auto& card : m_throwawayPile) {
lst.append(QVariant::fromValue(card));
}
return lst;
} }
QList<QList<ColumnSlot*> > GameState::columns() const { QVariantList GameState::columns() const {
return m_columns; QVariantList lst;
for (auto& columnStack : m_columns) {
lst.append(QVariant::fromValue(columnStack));
}
return lst;
} }
QList<QList<PlayingCard*> > GameState::foundation() const { QVariantList GameState::foundation() const {
return m_foundation; QVariantList lst;
for (auto& foundationStack : m_foundation) {
lst.append(QVariant::fromValue(foundationStack));
}
return lst;
} }
bool GameState::gameWon() const { bool GameState::gameWon() const {

View file

@ -5,25 +5,26 @@
#include "playingcard.h" #include "playingcard.h"
#include <QObject> #include <QObject>
#include <qqmlintegration.h> #include <qqmlintegration.h>
#include <qqmllist.h>
class GameState : public QObject { class GameState : public QObject {
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
QML_SINGLETON QML_SINGLETON
Q_PROPERTY(QList<PlayingCard*> drawPile READ drawPile NOTIFY drawPileChanged) Q_PROPERTY(QVariantList drawPile READ drawPile NOTIFY drawPileChanged)
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QVariantList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged) Q_PROPERTY(QVariantList columns READ columns NOTIFY columnsChanged)
Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged) Q_PROPERTY(QVariantList foundation READ foundation NOTIFY foundationChanged)
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
public: public:
explicit GameState(QObject* parent = nullptr); explicit GameState(QObject* parent = nullptr);
// Getters // Getters
QList<PlayingCard*> drawPile() const; QVariantList drawPile() const;
QList<PlayingCard*> throwawayPile() const; QVariantList throwawayPile() const;
QList<QList<ColumnSlot*>> columns() const; QVariantList columns() const;
QList<QList<PlayingCard*>> foundation() const; QVariantList foundation() const;
bool gameWon() const; bool gameWon() const;
// General functions // General functions