Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't deal with QTableView+QStandardItemModel, but may be example below will help you:</p> <p>1). table.h file:</p> <pre><code>#ifndef TABLE__H #define TABLE__H #include &lt;QtGui&gt; class ItemDelegate : public QItemDelegate { public: ItemDelegate(QObject *parent = 0) : QItemDelegate(parent) { } virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &amp;option, const QRect &amp;, Qt::CheckState state) const { const int textMargin = QApplication::style()-&gt;pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, check(option, option.rect, Qt::Checked).size(), QRect(option.rect.x() + textMargin, option.rect.y(), option.rect.width() - (textMargin * 2), option.rect.height())); QItemDelegate::drawCheck(painter, option, checkRect, state); } virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) { Q_ASSERT(event); Q_ASSERT(model); // make sure that the item is checkable Qt::ItemFlags flags = model-&gt;flags(index); if (!(flags &amp; Qt::ItemIsUserCheckable) || !(flags &amp; Qt::ItemIsEnabled)) return false; // make sure that we have a check state QVariant value = index.data(Qt::CheckStateRole); if (!value.isValid()) return false; // make sure that we have the right event type if (event-&gt;type() == QEvent::MouseButtonRelease) { const int textMargin = QApplication::style()-&gt;pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, check(option, option.rect, Qt::Checked).size(), QRect(option.rect.x() + textMargin, option.rect.y(), option.rect.width() - (2 * textMargin), option.rect.height())); if (!checkRect.contains(static_cast&lt;QMouseEvent*&gt;(event)-&gt;pos())) return false; } 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; } Qt::CheckState state = (static_cast&lt;Qt::CheckState&gt;(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); QMessageBox::information(0, QString((state == Qt::Checked) ? "Qt::Checked" : "Qt::Unchecked"), QString("[%1/%2]").arg(index.row()).arg(index.column())); return model-&gt;setData(index, state, Qt::CheckStateRole); } virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &amp;option, const QRect &amp;rect) const { QItemDelegate::drawFocus(painter, option, option.rect); } }; static int ROWS = 3; static int COLS = 1; class Table : public QTableWidget { Q_OBJECT public: Table(QWidget *parent = 0) : QTableWidget(ROWS, COLS, parent) { setItemDelegate(new ItemDelegate(this)); QTableWidgetItem *item = 0; for (int i=0; i&lt;rowCount(); ++i) { for (int j=0; j&lt;columnCount(); ++j) { setItem(i, j, item = new QTableWidgetItem); item-&gt;setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable); item-&gt;setCheckState((i+j) % 2 == 0 ? Qt::Checked : Qt::Unchecked); } } } }; #endif </code></pre> <p>2). main.cpp file:</p> <pre><code>#include &lt;QApplication&gt; #include "table.h" int main(int argc, char **argv) { QApplication a(argc, argv); Table w; w.show(); return a.exec(); } </code></pre> <p>Good luck.</p> <p>PS: here is the original <a href="http://qt.nokia.com/developer/faqs/faq.2008-01-03.2614755816" rel="nofollow noreferrer">text</a>.</p>
    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. 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.
 

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