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. Submodel into model

Submodel into model

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.7k Views
  • 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.
  • RomanoFXR Offline
    RomanoFXR Offline
    RomanoFX
    wrote on last edited by
    #1

    Hi,

    In my application, I have a simple model (to share variable to QML)

    Header :

    class ScreenModel : public QObject
    {
        Q_OBJECT
        Q_PROPERTY( int positionLogique READ getPositionLogique WRITE setPositionLogique NOTIFY positionLogiqueChanged )
        Q_PROPERTY( int couleurFondLCD READ getCouleurFondLCD WRITE setCouleurFondLCD NOTIFY couleurFondLCDChanged)
        Q_PROPERTY( int couleurReticule READ getCouleurReticule WRITE setCouleurReticule NOTIFY couleurReticuleChanged)
        Q_PROPERTY( int nombreEcran READ getNombreEcran WRITE setNombreEcran NOTIFY nombreEcranChanged)
    public:
        explicit ScreenModel(QObject *parent = 0);
    
        void setPositionLogique(QVariant PositionLogique);
        void setCouleurFondLCD(QVariant CouleurFondLCD);
        void setCouleurReticule(QVariant CouleurReticule);
        void setNombreEcran(QVariant NombreEcran);
    
        inline int getPositionLogique() const { return m_PositionLogique; }
        inline int getCouleurFondLCD() const { return m_CouleurFondLCD; }
        inline int getCouleurReticule() const { return m_CouleurReticule; }
        inline int getNombreEcran() const { return m_NombreEcran; }
    
    signals:
    
        void positionLogiqueChanged();
        void couleurFondLCDChanged();
        void couleurReticuleChanged();
        void nombreEcranChanged();
    
    public slots:
    
    private:
        int m_PositionLogique;
        int m_CouleurFondLCD;
        int m_CouleurReticule;
        int m_NombreEcran;
    };
    

    I would insert a model herited from QAbstractListModel into that one, and keeping the notification behavior of that last one.

    This is the model I would insert:

    #ifndef CUSTOMSCREENMODEL_H
    #define CUSTOMSCREENMODEL_H
    
    #include <QObject>
    #include <QAbstractListModel>
    
    
    class CustomScreenItem
    {
    
    public:
        /*
        \fn Constructor
        */
        CustomScreenItem(const int &ChannelIndex, const QString &Channel, const int &PositionReticule);
    
        /*
        \fn Setters
        */
        void setChannelIndex(QVariant ChannelIndex);
        void setChannel(QVariant Channel);
        void setPositionReticule(QVariant PositionReticule);
    
        /*
        \fn Getters
        */
        inline int ChannelIndex() const { return m_ChannelIndex; }
        inline QString Channel() const { return m_Channel; }
        inline int PositionReticule() const { return m_PositionReticule; }
    
    private:
        int m_ChannelIndex;
        QString m_Channel;
        int m_PositionReticule;
    };
    
    class CustomScreenModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        enum CustomScreenRoles {
            ChannelIndexRole = Qt::UserRole+1,
            ChannelRole,
            PositionReticuleRole
        };
        CustomScreenModel(QObject *parent = 0);
        void addTriggerComplexItem(const CustomScreenItem &customScreenItem);
        Q_INVOKABLE void append(const int &ChannelIndex, const QString &Channel, const int &PositionReticule);
        Q_INVOKABLE int rowCount(const QModelIndex & parent = QModelIndex()) const;
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        Qt::ItemFlags flags(const QModelIndex &index) const;
        Q_INVOKABLE bool removeRow(const int index);
        Q_INVOKABLE QVariantMap get(int idx) const;
    signals:
        void dataChanged();
    protected:
        QHash<int, QByteArray> roleNames() const;
    private:
        QList<CustomScreenItem> m_customScreenItem;
    };
    #endif // CUSTOMSCREENMODEL_H
    
    

    Thank you

    raven-worxR 1 Reply Last reply
    0
    • RomanoFXR RomanoFX

      Hi,

      In my application, I have a simple model (to share variable to QML)

      Header :

      class ScreenModel : public QObject
      {
          Q_OBJECT
          Q_PROPERTY( int positionLogique READ getPositionLogique WRITE setPositionLogique NOTIFY positionLogiqueChanged )
          Q_PROPERTY( int couleurFondLCD READ getCouleurFondLCD WRITE setCouleurFondLCD NOTIFY couleurFondLCDChanged)
          Q_PROPERTY( int couleurReticule READ getCouleurReticule WRITE setCouleurReticule NOTIFY couleurReticuleChanged)
          Q_PROPERTY( int nombreEcran READ getNombreEcran WRITE setNombreEcran NOTIFY nombreEcranChanged)
      public:
          explicit ScreenModel(QObject *parent = 0);
      
          void setPositionLogique(QVariant PositionLogique);
          void setCouleurFondLCD(QVariant CouleurFondLCD);
          void setCouleurReticule(QVariant CouleurReticule);
          void setNombreEcran(QVariant NombreEcran);
      
          inline int getPositionLogique() const { return m_PositionLogique; }
          inline int getCouleurFondLCD() const { return m_CouleurFondLCD; }
          inline int getCouleurReticule() const { return m_CouleurReticule; }
          inline int getNombreEcran() const { return m_NombreEcran; }
      
      signals:
      
          void positionLogiqueChanged();
          void couleurFondLCDChanged();
          void couleurReticuleChanged();
          void nombreEcranChanged();
      
      public slots:
      
      private:
          int m_PositionLogique;
          int m_CouleurFondLCD;
          int m_CouleurReticule;
          int m_NombreEcran;
      };
      

      I would insert a model herited from QAbstractListModel into that one, and keeping the notification behavior of that last one.

      This is the model I would insert:

      #ifndef CUSTOMSCREENMODEL_H
      #define CUSTOMSCREENMODEL_H
      
      #include <QObject>
      #include <QAbstractListModel>
      
      
      class CustomScreenItem
      {
      
      public:
          /*
          \fn Constructor
          */
          CustomScreenItem(const int &ChannelIndex, const QString &Channel, const int &PositionReticule);
      
          /*
          \fn Setters
          */
          void setChannelIndex(QVariant ChannelIndex);
          void setChannel(QVariant Channel);
          void setPositionReticule(QVariant PositionReticule);
      
          /*
          \fn Getters
          */
          inline int ChannelIndex() const { return m_ChannelIndex; }
          inline QString Channel() const { return m_Channel; }
          inline int PositionReticule() const { return m_PositionReticule; }
      
      private:
          int m_ChannelIndex;
          QString m_Channel;
          int m_PositionReticule;
      };
      
      class CustomScreenModel : public QAbstractListModel
      {
          Q_OBJECT
      public:
          enum CustomScreenRoles {
              ChannelIndexRole = Qt::UserRole+1,
              ChannelRole,
              PositionReticuleRole
          };
          CustomScreenModel(QObject *parent = 0);
          void addTriggerComplexItem(const CustomScreenItem &customScreenItem);
          Q_INVOKABLE void append(const int &ChannelIndex, const QString &Channel, const int &PositionReticule);
          Q_INVOKABLE int rowCount(const QModelIndex & parent = QModelIndex()) const;
          QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
          QVariant headerData(int section, Qt::Orientation orientation, int role) const;
          bool setData(const QModelIndex &index, const QVariant &value, int role);
          Qt::ItemFlags flags(const QModelIndex &index) const;
          Q_INVOKABLE bool removeRow(const int index);
          Q_INVOKABLE QVariantMap get(int idx) const;
      signals:
          void dataChanged();
      protected:
          QHash<int, QByteArray> roleNames() const;
      private:
          QList<CustomScreenItem> m_customScreenItem;
      };
      #endif // CUSTOMSCREENMODEL_H
      
      

      Thank you

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @RomanoFX said:

      I would insert a model herited from QAbstractListModel into that one, and keeping the notification behavior of that last one.

      what exactly do you mean?

      You wrapper class doesn't inherit from QAbstractItemModel, so you can't set it directly to an item view widget.
      Why not creating a simple getter method to get a pointer to the internal model and keep working with it.
      Your wrapper class can connect to any signal from the model.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • RomanoFXR Offline
        RomanoFXR Offline
        RomanoFX
        wrote on last edited by RomanoFX
        #3

        Hi @raven-worx ,

        Thank you for your answer.

        I would have my ScreenModel like this :
        4 datas : positionLogique, couleurFondLCD, couleurReticule and nombreEcran
        x2 CustomScreenModel

        Should I declare two instances of CustomScreenModel as Q_PROPERTY?

        According to yours questions, CustomScreenModel inherits from QAbstractListModel (class CustomScreenModel : public QAbstractListModel), but every item (CustomScreenItem) is not inherited from QAbstractItemModel.
        Maybe am I wrong?

        Thank you

        raven-worxR 1 Reply Last reply
        0
        • RomanoFXR RomanoFX

          Hi @raven-worx ,

          Thank you for your answer.

          I would have my ScreenModel like this :
          4 datas : positionLogique, couleurFondLCD, couleurReticule and nombreEcran
          x2 CustomScreenModel

          Should I declare two instances of CustomScreenModel as Q_PROPERTY?

          According to yours questions, CustomScreenModel inherits from QAbstractListModel (class CustomScreenModel : public QAbstractListModel), but every item (CustomScreenItem) is not inherited from QAbstractItemModel.
          Maybe am I wrong?

          Thank you

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @RomanoFX
          may it be that you just want to use your ScreenModel class simply as interface?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • RomanoFXR Offline
            RomanoFXR Offline
            RomanoFX
            wrote on last edited by
            #5

            I think I found the right way to do that :

            class ScreenModel : public QObject
            {
                Q_OBJECT
                Q_PROPERTY( int positionLogique READ getPositionLogique WRITE setPositionLogique NOTIFY positionLogiqueChanged )
                Q_PROPERTY( int couleurFondLCD READ getCouleurFondLCD WRITE setCouleurFondLCD NOTIFY couleurFondLCDChanged)
                Q_PROPERTY( int couleurReticule READ getCouleurReticule WRITE setCouleurReticule NOTIFY couleurReticuleChanged)
                Q_PROPERTY( int nombreEcran READ getNombreEcran WRITE setNombreEcran NOTIFY nombreEcranChanged)
                Q_PROPERTY( CustomScreenModel* custom1 READ custom1 WRITE setCustom1 )
            public:
                explicit ScreenModel(QObject *parent = 0);
            
                void setPositionLogique(QVariant PositionLogique);
                void setCouleurFondLCD(QVariant CouleurFondLCD);
                void setCouleurReticule(QVariant CouleurReticule);
                void setNombreEcran(QVariant NombreEcran);
                void setCustom1(CustomScreenModel* Custom1);
            
                inline int getPositionLogique() const { return m_PositionLogique; }
                inline int getCouleurFondLCD() const { return m_CouleurFondLCD; }
                inline int getCouleurReticule() const { return m_CouleurReticule; }
                inline int getNombreEcran() const { return m_NombreEcran; }
                inline CustomScreenModel *custom1() const { return m_Custom1; }
                //inline CustomScreenModel getNombreEcran() const { return m_NombreEcran; }
            
            
            
            signals:
            
                void positionLogiqueChanged();
                void couleurFondLCDChanged();
                void couleurReticuleChanged();
                void nombreEcranChanged();
            
            public slots:
            
            private:
                int m_PositionLogique;
                int m_CouleurFondLCD;
                int m_CouleurReticule;
                int m_NombreEcran;
                CustomScreenModel *m_Custom1;
            

            I should add NOTIFY behavior. Is this correct?
            It makes possible to access some models into that interface

            1 Reply Last reply
            0

            • Login

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