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. How to change item editable / not-editable in QAbstractItemModel?
Forum Updated to NodeBB v4.3 + New Features

How to change item editable / not-editable in QAbstractItemModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.0k Views 1 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.
  • O Offline
    O Offline
    Oktay K.
    wrote on last edited by
    #1

    Hi,

    We want to make our table columns editable or not-editable with user input. Therefore, we write a function to do that.

    • Qt Version : 5.9.7
    • We are allowed to use only QAbstractItemModel.
    • this pointer is QTableView

    The function code block is below.

    void setColumnEditable(const int &column, const bool is_editable) const
    {
        QAbstractItemModel *p_model = this->model();
    
        if(Q_NULLPTR == p_model)
            return;
    
        if(0 <= column)
        {
            for(int row = 0; row < p_model->rowCount(); ++row)
            {
                p_model->flags(p_model->index(row,column)).setFlag(Qt::ItemIsEditable,is_editable);
                qDebug() << "is editable ?" << is_editable << p_model->flags(p_model->index(row,column)); 
            }
        }
    } 
     
    

    qDebug result:

    is editable ? true QFlags<Qt::ItemFlags>(ItemIsSelectable|ItemIsDragEnabled|ItemIsDropEnabled|ItemIsEnabled) 
    

    However, ItemIsEditable does not set.

    How can we change the ItemFlags?

    JonBJ 1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are changing the value of the temporary object returned by the flags method.

      What model are you currently use ?

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

      O 1 Reply Last reply
      1
      • O Oktay K.

        Hi,

        We want to make our table columns editable or not-editable with user input. Therefore, we write a function to do that.

        • Qt Version : 5.9.7
        • We are allowed to use only QAbstractItemModel.
        • this pointer is QTableView

        The function code block is below.

        void setColumnEditable(const int &column, const bool is_editable) const
        {
            QAbstractItemModel *p_model = this->model();
        
            if(Q_NULLPTR == p_model)
                return;
        
            if(0 <= column)
            {
                for(int row = 0; row < p_model->rowCount(); ++row)
                {
                    p_model->flags(p_model->index(row,column)).setFlag(Qt::ItemIsEditable,is_editable);
                    qDebug() << "is editable ?" << is_editable << p_model->flags(p_model->index(row,column)); 
                }
            }
        } 
         
        

        qDebug result:

        is editable ? true QFlags<Qt::ItemFlags>(ItemIsSelectable|ItemIsDragEnabled|ItemIsDropEnabled|ItemIsEnabled) 
        

        However, ItemIsEditable does not set.

        How can we change the ItemFlags?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @Oktay-K said in How to change item editable / not-editable in QAbstractItemModel?:

        p_model->flags(p_model->index(row,column)).setFlag(Qt::ItemIsEditable,is_editable);
        

        https://doc.qt.io/qt-5/qabstractitemmodel.html#flags shows Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const is const, and the flags it returns is just a copied value. Calling setFlag() on that does nothing to the model.

        I think you have to implement a yourModelItem.setFlags(), storing the value into the data. For example https://doc.qt.io/qt-5/qstandarditem.html#setFlags might be implemented via:

        void QStandardItem::setFlags(Qt::ItemFlags flags)
        {
            setData((int)flags, Qt::UserRole - 1);
        }
        

        Or, if you want every item to be editable, or certain columns to be editable, without having to selectively store that against individual items, you might subclass QAbstractItemModel and override its virtual flags() to return flags plus OR-in Qt::ItemIsEditable where appropriate, but I'm not sure that is what you want if you wish the user to be able to affect it.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You are changing the value of the temporary object returned by the flags method.

          What model are you currently use ?

          O Offline
          O Offline
          Oktay K.
          wrote on last edited by
          #4

          @SGaist

          We are currently using QStandardItemModel . However, we can use any model available thats why we need to change it to the QAbstractItemModel.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Then retrieve the item matching the index and set the new flags on it.

            The method is:

            • retrieve the flags from the item
            • change them
            • set the changed flags on the item

            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
            0

            • Login

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