是否可以将模板派生的C ++类与Qt的Q_OBJECT混合?

 2020-02-15 

Is it possible to mix template-derived C++ classes with Qt's Q_OBJECT?

在我的应用程序中,我具有以下类层次结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Word
{
    ...
}

template <typename T> class Dictionary
{
    ...
};

class WordDictionary : public Dictionary<Word>
{
    Q_OBJECT

    ...
}

WordDictionary类分析需要很长时间的字典。我正在一个单独的线程中运行解析函数,并且我希望它能够不时地向GUI线程发送信号,以基于正在解析的当前行号提供进度更新。这就是为什么我希望它成为Q_OBJECT。我试图将基类Dictionary作为Q_OBJECT,但收到一条消息,提示不支持Q_OBJECT模板。当我删除宏时,仅将WordDictionary保留为Q_OBJECT,我收到了一堆一般形式的错误消息:

.\GeneratedFiles
elease\moc_dictionary.cpp(44) : error C2039: 'staticMetaObject' : is not a member of 'Dictionary'
with
[
T=Word
]

除了对其中的模板函数进行硬编码以生成大量样板代码之外,我还能做些什么使我的模板派生的WordDictionary类成为Q_OBJECT?

编辑:将模板声明更改为:

1
template <typename T> class Dictionary : public QObject

使代码编译。我不确定我是否没有做一些愚蠢的事情,但是是否可以正常工作。


您不能直接执行此操作,但是可以进行一些工作。 请参阅此处的文章。

While it is theoretically possible for
moc to handle templates, it would be
extremely complex to implement, and
would be highly impractical to use:
For each template instantiation, moc
would have to generate the appropriate
meta-object code, and the generated
code would have to be included once
per link unit---which becomes a
nightmare to maintain once a template
class is used with the same template
parameter in different compilation
units.

If the signals and slots don't require
the template parameter to be part of
the prototype, the workaround is to
make a template class inherit a
QObject subclass that provides the
required signals and slots. If
signals and slots need to use the
template parameters, the Observer
pattern is an alternative.


我刚刚尝试了这段代码,它可以编译并正常运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <QtCore/QCoreApplication>
#include <QObject>

class Word
{

};

template <typename T> class Dictionary
{

};

class WordDictionary : public Dictionary<Word>, QObject
{
    Q_OBJECT
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    WordDictionary wd();
    return a.exec();
}

可能是我缺少了什么吗?