QT:模板化 Q_OBJECT 类

QT : Templated Q_OBJECT class(QT:模板化 Q_OBJECT 类)
本文介绍了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
{...

这篇关于QT:模板化 Q_OBJECT 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Unresolved external symbol quot;public: virtual struct QMetaObject const * __thiscall Parent(未解析的外部符号“public: virtual struct QMetaObject const * __thiscall Parent)
QVector vs QList(QVector 与 QList)
How to create/read/write JSON files in Qt5(如何在 Qt5 中创建/读取/写入 JSON 文件)
Qt: How do I handle the event of the user pressing the #39;X#39; (close) button?(Qt:如何处理用户按下“X(关闭)按钮的事件?)
STL or Qt containers?(STL 还是 Qt 容器?)
Sort filenames naturally with Qt(使用 Qt 自然地对文件名进行排序)