如何在QObject派生的类上正确使用qRegisterMetaType? [英] How to properly use qRegisterMetaType on a class derived from QObject?

查看:196
本文介绍了如何在QObject派生的类上正确使用qRegisterMetaType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,但无济于事。我的感叹是:

I've been searching far and wide for an answer to this but to no avail. My lament is as follows:

我有一个 ClassA ,看起来像这样:

I have a ClassA that roughly looks like this:

class ClassA : public QObject {
    Q_OBJECT
public:
    ClassA() { mName = "lol"; }
    ~ClassA();
    void ShowName() { std::cout << mName << std::endl; }
    std::string mName;
};

当然,由于我使用了moc,因此该类实际上在我的项目中分为cpp和hpp,但是

Of course, since I use moc, this class is actually split into cpp and hpp in my project but that part is not the issue here.

请注意,我没有故意使用 Q_DECLARE_METATYPE ,因为我不会现在实际上确实需要它的功能(QVariant扩展)。我只关心运行时实例化。

Please note that I do not use Q_DECLARE_METATYPE on purpose because I don't actually need its features (QVariant expansion) right now. I only care about runtime instantiation.

这里的问题是 Q_OBJECT 禁止复制和赋值构造函数。因此,我必须将 qRegisterMetaType 应用于 ClassA 本身,而不是 ClassA * 乍看起来似乎还不错。

The issue here is that Q_OBJECT forbids the copy and assignment constructors. Due to that, I have to apply qRegisterMetaType not to ClassA itself but to ClassA* which seems to work fine at first glance.

现在,我想在运行时从字符串动态创建此类并运行方法 ShowName()。我这样做是这样的:

Now, I want to create this class dynamically at runtime from a string and run the method ShowName(). I'm doing that like this:

int main() {
    qRegisterMetaType<ClassA*>("ClassA*");

    int id = QMetaType::type("ClassA*");
    std::cout << "meta id: " << id << std::endl; // Outputs correct generated user id (not 0)

    ClassA* myclass = static_cast<ClassA*>(QMetaType::construct(id));
    myclass->ShowName(); // Segfaults, oh dear

    return 0;
}

现在,这是我的问题。我似乎实际上没有在那里正确构造的对象。

Now, there is my issue. I don't seem to actually have a correctly constructed object there.

如果我们将类更改为以下样子:

If we change the class to look like this:

class ClassA : public QObject {
    Q_OBJECT
public:
    ClassA() { mName = "lol"; }
    ClassA(const ClassA& other) { assert(false && "DONT EVER USE THIS"); }
    ~ClassA();
    void ShowName() { std::cout << mName << std::endl; }
    std::string mName;
};

然后我们可以相应地将程序更改为:

then we can change our program accordingly to:

int main() {
    qRegisterMetaType<ClassA>("ClassA");

    int id = QMetaType::type("ClassA");
    std::cout << "meta id: " << id << std::endl; // Outputs correct generated user id (not 0)

    ClassA* myclass = static_cast<ClassA*>(QMetaType::construct(id));
    myclass->ShowName(); // "lol", yay

    return 0;
}

很明显,我只能使用伪造的覆盖副本构造函数,但感觉不对是正确的,而Qt建议反对,而是建议仅使用指向QObjects的指针。

Obviously I could just use my fake overwritten copy constructor but it doesn't feel right and Qt suggests against that and instead suggests the use of pointers to QObjects only.

有人在这里看到什么问题吗?另外,我知道在SO上也有类似的问题,但没有一个能解决这个确切的问题。

Does anyone see what's wrong here? Also, I am aware there are similar questions on SO but none of them tackle this exact problem.

推荐答案

一些事情:


  • 注册ClassA *无效的原因是因为您对Construct()的调用正在构造指向ClassA对象的指针

  • The reason that registering ClassA* isn't working is because your call to construct() is constructing a pointer to a ClassA object, but not an actual object.

值得注意的是QMetaType文档中的以下引用:

It is worthy of noting the following quote from the QMetaType documentation:


可以注册具有公共默认构造函数,公共
复制构造函数和公共析构函数的任何类或结构。

Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered.




  • 看看Qt对qMetaTypeConstructHelper的实现:

    • Take a look at Qt's implementation of qMetaTypeConstructHelper:

      template <typename T>
      void *qMetaTypeConstructHelper(const T *t)
      {
          if (!t)
              return new T();
          return new T(*static_cast<const T*>(t));
      }
      


    • 并注意其用法复制构造函数。在这种情况下,您有两种解决方法:

      and note their usage of the copy constructor. This being the case, you have two ways around the problem:

      1)提供一个副本构造函数(您已完成)

      1) Provide a copy constructor (which you have done)

      2)提供不使用复制构造函数的qMetaTypeConstructHelper的特殊化:

      2) Provide a specialization of qMetaTypeConstructHelper that doesn't use the copy constructor:

      template <>
      void *qMetaTypeConstructHelper<ClassA>(const ClassA *)
      {
          return new ClassA();
      }
      

      这篇关于如何在QObject派生的类上正确使用qRegisterMetaType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆