Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editable checkbox only column in QTableView

I have column with a checkbox in a QTableView. The checkbox is generated by:

  1. returning Qt::ItemIsUserCheckable in overridden flags member function
  2. in overridden data() function I return a Qt::CheckState for role == Qt::CheckStateRole according to data

Works, see screenshot.

But beside the checkbox I have some editable textbox in the column. How can I get rid of this textbox (where I have entered "dsdsdsds" for demonstration? Clarification, the checkbox shall be editable, but nothing else.

ReadmodeEditmode


As requested, I can only show simplified version

Qt::ItemFlags MyClass::flags(const QModelIndex &index) const {
 Qt::ItemFlags f = QAbstractListModel::flags(index);
 ... return f if index is not target column ....
 // for target column with checkbox
 return (f | Qt::ItemIsEditable | Qt::ItemIsUserCheckable; )
}


QVariant MyClass::data(const QModelIndex &index, int role) const {
  .. do something for other columns
  .. for checkbox column
  if (role != Qt::CheckStateRole) { return QVariant(); }
  bool b = ... get value for checkbox column
  Qt::CheckState cs = b ? Qt::Checked : Qt::Unchecked;
  return QVariant(static_cast<int>(cs));
}

If I remove Qt::ItemIsEditable then the checkbox is read only too. I later found an SO answer with a similar approach.


Remark: No duplicate of A checkbox only column in QTableView

like image 425
Horst Walter Avatar asked Oct 20 '22 19:10

Horst Walter


1 Answers

Replace the flag

Qt::ItemIsEditable

with the flag

Qt::ItemIsEnabled

The first one tells Qt to create an editor for the value present in the model, which seems to be a texteditor in your case. If the value is of type bool then a dropdown list containing true and false would be shown instead.

like image 129
MKroehnert Avatar answered Oct 22 '22 14:10

MKroehnert