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.@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);
-
@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... -
You should follow this example: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview
@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.
-
@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
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.
@JonB, yeees, I understood this and I was using
Qt::TextAlignmentRole
and returningQt::AlignCenter
, but for some reason it's not aligned :(
-
@JonB, yeees, I understood this and I was using
Qt::TextAlignmentRole
and returningQt::AlignCenter
, but for some reason it's not aligned :(
@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. -
@JonB, yeees, I understood this and I was using
Qt::TextAlignmentRole
and returningQt::AlignCenter
, but for some reason it's not aligned :(
This post is deleted! -
@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.@JonB, yes, it's defenitely triggered.
-
@JonB, yes, it's defenitely triggered.
@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
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>()
.@JonB, it always retruns
QFlags<Qt::AlignmentFlag>() 1
which isQt::DecorationRole
, right? -
@JonB, it always retruns
QFlags<Qt::AlignmentFlag>() 1
which isQt::DecorationRole
, right?@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 -
@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?
-
@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, wait! I confused you and me. This '1' is my custom printout. So what you suggested printing just gives following output:
QFlags<Qt::AlignmentFlag>()
-
@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
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
.@JonB, yeah, it is, but as I said, no numbers are printed.
-
@JonB, it always retruns
QFlags<Qt::AlignmentFlag>() 1
which isQt::DecorationRole
, right?@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 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.@JonB, yep, I can. I'm really sorry for confusing you. No
1
is printed, it's just my mistake. -
@JonB, yep, I can. I'm really sorry for confusing you. No
1
is printed, it's just my mistake.@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. -
@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.@JonB, I guess you're right, but, as I showed on this screenshot:
I'm using the correct role and column is defenitely correct (checked usingqDebug()
). -
@JonB, I guess you're right, but, as I showed on this screenshot:
I'm using the correct role and column is defenitely correct (checked usingqDebug()
).- 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