[Interest] Change QCheckBox to QPushButton in a QTableView

André Somers andre at familiesomers.nl
Thu Dec 7 07:10:02 CET 2017



Op 07/12/2017 om 00:21 schreef Jason H:
> I have a model, a few columns of which are supposed to be QPushButtons. 
> I've got it working with checkboxes just fine, and I can get the QPushButtons to display while editing. 
> But I want to display the checkable QPushButton reflecting it's checked state to be displayed all the time, not just when editing.
>
> I looked at the stars delegate example, but that's got some rudimentary painting. 
> I found the ProgressBar example in QAbstractItemDelegate, but that didn't worl
>
> void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
> 	if (index.column() == 0) {
> 		QStyleOptionButton button;
> 		button.text = _text;
> 		QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
> 	} else
> 		QStyledItemDelegate::paint(painter, option, index);
> }
>
> QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
> 	return QSize(50,10);
> }
>
> But all I got out of it was a black pixel in the first cell. 

You're going to have to do a proper initialization of the
QStyleOptionButton. At the very least, set the rect it is supposed to be
drawn in. You can copy that information from the QStyleOptionViewItem.
You will need to set the checked state from there as well.

André



More information about the Interest mailing list