[Interest] [Solved] Change QCheckBox to QPushButton in a QTableView
Jason H
jhihn at gmx.com
Thu Dec 7 21:49:50 CET 2017
Oh man what a trip. The essentials for anyone who wants to replace checkboxes with QPushbuttons:
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
bool isChecked = index.data(Qt::CheckStateRole).toInt() == Qt::Checked;
//qDebug() << Q_FUNC_INFO << "checkState" << index.row() << index.column() << isChecked << option.state;
QStyleOptionButton sob;
PushButton button;
button.setCheckable(true);
button.setChecked(isChecked);
button.initStyleOption(&sob);
sob.rect = option.rect;
sob.text = _text;
sob.state = QStyle::State_Raised;
sob.state.setFlag( isChecked ? QStyle::State_On : QStyle::State_Off);
QApplication::style()->drawControl(QStyle::CE_PushButton, &sob, painter);
}
void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
qDebug() << Q_FUNC_INFO;
QPushButton *button = static_cast<QPushButton*>(editor);
bool isChecked = index.model()->data(index, Qt::CheckStateRole).toInt() == Qt::Checked;
button->setChecked( ! isChecked); // Toggle it here!
}
void TableView::mouseReleaseEvent(QMouseEvent *event)
{
QModelIndex index = indexAt(event->pos());
//setCurrentIndex(index);
QWidget * widget = indexWidget(index);
if (event->button() == Qt::LeftButton && index.isValid()) {
CsvModel *csv_model = static_cast<CsvModel*>(model());
if (csv_model->isOutput(index.column())) { // column you want to use for one click
edit(index);
endEdit(); // don't need to wait, we toggled it via setEditorData
}
}
QTableView::mousePressEvent(event);
}
void TableView::endEdit() {
QModelIndex index = currentIndex();
currentChanged( index, index );
}
More information about the Interest
mailing list