[英]Confusion with Generics
I have 我有
class Foo : public QFrame {...}
and in the module when this class is used, I have 在使用此类的模块中,我有
QWidget* screen = this->parentWidget();
Foo* foo = (Foo*) screen->findChild<QFrame*>("foo1"); // foo1 is the name of the control from .ui file
and this works. 这可行。 If I change it to
如果我将其更改为
QWidget* screen = this->parentWidget();
Foo* foo = screen->findChild<Foo*>("foo1"); // foo1 is the name of the control from .ui file
I get this linker error 我收到此链接器错误
Error 4 error LNK2001: unresolved external symbol "public: static struct QMetaObject const Foo::staticMetaObject" (?staticMetaObject@Foo@@2UQMetaObject@@B) Foo.obj
错误4错误LNK2001:无法解析的外部符号“ public:静态结构QMetaObject const Foo :: staticMetaObject”(?staticMetaObject @ Foo @@ 2UQMetaObject @@ B)Foo.obj
What's the difference between the two snippets and why does the first one work and the second one doesn't? 这两个摘要之间有什么区别,为什么第一个摘要有效而第二个摘要无效?
This is probably because Q_DECLARE_METATYPE is missing on Foo class 这可能是因为Foo类缺少Q_DECLARE_METATYPE
From Qt Documenation 从Qt文档
Adding a Q_DECLARE_METATYPE() makes the type
known to all template based functions
Something like 就像是
class Foo : public QFrame
{
//everything
}
Q_DECLARE_METATYPE(Foo)
Your class Foo is probably missing the Q_OBJECT macro. 您的Foo类可能缺少Q_OBJECT宏。 Add it and, if you use QMake, add the header to the HEADERS list and rerun qmake.
添加它,如果使用QMake,则将头添加到HEADERS列表中,然后重新运行qmake。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.