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 receive notification of checking checkbox in QAbstractItemModel?

How to receive notification of checking checkbox in QAbstractItemModel?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 734 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.
  • J Offline
    J Offline
    johnby
    wrote on last edited by
    #1

    I have a TreeModel derived from QAbstractItemModel in which I have several TreeItem's which have check boxes by using this function.

    Qt::ItemFlags MyTreeModel::flags(const QModelIndex &index) const
    {
        if (!index.isValid())
            return 0;
    
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    
        if (index.column() == 0 && !index.parent().isValid()) {
            flags |= Qt::ItemIsUserCheckable;
    
            return flags;
        }
    
        return QAbstractItemModel::flags(index);
    }
    

    I'd like to receive some sort of callback or notification if the item is checked but not sure how this is done. I suspect there is already some way to do this that I'm not aware of.

    Any ideas?

    JonBJ 1 Reply Last reply
    0
    • J johnby

      I have a TreeModel derived from QAbstractItemModel in which I have several TreeItem's which have check boxes by using this function.

      Qt::ItemFlags MyTreeModel::flags(const QModelIndex &index) const
      {
          if (!index.isValid())
              return 0;
      
          Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
      
          if (index.column() == 0 && !index.parent().isValid()) {
              flags |= Qt::ItemIsUserCheckable;
      
              return flags;
          }
      
          return QAbstractItemModel::flags(index);
      }
      

      I'd like to receive some sort of callback or notification if the item is checked but not sure how this is done. I suspect there is already some way to do this that I'm not aware of.

      Any ideas?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @johnby
      There isn't a specific signal (your "callback or notification") when the checked value is changed in the model; there might be in any attached view, I don't know.

      But if you override your model's setData() method you should see that it is called with parameter role == Qt::CheckStateRole for this case, which you can act on.

      I think https://stackoverflow.com/questions/17786086/how-set-qcheckbox-in-qabstractitemmodel illustrates this approach.

      Alternatively, I have not tested this, but signal dataChanged() (https://doc.qt.io/qt-5/qabstractitemmodel.html#dataChanged) may also be emitted for this, with role == Qt::CheckStateRole, to which you could attach a slot.

      Does this answer your question?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        johnby
        wrote on last edited by
        #3

        Close enough, yes, thanks.

        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