Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    5
    1133
    Loading More Posts
    • 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
      Oktay K. last edited by

      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?

      JonB 1 Reply Last reply Reply Quote 1
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • JonB
          JonB @Oktay K. last edited by JonB

          @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 Reply Quote 0
          • O
            Oktay K. @SGaist last edited by

            @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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 0
              • First post
                Last post