Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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


  • Moderators

    @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).


  • Lifetime Qt Champion

    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.



  • 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;
        }
    
    }
    

  • Moderators

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



  • if you provide a example code . It will be helpful


  • Moderators

    @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>();
           }
    }
    

Log in to reply