QT:模板化 Q_OBJECT 类
是否有可能有一个从 QObject 继承的模板类(并且在它的声明中有 Q_OBJECT 宏)?
Is it possible to have a template class, which inherit from QObject (and has Q_OBJECT macro in it's declaration)?
我想为插槽创建类似适配器的东西,它可以做一些事情,但插槽可以接受任意数量的参数(参数数量取决于模板参数).
I would like to create something like adapter for slots, which would do something, but the slot can take arbitrary number of arguments (number of arguments depends on the template argument).
我刚刚尝试这样做,但出现链接器错误.我猜 gmake 或 moc 没有在这个模板类上被调用.有没有办法做到这一点?也许通过显式实例化模板?
I just tried doing it, and got linker errors. I guess gmake or moc is not getting called on this template class. Is there a way to do this? Maybe by explicitly instantiating templates?
推荐答案
混合模板和 Q_OBJECT 是不可能的,但如果你有一个类型的子集,你可以像这样列出插槽和信号:
It is not possible to mix template and Q_OBJECT but if you have a subset of types you can list the slots and signals like this:
class SignalsSlots : public QObject
{
Q_OBJECT
public:
explicit SignalsSlots(QObject *parent = 0) :
QObject(parent) {}
public slots:
virtual void writeAsync(int value) {}
virtual void writeAsync(float value) {}
virtual void writeAsync(double value) {}
virtual void writeAsync(bool state) {}
virtual void writeAsync(svga::SSlideSwitch::SwitchState state) {}
signals:
void readAsynkPolledChanged(int value);
void readAsynkPolledChanged(float value);
void readAsynkPolledChanged(double value);
void readAsynkPolledChanged(bool state);
void readAsynkPolledChanged(svga::SSlideSwitch::SwitchState state);
};
...
template <class T>
class Abstraction : public SignalsSlots
{...
相关文章