From 883d766dc3c28f0103d1232ad2d0c9c809a86411 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Tue, 3 Dec 2024 15:05:22 +0100 Subject: [PATCH] Register QObject classes as QML elements via macros Using manual registration with qmlRegisterType (or qmlRegisterUncreatableType) isn't ideal, because Qt Creator can't pick up on it. When using the macros, autocompletion & unknown type warnings in the code disappear. --- columnslot.h | 3 +++ gamestate.h | 3 +++ main.cpp | 3 --- playingcard.h | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/columnslot.h b/columnslot.h index 3906c26..2079e8f 100644 --- a/columnslot.h +++ b/columnslot.h @@ -2,11 +2,14 @@ #define COLUMNSLOT_H #include +#include #include "playingcard.h" class ColumnSlot : public QObject { Q_OBJECT + QML_ELEMENT + QML_UNCREATABLE("Use C++ logic to instantiate") Q_PROPERTY(PlayingCard* card READ card CONSTANT) Q_PROPERTY(bool revealed READ isRevealed NOTIFY revealedChanged) diff --git a/gamestate.h b/gamestate.h index 689d955..8465373 100644 --- a/gamestate.h +++ b/gamestate.h @@ -2,12 +2,15 @@ #define GAMESTATE_H #include +#include #include "playingcard.h" #include "columnslot.h" class GameState : public QObject { Q_OBJECT + QML_ELEMENT + QML_UNCREATABLE("Use C++ logic to instantiate") Q_PROPERTY(QList drawPile READ drawPile NOTIFY drawPileChanged) Q_PROPERTY(QList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QList> columns READ columns NOTIFY columnsChanged) diff --git a/main.cpp b/main.cpp index 2b3465a..1867309 100644 --- a/main.cpp +++ b/main.cpp @@ -15,9 +15,6 @@ int main(int argc, char *argv[]) []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); - qmlRegisterUncreatableType("Solitare", 1, 0, "PlayingCard", "PlayingCard cannot be directly created in QML. Use C++ logic to instantiate."); - qmlRegisterUncreatableType("Solitare", 1, 0, "ColumnSlot", "ColumnSlot cannot be directly created in QML. Use C++ logic to instantiate."); - GameState gameState; gameState.dealCards(); gameState.drawNextCard(); diff --git a/playingcard.h b/playingcard.h index e103a6c..fc69643 100644 --- a/playingcard.h +++ b/playingcard.h @@ -2,10 +2,13 @@ #define PLAYINGCARD_H #include +#include class PlayingCard : public QObject { Q_OBJECT + QML_ELEMENT + QML_UNCREATABLE("Use C++ logic to instantiate") Q_ENUMS(Value) Q_ENUMS(Suit)