[Interest] Change QCheckBox to QPushButton in a QTableView
Jason H
jhihn at gmx.com
Thu Dec 7 16:09:56 CET 2017
> I added an assignment of QStypeOptionButton rect from options, and that now shows the text, but the rest of the button is not rendered (Attached). If I use a pushbutton to initialize options I've got to subclass it because initStypeOptions is protected (why?), won't I overwrite the supplied options and then have to merge them back in, how do I know what needs to be preserved from the original options? This whole system seems backwards. My delegate doesn't have a pointer to a widget does it?
>
> This whole setup seems backwards, which is unusual for me with Qt. I can get behind the idea of being passed a painter and rect to render into, but the rest of the API seems like it's making a main use-case extremely difficult. Which usually isn't the case with Qt, so it makes me think I'm doing something very wrong.
Well I'm making some progress.
The current code looks like:
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column() == 2) {
bool isChecked = index.data(Qt::CheckStateRole).toInt() == Qt::Checked; // always is true for some reason
qDebug() << Q_FUNC_INFO << "checkState" << index.row() << index.column() << option.checkState << isChecked;
QStyleOptionButton sob;
PushButton button; // just makes initStyleOption() public!!!
button.setCheckable(true); // these don't seem
button.setChecked(isChecked); // to have any affect.
button.initStyleOption(&sob);
sob.rect = option.rect;
sob.text = _text;
sob.state |= isChecked ? QStyle::State_On : QStyle::State_Off; // this doesn't seem to have any affect either.
QApplication::style()->drawControl(QStyle::CE_PushButton, &sob, painter);
} else
QStyledItemDelegate::paint(painter, option, index);
}
And it displays a button that only kind-of works. It doesn't show the check state, until I double-click and the editor widget takes over.
Issues:
1. I want single-click to toggle.
2. Check state not being shown until in the editor.
More information about the Interest
mailing list