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 Qt::CheckStateRole
QtWS25 Last Chance

Disable Qt::CheckStateRole

Scheduled Pinned Locked Moved Unsolved General and Desktop
model-view
4 Posts 2 Posters 2.3k 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.
  • O Offline
    O Offline
    ochampao
    wrote on last edited by
    #1

    I am developing a custom model derived from QAbstractTableModel. The model re-implements the data(), setData() and flags() methods as follows:

    QVariant TableModel::data(const QModelIndex &index, int role) const
    {
    	if (!index.isValid())
    		return QVariant();
    
    	// get the coordinates of the item
    	int row = index.row();
    	int col = index.column();
    
    	// load the record pointed to by the row index for the current query
    	Record recordLoaded = mDao->readRecordN(row, mCurrentQuery);
    
    	// handle the different roles
    	switch (role)
    	{
    	case Qt::DisplayRole:
    		return readDisplayRoleData(col, recordLoaded);
    	
    	case Qt::EditRole:
    		return readEditRoleData(col, recordLoaded);
    
    	case Qt::BackgroundRole:
    		return readBackgroundRole(col, recordLoaded);
    
    	case Qt::CheckStateRole:
    		return readCheckStateRoleData(col, recordLoaded);
    
    	default:
    		break;
    	}
    
    	return QVariant();
    }
    
    bool TableModel::setData(const QModelIndex & index, const QVariant & value, int role)
    {
    	if (!index.isValid())
    		return false;
    
    	// get the coordinates of the item to be edited
    	int row = index.row();
    	int col = index.column();
    
    	// load record to be edited
    	Record recordEdited = mDao->readRecordN(row, mCurrentQuery);
    
    	switch (role)
    	{
    	case Qt::EditRole:
    	if (writeEditRoleData(col, recordEdited, value))
    	{
    		mDao->updateRecord(recordEdited);
    
    		// notify the view for the changes
    		emit dataChanged(index, index);
    
    		return true;
    	}
    	case Qt::BackgroundRole:
    	if (writeBackgroundRole(col, recordEdited, value))
    	{
    		// notify the view for the changes
    		emit dataChanged(index, index);
    
    		return true;
    	}
    	case Qt::CheckStateRole:
    	if (writeCheckStateRoleData(col, recordEdited, value))
    	{
    		// notify the view for the changes
    		emit dataChanged(index, index);
    		
    		return true;
    		}
    	default:
    		break;
    	}
    
    	return false;
    
    }
    
    Qt::ItemFlags TableModel::flags(const QModelIndex& index) const
    {
    	if (!index.isValid())
    		return Qt::ItemIsEnabled;
    
    	int columnId = index.column();
    	 
    	switch (columnId)
    	{
    	case TableModel::Column1:
    	case TableModel::Column2:
    	case TableModel::Column3:
    	case TableModel::Column4:
    	default:
    	  return QAbstractItemModel::flags(index);
    		break;
    	}
    }
    

    It is also important to note how the two methods readCheckStateRoleData() and writeCheckStateRoleData() are implemented:

    QVariant TableModel::readCheckStateRoleData(int columnId, Record& record) const
    {
    	switch (columnId)
    	{
    	case TableModel::Column1:
    	{
    		bool isVisible2D = false;
    		
    		// Some code to read value of isVisible2D
    
    		return (isVisible2D ? Qt::Checked : Qt::Unchecked);
    	}
    	case TableModel::Column2:
    	{
    		bool isVisible3D = false;
    		
    		// some code to read value of isVisible3D
    		
    		return (isVisible3D ? Qt::Checked : Qt::Unchecked);
    	}
    	case TableModel::Column3:
    	case TableModel::Column4:
    	default:
    		return QVariant();
    	}
    }
    
    bool TableModel::writeCheckStateRoleData(int columnId, PlanImageRecord& record, const QVariant& value)
    {
    	switch (columnId)
    	{
    	case TableModel::Column1:
    	{	
    		bool isChecked = false;
    		if ((Qt::CheckState)value.toInt() == Qt::Checked)
    			isChecked = true;
    
    		// code to save value in database
    		return true;
    	}
    	case TableModel::Column2:
    	{
    		bool isChecked = false;
    		if ((Qt::CheckState)value.toInt() == Qt::Checked)
    			isChecked = true;
    
    		// code to write new value in database
    		return true;
    	}
    	case TableModel::Column3:
    	case TableModel::Column4:
    	default:
    		return false;
    	}
    }
    

    The problem that I have is that although the flags() method for Column1 and Column2 does not set the flag Qt::ItemIsUserCheckable, the items of these columns appear as checkboxes. How can I avoid this while still handling the Qt::CheckStateRole role in the data() and setData() methods? Is this possible?

    The reason why I am trying to keep handling the Qt::CheckStateRole is because I am displaying the same model in two separate QTableView's but in the second view the model is wrapped in a proxy model which modifies the flags such that the columns are editable or displayed as check-boxes where appropriate. In the first view, I only want the DisplayRole of the items to be shown.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ochampao said in Disable Qt::CheckStateRole:

      Is this possible?

      No since you return valid values for the Qt::CheckStateRole for those columns. When you don't want checkboxes then don't return a valid value for them.
      The flag only defines if the use is able to modify the check state or not.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • O Offline
        O Offline
        ochampao
        wrote on last edited by
        #3

        Thank you for your reply!

        So the only way that I can achieve what I am trying to do is using a custom Delegate? Is there an alternative to implementing a delegate?

        Thanks.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can use a QIdentityProxyModel

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          • Login

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