Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disable Qt::CheckStateRole



  • 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.


  • Lifetime Qt Champion

    @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.



  • 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.


  • Lifetime Qt Champion

    You can use a QIdentityProxyModel


Log in to reply