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?
Forum Updated to NodeBB v4.3 + New Features

Automatic registration of custom types?

Scheduled Pinned Locked Moved General and Desktop
qmetatype
2 Posts 2 Posters 268 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.
  • C Offline
    C Offline
    CJha
    wrote on 22 Jul 2024, 07:33 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?

    P 1 Reply Last reply 22 Jul 2024, 12:23
    0
    • C CJha marked this topic as a regular topic on 22 Jul 2024, 07:33
    • C CJha
      22 Jul 2024, 07:33

      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?

      P Offline
      P Offline
      Pl45m4
      wrote on 22 Jul 2024, 12:23 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

      2/2

      22 Jul 2024, 12:23

      • Login

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