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. Is it possible to get pointer to View object inside the methods of CustomModel
Forum Updated to NodeBB v4.3 + New Features

Is it possible to get pointer to View object inside the methods of CustomModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by A Former User
    #1
    class myModel: public public QAbstractItemModel
    {
     public :
         virtual QVariant data(const QModelIndex& index, int role) const;
      
    };
    
    }
    
    class my View : public QTreeView {
    
      enableColors() {   ....};
     private:
       myModel * model;
    };
    
    myModel::data(const QModelIndex& index, int role) {
        // I want to access menthod enableColors from here    
        
    }
    
    myView::myView() {
     model = new myModel;
     setModel(model);
    }
    

    can some one guide how to call enableColors from data method of myModel

    raven-worxR 1 Reply Last reply
    0
    • Q Qt Enthusiast
      class myModel: public public QAbstractItemModel
      {
       public :
           virtual QVariant data(const QModelIndex& index, int role) const;
        
      };
      
      }
      
      class my View : public QTreeView {
      
        enableColors() {   ....};
       private:
         myModel * model;
      };
      
      myModel::data(const QModelIndex& index, int role) {
          // I want to access menthod enableColors from here    
          
      }
      
      myView::myView() {
       model = new myModel;
       setModel(model);
      }
      

      can some one guide how to call enableColors from data method of myModel

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

      @Qt-Enthusiast
      in general no, since a model can be used with multiple views simultaneously.
      But if you know you are only using it in a 1:1 relation, you need to set the pointer to your view widget to the model (like you already do the other way around).

      --- 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Why does your model need to access a function of the view ? You are on the way to create a tight coupling which is a bad idea.

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

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by A Former User
          #4

          I want to enable some colors view in model icons when some flag is set in my View object . Thus I have first check if the flag is set in my view then send the data

          QVariant myModel::data(const QModelIndex& index, int role) const
          
          switch (role) {
            case Qt::DecorationRole:
              
                if  (myView flag is set)------------------------------------------------- here I want to access the method of view function
                    if ( index.column() == 1) {
                     QColor sc ;
                     sc = getColorFromInstance(index);
                 }
                 QPixmap cellPixmap(QSize(10,10));
                cellPixmap.fill(sc);
                QIcon icon(cellPixmap);
                return icon;
              }
          
          }
          
          raven-worxR 1 Reply Last reply
          0
          • Q Qt Enthusiast

            I want to enable some colors view in model icons when some flag is set in my View object . Thus I have first check if the flag is set in my view then send the data

            QVariant myModel::data(const QModelIndex& index, int role) const
            
            switch (role) {
              case Qt::DecorationRole:
                
                  if  (myView flag is set)------------------------------------------------- here I want to access the method of view function
                      if ( index.column() == 1) {
                       QColor sc ;
                       sc = getColorFromInstance(index);
                   }
                   QPixmap cellPixmap(QSize(10,10));
                  cellPixmap.fill(sc);
                  QIcon icon(cellPixmap);
                  return icon;
                }
            
            }
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @Qt-Enthusiast
            the clean way would be introducing a custom data role (value starting at Qt::UserDataRole +1).
            Then the view should decide which role it requests from the model. As i said a model can be shared by multiple views simultaneously.
            Thus a model should be only a homogeneous data provider.

            The view decides if it requests the decoration role or the introduced custom role.
            This should be done by subclassing the view and reimplement the viewOptions() method. First call the base class implementation and then overwrite the returned option's icon property.

            --- 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
            2
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              if you provide a example code . It will be helpful

              raven-worxR 1 Reply Last reply
              0
              • Q Qt Enthusiast

                if you provide a example code . It will be helpful

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

                @Qt-Enthusiast

                sorry, but i gave you a wrong information before. This can't be handled in the view completely (via viewOptions() method), since the icon depends actually on the index.

                So you would need to do this in a custom item delegate. Which is at the end not so much cleaner than your initial approach. But here is an example:

                Model:

                class MyModel : public ...
                {
                public:
                     enum MyDataRole {
                           MySpecialDecorationRole = Qt::UserDataRole + 1,
                           MySecondSpecialDecorationRole
                     };
                
                     QVariant data(const QModelIndex& index, int role) const
                     {
                             ...
                             switch( role )
                             {
                                   case Qt::DecorationRole:    return ...;   break;
                                   case MySpecialDataRole:    return ...;   break;
                             }
                     }
                }
                

                View:

                class MyDelegate : public QStyledItemDelegate
                {
                
                protected:
                       virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                       {
                                QStyledItemDelegate::initStyleOption(option, index);
                                if( flagIsSet )
                                    option->icon = index.data( MyModel::MySpecialDataRole ).value<QIcon>();
                       }
                }
                

                --- 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

                • Login

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