3 #include <QApplication> 7 QStyledItemDelegate(parent)
16 QStyleOptionViewItem viewItemOption(option);
18 if (index.column() == 0)
20 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
21 QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
22 QSize(option.decorationSize.width() + 5,option.decorationSize.height()),
23 QRect(option.rect.x() + textMargin, option.rect.y(),
24 option.rect.width() - (2 * textMargin), option.rect.height()));
25 viewItemOption.rect = newRect;
35 Qt::ItemFlags flags = model->flags(index);
36 if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
39 QVariant value = index.data(Qt::CheckStateRole);
44 if (event->type() == QEvent::MouseButtonRelease)
46 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
47 QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
48 option.decorationSize,
49 QRect(option.rect.x() + (2 * textMargin), option.rect.y(),
50 option.rect.width() - (2 * textMargin),
51 option.rect.height()));
53 if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
56 else if (event->type() == QEvent::KeyPress)
58 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space&&
static_cast<QKeyEvent*
>(event)->key() != Qt::Key_Select)
66 Qt::CheckState state = (
static_cast<Qt::CheckState
>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
68 return model->setData(index, state, Qt::CheckStateRole);
Just use a delegate to render checkboxes centralized on a view.
void paint(te::qt::widgets::Canvas *c, bool generatePNG, std::string fileName)