3

So I'd like to make a template class in C++ Qt. The problem is that I'm using signals/slots in this class.

My class header looks like

template <class T>
class Container : public QObject
{
    Q_OBJECT

protected:
    QVector<T*> vec;

public:
    Container();
    ~Container(void);

    int getSize() const;

    T * operator[] (int);
    T * operator() (int);
    void operator <<(T*);
    void operator >>(T*);

    bool exists(T*);

signals:
    void updateTable();
    void updateTable(int,int);
    void sizeSignal(int);

public slots:
    void insert(T*);
    void insert(int, T*);

    void edit(QTableWidgetItem*);

    void remove(int);
};

How to make it work?

Tried something like this:

template <class T>
class Container : public ContainerSignalsSlots
{

public:
    Container();
    ~Container(void);

    int getSize() const;

    T * operator[] (int);
    T * operator() (int);
    void operator <<(T*);
    void operator >>(T*);

    bool exists(T*);
};

class ContainerSignalsSlots : public QObject
{
    Q_OBJECT

protected:
    QVector<T*> vec; 

signals:
    void updateTable();
    void updateTable(int,int);
    void sizeSignal(int);

public slots:
    void insert(Class1*);
    void insert(int, Class1*);
    void insert(Class2*);
    void insert(int, Class2*);

    void edit(QTableWidgetItem*);

    void remove(int);
};

But how I'm supposed to handle with QVector?

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
Yonji
  • 31
  • 1
  • 3
  • 1
    You can't easily make `QObject` a template class. However, you might employ [this](http://stackoverflow.com/a/19138963/1217285) little trick to work around that limitation. – Dmitry Jan 23 '17 at 15:33
  • @Dmitry Seen that, but what to do with QVector then? I'm supposed to declare separate QVector for every class type? ContainerSignalsSlots & Container both needs access to QVector. – Yonji Jan 23 '17 at 16:04
  • You could, for example, make slots pure virtual in `ContainerSignalsSlots` and implement them in `Container`. This way you'd only need to access `QVector` in `Container`. – Dmitry Jan 23 '17 at 18:57
  • The answer is obvious, use an alternative signal/slot implementation, not Qts. – user1095108 Jun 07 '17 at 08:40
  • See my [post](https://codereview.stackexchange.com/questions/268217/handle-c-templates-in-qt5). May help. – LRDPRDX Sep 21 '21 at 13:01

0 Answers0