Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Compile error when using Q_GADGET macro
Forum Update on Tuesday, May 27th 2025

Compile error when using Q_GADGET macro

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 4 Mar 2021, 11:57 last edited by
    #1

    Hi all,
    I opened a new Qt Quick Application, just to learn the behavior of Q_GADGET macro

    below is the code,

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include "base.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<Base>("Base",1,0,"Base");  // register here
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    base.h

    #ifndef BASE_H
    #define BASE_H
    
    #include <QObject>
    
    class Base
    {
        Q_GADGET
    
    public:
        Base();
    
    };
    Q_DECLARE_METATYPE(Base)
    
    
    #endif // BASE_H
    

    base.cpp

    #include "base.h"
    
    
    Base::Base()
    {
    
    }
    

    When building my application i am getting the error,
    D:\Qt\5.15.2\msvc2019\include\QtQml\qqmlprivate.h:142: error: C3668: 'QQmlPrivate::QQmlElement<T>::~QQmlElement': method with override specifier 'override' did not override any base class methods
    with
    [
    T=Base
    ]

    Compiler used: MSVC 2019 32 bit

    Can someone please help me, what is the issue.

    Thanks in advance

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 4 Mar 2021, 12:01 last edited by
      #2

      @Vinoth-Rajendran4 said in Compile error when using Q_GADGET macro:

      qmlRegisterType<Base>("Base",1,0,"Base"); // register here

      Q_GADGETs cannot be registered as QML types.

      (Z(:^

      V 1 Reply Last reply 4 Mar 2021, 12:54
      1
      • S sierdzio
        4 Mar 2021, 12:01

        @Vinoth-Rajendran4 said in Compile error when using Q_GADGET macro:

        qmlRegisterType<Base>("Base",1,0,"Base"); // register here

        Q_GADGETs cannot be registered as QML types.

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on 4 Mar 2021, 12:54 last edited by Vinoth Rajendran4 3 Apr 2021, 12:55
        #3

        @sierdzio : Thanks for the reply.

        In the documentation, it is mentioned Q_GADGET macro supports Q_PROPERTY, Q_ENUM. How do we expose them to QML ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 4 Mar 2021, 13:13 last edited by
          #4

          Yes, QML understands gadgets, can modify properties etc. But it cannot instantiate a gadget, so you cannot register it as type. It's a pretty annoying limitation... and not very well documented.

          So, to use a gadget in QML, make sure some object creates the gadget on C++ side, then returns it. Something like (pseudo code):

          class MyController : public QObject
          {
            Q_OBJECT
          
            Q_INVOKABLE MyGadget getGadget() const;
          };
          
          struct MyGadget
          {
            Q_GADGET
            Q_PROPERTY(QString title MEMBER title)
            QString title = "Hello world!";
          };
          
          // QML:
          console.log("Title is:", myController.getGadget().title)
          

          You can also use gadget in a property instead of Q_INVOKABLE.

          (Z(:^

          V 1 Reply Last reply 4 Mar 2021, 17:01
          7
          • S sierdzio
            4 Mar 2021, 13:13

            Yes, QML understands gadgets, can modify properties etc. But it cannot instantiate a gadget, so you cannot register it as type. It's a pretty annoying limitation... and not very well documented.

            So, to use a gadget in QML, make sure some object creates the gadget on C++ side, then returns it. Something like (pseudo code):

            class MyController : public QObject
            {
              Q_OBJECT
            
              Q_INVOKABLE MyGadget getGadget() const;
            };
            
            struct MyGadget
            {
              Q_GADGET
              Q_PROPERTY(QString title MEMBER title)
              QString title = "Hello world!";
            };
            
            // QML:
            console.log("Title is:", myController.getGadget().title)
            

            You can also use gadget in a property instead of Q_INVOKABLE.

            V Offline
            V Offline
            Vinoth Rajendran4
            wrote on 4 Mar 2021, 17:01 last edited by
            #5

            @sierdzio , Thanks for your clear explanation with code snippet. I got the point.

            1 Reply Last reply
            0

            1/5

            4 Mar 2021, 11:57

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved