Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Direct initialization of a class inherited of QAbstractListModel does not let compile

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by
    #1

    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

    #ifndef SOURCEUNITMODEL_H
    #define SOURCEUNITMODEL_H
    
    #include <QObject>
    #include <QAbstractListModel>
    
    #include "DataTypes/unit.h"
    
    class SourceUnitModel : public QAbstractListModel
    {
        Q_OBJECT
        //Q_PROPERTY(int count READ count NOTIFY countChanged)
    public:
        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);
        ~SourceUnitModel();
    
        //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_);
    
    signals:
        void countChanged(int arg);
    
    protected: // interface QAbstractListModel
        virtual QHash<int, QByteArray> roleNames() const;
    private:
        //QList<QColor> m_data;
        QHash<int, QByteArray> m_roleNames;
        //int m_count;
    
        QHash<Unit::Category, QList<Unit *> > modelsByCategory;
        Unit::Category category;
    };
    #endif // SOURCEUNITMODEL_H
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        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)

        K Offline
        K Offline
        Kofr
        wrote on last edited by
        #3

        @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_OBJECT
            Q_DISABLE_COPY(Converter)
            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)
        public:
            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://prperties
        
        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
        signals:
            void unitsChanged();
            void categoryModelChanged();
            void sourceUnitModelChanged();
        };
        
        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          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.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved