Important: Please read the Qt Code of Conduct -

Direct initialization of a class inherited of QAbstractListModel does not let compile

  • here is the code.
    the compiler says error expected identifier before numeric constant and expected ',' or '...' before numeric constant

    on this

    SourceUnitModel m_sourceUnitModel(0, modelsByCategory);

    what wrong with this direct initialization?
    header below

    #include <QObject>
    #include <QAbstractListModel>
    #include "DataTypes/unit.h"
    class SourceUnitModel : public QAbstractListModel
        //Q_PROPERTY(int count READ count NOTIFY countChanged)
        enum RoleNames {
            NameRole = Qt::UserRole,
            CategoryRole = Qt::UserRole+2,
            CategoryNameRole = Qt::UserRole+3,
            CoefficientRole = Qt::UserRole+4
        SourceUnitModel(QObject *parent = 0, const QHash<Unit::Category, QList<Unit *> > &modelsByCategory_ = QHash<Unit::Category , QList<Unit *> >());
        SourceUnitModel(QObject *parent = 0);
        //Q_INVOKABLE void insert(int index, const QString& colorValue);
        //Q_INVOKABLE void append(const QString& colorValue);
        //Q_INVOKABLE void remove(int index);
        //Q_INVOKABLE void clear();
        //Q_INVOKABLE QColor get(int index);
    public: // interface QAbstractListModel
        virtual int rowCount(const QModelIndex &parent) const;
        virtual QVariant data(const QModelIndex &index, int role) const;
        //int count() const;
        void updateCategory(Unit::Category category_);
        void countChanged(int arg);
    protected: // interface QAbstractListModel
        virtual QHash<int, QByteArray> roleNames() const;
        //QList<QColor> m_data;
        QHash<int, QByteArray> m_roleNames;
        //int m_count;
        QHash<Unit::Category, QList<Unit *> > modelsByCategory;
        Unit::Category category;

  • Lifetime Qt Champion


    What does modelsByCategory contains ?

    On a side note that when subclassing a QWidget, the parent parameter is usually the last one (see Qt's widgets subclasses)

  • @SGaist
    QHash<Unit::Category, QList<Unit *> > modelsByCategory;
    enum as key and Unit is QObject derived class.

    the header where I initialize it in.

    #include <QQuickItem>
    #include "DataTypes/numberofunits.h"
    #include "DataModels/sourceunitmodel.h"
    class Converter : public QQuickItem
        Q_CLASSINFO("DefaultProperty", "units")
        Q_PROPERTY(QQmlListProperty<Unit> units READ units NOTIFY unitsChanged)
        Q_PROPERTY(QList<QObject *> categoryModel READ categoryModel NOTIFY categoryModelChanged)
        Q_PROPERTY(SourceUnitModel sourceUnitModel READ sourceUnitModel /*WRITE setConvertedUnitModel*/ NOTIFY sourceUnitModelChanged)
        Converter(QQuickItem *parent = 0);
    public:// properties
        QQmlListProperty<Unit> units();
        int unitCount() const;
        Unit *unit(int index) const;
        QList<QObject *> categoryModel();
    public://source and converted
    private: //models
        QHash<Unit::Category, QList<Unit *> > modelsByCategory;
        QList<QObject *> CategoryDataModel;
        QList<QObject *> sourceUnitDataModel;
        SourceUnitModel m_sourceUnitModel(0, modelsByCategory);
    private://source and converted data
        ValAndUnit source;
        ValAndUnit converted;
    private: //service
        bool initialized = false;
        QList<Unit*> m_units;
    public slots:
        void initialize();//set up data models from object config
        void unitsChanged();
        void categoryModelChanged();
        void sourceUnitModelChanged();

  • Moderators

    You are trying to initialize a non-static class member in the header. This is only possible if you compile in c++11 mode and the syntax you used is wrong.

    It can be either explicit intialization (aka "the old style")

    SourceUnitModel m_sourceUnitModel = SourceUnitModel(0, modelsByCategory);

    or with the uniform initialization syntax:

    SourceUnitModel m_sourceUnitModel = { 0, modelsByCategory };

    Note though that what you do is quite risky, as you're initializing one member with a reference to another. Since you do that in a header it depends on the order of members. There's no problem now but there might be if someone someday shuffles the declaration. It would be safer to do the initialization in the body of the constructor, where both members are already constructed.

Log in to reply