問題描述
是否有可能有一個從 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
{...
這篇關于QT:模板化 Q_OBJECT 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!