How can I center QCheckBox in QTableView?
-
wrote on 9 Nov 2021, 16:28 last edited by
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
-
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
yep, definitely follow the example provided by @Christian-Ehrlicher.
its always better to draw stuff rather than instantiate widgets for such purposes! -
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.wrote on 9 Nov 2021, 20:17 last edited by eyllanesc 11 Sept 2021, 20:19@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
wrote on 12 Nov 2021, 13:28 last edited by@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.
wrote on 12 Nov 2021, 13:47 last edited by JonB 11 Dec 2021, 13:47@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.
wrote on 12 Nov 2021, 13:55 last edited by CuriousPan 11 Dec 2021, 13:55@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 :(
wrote on 12 Nov 2021, 13:57 last edited by JonB 11 Dec 2021, 13:58@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 :(
wrote on 12 Nov 2021, 13:57 last edited byThis 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.wrote on 12 Nov 2021, 13:58 last edited by@JonB, yes, it's defenitely triggered.
-
@JonB, yes, it's defenitely triggered.
wrote on 12 Nov 2021, 14:04 last edited by@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>()
.wrote on 12 Nov 2021, 14:07 last edited by@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-enumwrote on 12 Nov 2021, 14:15 last edited by@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-enumwrote on 12 Nov 2021, 14:20 last edited by@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>()
wrote on 12 Nov 2021, 14:26 last edited by JonB 11 Dec 2021, 14:26@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
.wrote on 12 Nov 2021, 14:28 last edited by@JonB, yeah, it is, but as I said, no numbers are printed.
-
@JonB, it always retruns
QFlags<Qt::AlignmentFlag>() 1
which isQt::DecorationRole
, right?wrote on 12 Nov 2021, 14:29 last edited by JonB 11 Dec 2021, 14:30@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.wrote on 12 Nov 2021, 14:30 last edited by@JonB, yep, I can. I'm really sorry for confusing you. No
1
is printed, it's just my mistake.
7/25