Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Automatic registration of custom types?
Qt 6.11 is out! See what's new in the release blog

Automatic registration of custom types?

Scheduled Pinned Locked Moved General and Desktop
qmetatype
2 Posts 2 Posters 703 Views 2 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by CJha
    #1

    Hi, I am using Qt 6.5 and C++20. I am getting my own type automatically registered without using either Q_DECLARE_METATYPE or qRegisterMetaType. Here is the full code:

    MainWindow.h

    #include <QWidget>
    #include <QDebug>
    #include <memory>
    #include "ui_MainWindow.h"
    
    struct Type
    {
      int value;
      std::unique_ptr<QString> ptr = std::make_unique<QString>("One"); // just to make Type struct a bit more complex
    };
    
    class MainWindow : public QWidget
    {
      Q_OBJECT
    
    public:
      MainWindow(QWidget* parent = nullptr);
      ~MainWindow();
    
    private:
      Ui::MainWindowClass ui;
    };
    

    MainWindow.cpp

    #include "MainWindow.h"
    
    MainWindow::MainWindow(QWidget* parent)
      : QWidget(parent)
    {
      ui.setupUi(this);
    
      Type type;
      int id = qMetaTypeId<Type>();
      qDebug() << "ID =" << id << QMetaType::isRegistered(id) << *type.ptr;
    }
    
    MainWindow::~MainWindow()
    {}
    

    I was expecting to get a compilation error as explained in the qMetaTypeId() documentation: Returns the meta type id of type T at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail. But there is no error when I run the project and I am getting the output as: ID = 65537 true "One" . What is happening?

    Pl45m4P 1 Reply Last reply
    0
    • CJhaC CJha marked this topic as a regular topic on
    • CJhaC CJha

      Hi, I am using Qt 6.5 and C++20. I am getting my own type automatically registered without using either Q_DECLARE_METATYPE or qRegisterMetaType. Here is the full code:

      MainWindow.h

      #include <QWidget>
      #include <QDebug>
      #include <memory>
      #include "ui_MainWindow.h"
      
      struct Type
      {
        int value;
        std::unique_ptr<QString> ptr = std::make_unique<QString>("One"); // just to make Type struct a bit more complex
      };
      
      class MainWindow : public QWidget
      {
        Q_OBJECT
      
      public:
        MainWindow(QWidget* parent = nullptr);
        ~MainWindow();
      
      private:
        Ui::MainWindowClass ui;
      };
      

      MainWindow.cpp

      #include "MainWindow.h"
      
      MainWindow::MainWindow(QWidget* parent)
        : QWidget(parent)
      {
        ui.setupUi(this);
      
        Type type;
        int id = qMetaTypeId<Type>();
        qDebug() << "ID =" << id << QMetaType::isRegistered(id) << *type.ptr;
      }
      
      MainWindow::~MainWindow()
      {}
      

      I was expecting to get a compilation error as explained in the qMetaTypeId() documentation: Returns the meta type id of type T at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail. But there is no error when I run the project and I am getting the output as: ID = 65537 true "One" . What is happening?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @CJha

      Look here.

      :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1

      • Login

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