Automatic registration of custom types?
-
wrote on 22 Jul 2024, 07:33 last edited by 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
orqRegisterMetaType
. 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? -
-
Hi, I am using Qt 6.5 and C++20. I am getting my own type automatically registered without using either
Q_DECLARE_METATYPE
orqRegisterMetaType
. 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?
2/2