How can I center QCheckBox in QTableView?
-
@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. -
@CuriousPan
So that would be the value for left-align. Either you are not setting or returning the desired "center" flag on your item for theQt::TextAlignmentRole
--- you have to do that if you want it centered, that's what the code does --- or you are not looking at the right item/index. -
- Print out
index.column()
in the delegate. - Print out
RecurrentColumn
. - Put a
qDebug()
just abovereturn Qt::AlignCenter
in your "screenshot" to make sure it's being hit. - Please don't paste code as screenshots, paste it as code. Show where you have that code and what is above the extract you show.
- Print out