[Interest] Template-derived classes using MOC

Curtis Mitch mitch.curtis at theqtcompany.com
Sat Sep 5 09:31:47 CEST 2015


Is the following code a valid use of MOC? If not, why? I couldn't see anything mentioned in the old Qt Quarterly article [1] about this specific usage.

#include <QtCore>

class AbstractBase : public QObject
{
public:
    AbstractBase() {}

    virtual void virt() = 0;
};

template<typename T>
class Base : public AbstractBase
{
public:
    Base() {}

    virtual void virt() {}

private:
    QVector<T> mContainer;
};

class Derived : public Base<QString>
{
    Q_OBJECT
    Q_PROPERTY(int blah READ blah WRITE setBlah)

public:
    Derived() : mBlah(0) {}
    ~Derived() {}

    int blah() const { return mBlah; }
    void setBlah(int blah) { mBlah = blah; }

public slots:
    void foo() { qDebug() << Q_FUNC_INFO; }

signals:
    void sig();

private:
    int mBlah;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Derived d;
    QObject::connect(&d, SIGNAL(sig()), &d, SLOT(foo()));

    emit d.sig();

    qDebug() << d.property("blah");
    d.setProperty("blah", QVariant::fromValue(5));
    qDebug() << d.property("blah");

    return 0;
}

#include "main.moc"

[1] https://doc.qt.io/archives/qq/qq15-academic.html


More information about the Interest mailing list