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. Disable tristate of checkbox in QTableView
Forum Updated to NodeBB v4.3 + New Features

Disable tristate of checkbox in QTableView

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

    I'm displaying a checkbox in a QTableView by overriding the table view model's flags-function:

    Qt::ItemFlags TableViewModel::flags(const QModelIndex& in_index) const
    {
        if (CHECKBOX_COL_INDEX == in_index.column())
        {
            return QStandardItemModel::flags(in_index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        }
    
        return QStandardItemModel::flags(in_index);
    }
    

    This works, but the resulting checkbox has three states. I want it to have only two.
    I found the flags Qt::ItemIsTristate (deprecated) and Qt::ItemIsAutoTristate, but I can't work out how to use them.
    Any help?

    JonBJ 1 Reply Last reply
    0
    • qwasder85Q qwasder85

      I'm displaying a checkbox in a QTableView by overriding the table view model's flags-function:

      Qt::ItemFlags TableViewModel::flags(const QModelIndex& in_index) const
      {
          if (CHECKBOX_COL_INDEX == in_index.column())
          {
              return QStandardItemModel::flags(in_index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
          }
      
          return QStandardItemModel::flags(in_index);
      }
      

      This works, but the resulting checkbox has three states. I want it to have only two.
      I found the flags Qt::ItemIsTristate (deprecated) and Qt::ItemIsAutoTristate, but I can't work out how to use them.
      Any help?

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

      @qwasder85
      Assuming, from what you say (I don't know if it's true, I'm slightly surprised that it does...) that QStandardItemModel::flags(in_index) is returning Qt::ItemIsAutoTristate among its flags, then to switch off, and switch others on, all in one line you would need:

       return (QStandardItemModel::flags(in_index) & ~Qt::ItemIsAutoTristate) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
      

      Adding a flag is always done via |, removing a flag via & ~.

      qwasder85Q 1 Reply Last reply
      2
      • JonBJ JonB

        @qwasder85
        Assuming, from what you say (I don't know if it's true, I'm slightly surprised that it does...) that QStandardItemModel::flags(in_index) is returning Qt::ItemIsAutoTristate among its flags, then to switch off, and switch others on, all in one line you would need:

         return (QStandardItemModel::flags(in_index) & ~Qt::ItemIsAutoTristate) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        

        Adding a flag is always done via |, removing a flag via & ~.

        qwasder85Q Offline
        qwasder85Q Offline
        qwasder85
        wrote on last edited by
        #3

        @JonB
        You were right about being surprised. Because it wasn't returning Qt::ItemIsAutoTristate.
        I'm overriding the setData()-function in that model as well and apparently the issue came from handling the checked-state in there. I'm not yet sure how this resulted in this behavior.

        So thank you, the issue was NOT with the flags.

        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