Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution. For this to work properly, your column should <strong>not</strong> have the <code>Qt::ItemIsEditable</code> or <code>Qt::ItemIsUserCheckable</code> flags set. This reads the boolean values from <code>Qt::DisplayRole</code> and calls <code>setData()</code> with <code>Qt::EditRole</code> (i.e. <strong>not</strong> <code>Qt::CheckStateRole</code>.)</p> <pre><code>#include "check_box_delegate.h" #include &lt;QtGui/QApplication&gt; #include &lt;QtGui/QMouseEvent&gt; static QRect CheckBoxRect(const QStyleOptionViewItem &amp;view_item_style_options) { QStyleOptionButton check_box_style_option; QRect check_box_rect = QApplication::style()-&gt;subElementRect( QStyle::SE_CheckBoxIndicator, &amp;check_box_style_option); QPoint check_box_point(view_item_style_options.rect.x() + view_item_style_options.rect.width() / 2 - check_box_rect.width() / 2, view_item_style_options.rect.y() + view_item_style_options.rect.height() / 2 - check_box_rect.height() / 2); return QRect(check_box_point, check_box_rect.size()); } CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const { bool checked = index.model()-&gt;data(index, Qt::DisplayRole).toBool(); QStyleOptionButton check_box_style_option; check_box_style_option.state |= QStyle::State_Enabled; if (checked) { check_box_style_option.state |= QStyle::State_On; } else { check_box_style_option.state |= QStyle::State_Off; } check_box_style_option.rect = CheckBoxRect(option); QApplication::style()-&gt;drawControl(QStyle::CE_CheckBox, &amp;check_box_style_option, painter); } // This is essentially copied from QStyledItemEditor, except that we // have to determine our own "hot zone" for the mouse click. bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) { if ((event-&gt;type() == QEvent::MouseButtonRelease) || (event-&gt;type() == QEvent::MouseButtonDblClick)) { QMouseEvent *mouse_event = static_cast&lt;QMouseEvent*&gt;(event); if (mouse_event-&gt;button() != Qt::LeftButton || !CheckBoxRect(option).contains(mouse_event-&gt;pos())) { return false; } if (event-&gt;type() == QEvent::MouseButtonDblClick) { return true; } } else if (event-&gt;type() == QEvent::KeyPress) { if (static_cast&lt;QKeyEvent*&gt;(event)-&gt;key() != Qt::Key_Space &amp;&amp; static_cast&lt;QKeyEvent*&gt;(event)-&gt;key() != Qt::Key_Select) { return false; } } else { return false; } bool checked = index.model()-&gt;data(index, Qt::DisplayRole).toBool(); return model-&gt;setData(index, !checked, Qt::EditRole); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload