QMetaType 和继承
好的,所以我对 Qt 和 C++ 都是新手。我正在尝试将 QMetaType 与我自己的类一起使用,但我不能让它与子类一起工作。这就是我所拥有的(可能有很多问题,抱歉):
testparent.h:
#include <QMetaType>
class TestParent
{
public:
TestParent();
~TestParent();
TestParent(const TestParent &t);
virtual int getSomething(); // in testparent.cpp, just one line returning 42
int getAnotherThing(); // in testparent.cpp, just one line returning 99
};
Q_DECLARE_METATYPE(TestParent)
这是 test1.h:
#include <QMetaType>
#include "testparent.h"
class Test1 : public TestParent
{
public:
Test1();
~Test1();
Test1(const Test1 &t);
int getSomething(); // int test1.cpp, just one line returning 67
};
Q_DECLARE_METATYPE(Test1)
... (除非另有说明,此处声明的所有成员都定义为不执行任何操作(只是打开括号,关闭括号)在 testparent.cpp 或 test1.cpp 中) 这是 main.cpp:
#include <QtGui/QApplication>
#include "test1.h"
#include "testparent.h"
#include <QDebug>
int main(int argc, char *argv[])
{
int id = QMetaType::type("Test1");
TestParent *ptr = new Test1;
Test1 *ptr1 = (Test1*)(QMetaType::construct(id));
// TestParent *ptr2 = (TestParent*)(QMetaType::construct(id));
qDebug() << ptr->getSomething();
qDebug() << ptr1->getSomething(); // program fails here
// qDebug() << ptr2->getAnotherThing();
// qDebug() << ptr2->getSomething();
delete ptr;
delete ptr1;
// delete ptr2;
return 0;
}
正如你所看到的,我试图用 ptr2 测试一些多态性的东西,但后来我意识到 ptr1 甚至不起作用。 (编辑:上一句没有意义。哦,好吧,问题解决了(编辑:nvm 它确实有意义))当我运行这个时会发生什么,如预期的那样,第一个 qDebug 打印 67,然后它卡住了几秒钟,然后最终退出,代码为-1073741819。
非常感谢。
OK, so I'm new to both Qt and C++ for that matter. I'm trying to use QMetaType with my own classes, and I can't get it to work with subclasses. Here's what I have (there're probably tons of problems, sorry):
testparent.h:
#include <QMetaType>
class TestParent
{
public:
TestParent();
~TestParent();
TestParent(const TestParent &t);
virtual int getSomething(); // in testparent.cpp, just one line returning 42
int getAnotherThing(); // in testparent.cpp, just one line returning 99
};
Q_DECLARE_METATYPE(TestParent)
And here's test1.h:
#include <QMetaType>
#include "testparent.h"
class Test1 : public TestParent
{
public:
Test1();
~Test1();
Test1(const Test1 &t);
int getSomething(); // int test1.cpp, just one line returning 67
};
Q_DECLARE_METATYPE(Test1)
... (Unless otherwise indicated, all the members declared here are defined to do nothing (just open bracket, close bracket) in testparent.cpp or test1.cpp) Here's main.cpp:
#include <QtGui/QApplication>
#include "test1.h"
#include "testparent.h"
#include <QDebug>
int main(int argc, char *argv[])
{
int id = QMetaType::type("Test1");
TestParent *ptr = new Test1;
Test1 *ptr1 = (Test1*)(QMetaType::construct(id));
// TestParent *ptr2 = (TestParent*)(QMetaType::construct(id));
qDebug() << ptr->getSomething();
qDebug() << ptr1->getSomething(); // program fails here
// qDebug() << ptr2->getAnotherThing();
// qDebug() << ptr2->getSomething();
delete ptr;
delete ptr1;
// delete ptr2;
return 0;
}
As you can see I was trying to test out some polymorphism stuff with ptr2, but then I realized ptr1 doesn't even work. (EDIT: prev sentence makes no sense. Oh well, problem resolved (EDIT: nvm it does make sense)) What happens when I run this is the first qDebug prints 67, as expected, and then it gets stuck for a few seconds and eventually exits with code -1073741819.
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型必须注册!宏
Q_DECLARE_METATYPE
还不够。您在 main 函数开始处缺少一行:
现在您可以获得不为零的 id (这意味着该类型已注册):
Type has to be registered! Macro
Q_DECLARE_METATYPE
is not sufficient.You are missing one line at start of main function:
now you can get
id
that is not zero (which means that type is registered):