saru
saru

Reputation: 255

Can we use Q_PROPERTY with template<typename T>?

I want to display on my GUI using QML and C++ a multi-datatype value of type template < typename T>. Is it possible to use it with Q_PROPERTY or shall I use function overloading for each data type and pass it to Q_PROPERTY ?

For example:

Q_PROPERTY(int dataread READ dataRead NOTIFY dataChanged)
Q_PROPERTY(float dataread READ dataRead NOTIFY dataChanged)
int dataRead (int data) {return data; }
float dataRead (float data) {return data; }

Upvotes: 3

Views: 855

Answers (1)

jonspaceharper
jonspaceharper

Reputation: 4367

No, you can't use templates with properties, as moc doesnt support templating. This has been discussed (and is doable), but there hasn't been enough interest to implement it.

Each property also identifies a concrete type, so you can't overload it, either. You can overload signals and slots, so this is doable:

class foo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int intRead READ readInt NOTIFY dataChanged)
    Q_PROPERTY(float floatRead READ readFloat NOTIFY dataChanged)

public:
    int readInt() {return data; }
    float readFloat() {return data; }
signals:
    void dataChanged(int data);
    void dataChanged(float data);
...
}

Note that I removed the parameters from your read functions.

Upvotes: 2

Related Questions