Editable checkbox in QTableView column

I have a checkbox column in QTableView

. This flag is generated:

  • return Qt::ItemIsUserCheckable

    to overridden flags

    member function
  • in the overridden function data()

    i return Qt::CheckState

    for role == Qt::CheckStateRole

    according to the data

Works, see screenshot.

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

ReadmodeEditmode


As requested, I can only show a 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 delete Qt::ItemIsEditable

then this checkbox is read as well. I later found a SO answer with a similar approach.


Note. No duplicate Column only in QTableView field

+3


source to share


1 answer


Replace flag

Qt::ItemIsEditable

with flag



Qt::ItemIsEnabled

The first tells Qt to create an editor for the value present in the model, which appears to be a text editor in your case. If the value is of type bool

, then a drop-down list containing true

and will be shown instead false

.

+2


source







All Articles