How can I center QCheckBox in QTableView?
-
I have 2 table views in my application. I'd like to have centered checkboxes as delegates for specific columns. One table should have delegate editable and another not.
Making a research, I leared that I have to implement my own delegate for it. Googling led me to this pretty old but useful link: https://www.qtcentre.org/threads/19157-QTableView-checkbox-center-with-stylesheet.
Unfortunately, after creating and setting delegates, everything is displayed as 'checked' and nothing has checking possibility. Also, I don't really understand how to use it. Thank you. -
You should follow this example: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview
-
@CuriousPan
yep, definitely follow the example provided by @Christian-Ehrlicher.
its always better to draw stuff rather than instantiate widgets for such purposes! -
@CuriousPan Another solution is use QProxyStyle, see https://stackoverflow.com/questions/60947269/pyqt5-qtablewidget-item-alignment/60954464#60954464:
class CheckBoxStyle: public QProxyStyle{ public: using QProxyStyle::QProxyStyle; QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget=nullptr) const{ QRect r = QProxyStyle::subElementRect(element, option, widget); if(element == QStyle::SE_ItemViewItemCheckIndicator){ r.moveCenter(option->rect.center()); } return r; } }
CheckBoxStyle* checkbox_style = new CheckBoxStyle(tableview->style()); tableview->setStyle(checkbox_style);
-
@eyllanesc
this moves every single checkbox in the table to the center... -
@Christian-Ehrlicher, hey, this is a really nice example, but I don't understand what 'role' should I use in the model to center the checkbox, because now it's in the left top corner.
-
@CuriousPan
The code at @Christian-Ehrlicher's https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview tells you what role it is usingAs alignment indicator the
Qt::TextAlignmentRole
is 'misused' since no text is drawn for the cell.and you can see it in the code you are copying:
opt.rect = QStyle::alignedRect(opt.direction, index.data(Qt::TextAlignmentRole).value<Qt::Alignment>(), rect.size(), opt.rect);
Or use your own role if you don't want to use that.
-
@CuriousPan
I do not know about the behaviour of the delegate. But if you want to know to know whether it is being "triggered" and recognises the role in question put in aqDebug()
statement (in the delegate) to find out. -
This post is deleted!
-
@CuriousPan
Find all your occurrences ofQStyle::alignedRect(opt.direction, index.data(Qt::TextAlignmentRole).value<Qt::Alignment>(), rect.size(), opt.rect)
and
qDebug() << index.data(Qt::TextAlignmentRole).value<Qt::Alignment>()
. -
@CuriousPan said in How can I center QCheckBox in QTableView?:
it always retruns QFlagsQt::AlignmentFlag() 1 which is Qt::DecorationRole, right
the
Qt::AlignmentFlag
flags/enum doesnt containQt::DecorationRole
, so no
https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum -
@raven-worx, oh, I see. You're right. Anyway, why is that so?
-
@raven-worx, wait! I confused you and me. This '1' is my custom printout. So what you suggested printing just gives following output:
QFlags<Qt::AlignmentFlag>()
-
@CuriousPan
Qt::AlignLeft 0x0001 Aligns with the left edge.
So this value
1
is left alignment, just as per your picture, and no kind of centering specified, which would have value4
. -
@CuriousPan said in How can I center QCheckBox in QTableView?:
@JonB, it always retruns QFlagsQt::AlignmentFlag() 1 which is Qt::DecorationRole, right?
That's what you wrote. See that
1
? Can you please concentrate on one thing at a time.