0

I want to add a column with checkbox in qtableview with model is qsqlmodel.The model already had query, and qtableview should display the checkbox in the firstcolumn.I try all the solution in google, but failed.Any help is welcome.

Matthew
  • 2,759
  • 18
  • 28
coolboymark
  • 203
  • 1
  • 3
  • 9

1 Answers1

0

Hello you'll want to do the follow.

Set flags

Set the flags for the column you want the checkbox to appear in. See this answer for an example of setting the Qt::ItemFlags for a checkbox, how do i get a checkbox item from a QTableView and QStandardItemModel alone?

I.e. your model class should override:

Qt::ItemFlags flags ( const QModelIndex & index ) const;

to ensure you are returning Qt::ItemIsUserCheckable for your first column.

Return checked state

Next you're going to want to modify your model class to override:

QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;

to ensure it returns something for Qt::CheckStateRole such as Qt::Unchecked, Qt::Checked, or Qt::PatriallyChecked

Hope that helps!

Community
  • 1
  • 1
Matthew
  • 2,759
  • 18
  • 28