Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I center QCheckBox in QTableView?
Forum Updated to NodeBB v4.3 + New Features

How can I center QCheckBox in QTableView?

Scheduled Pinned Locked Moved Solved General and Desktop
qtableview
25 Posts 5 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Online
    C Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 9 Nov 2021, 16:31 last edited by
    #2

    You should follow this example: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    C 1 Reply Last reply 12 Nov 2021, 13:28
    4
    • C CuriousPan
      9 Nov 2021, 16:28

      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.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 9 Nov 2021, 16:49 last edited by
      #3

      @CuriousPan
      yep, definitely follow the example provided by @Christian-Ehrlicher.
      its always better to draw stuff rather than instantiate widgets for such purposes!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • C CuriousPan
        9 Nov 2021, 16:28

        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.

        E Offline
        E Offline
        eyllanesc
        wrote on 9 Nov 2021, 20:17 last edited by eyllanesc 11 Sept 2021, 20:19
        #4

        @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);
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        R 1 Reply Last reply 9 Nov 2021, 20:33
        1
        • E eyllanesc
          9 Nov 2021, 20:17

          @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);
          
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 9 Nov 2021, 20:33 last edited by
          #5

          @eyllanesc
          this moves every single checkbox in the table to the center...

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • C Christian Ehrlicher
            9 Nov 2021, 16:31

            You should follow this example: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview

            C Offline
            C Offline
            CuriousPan
            wrote on 12 Nov 2021, 13:28 last edited by
            #6

            @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.

            J 1 Reply Last reply 12 Nov 2021, 13:47
            0
            • C CuriousPan
              12 Nov 2021, 13:28

              @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.

              J Offline
              J Offline
              JonB
              wrote on 12 Nov 2021, 13:47 last edited by JonB 11 Dec 2021, 13:47
              #7

              @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 using

              As 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.

              C 1 Reply Last reply 12 Nov 2021, 13:55
              1
              • J JonB
                12 Nov 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 using

                As 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.

                C Offline
                C Offline
                CuriousPan
                wrote on 12 Nov 2021, 13:55 last edited by CuriousPan 11 Dec 2021, 13:55
                #8

                @JonB, yeees, I understood this and I was using Qt::TextAlignmentRole and returning Qt::AlignCenter, but for some reason it's not aligned :(
                21cc5073-d844-4a87-ac13-f40a60c0cea3-image.png

                3076c24a-05b5-476e-9154-09e658caebab-image.png

                J C 2 Replies Last reply 12 Nov 2021, 13:57
                0
                • C CuriousPan
                  12 Nov 2021, 13:55

                  @JonB, yeees, I understood this and I was using Qt::TextAlignmentRole and returning Qt::AlignCenter, but for some reason it's not aligned :(
                  21cc5073-d844-4a87-ac13-f40a60c0cea3-image.png

                  3076c24a-05b5-476e-9154-09e658caebab-image.png

                  J Offline
                  J Offline
                  JonB
                  wrote on 12 Nov 2021, 13:57 last edited by JonB 11 Dec 2021, 13:58
                  #9

                  @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 a qDebug() statement (in the delegate) to find out.

                  C 1 Reply Last reply 12 Nov 2021, 13:58
                  0
                  • C CuriousPan
                    12 Nov 2021, 13:55

                    @JonB, yeees, I understood this and I was using Qt::TextAlignmentRole and returning Qt::AlignCenter, but for some reason it's not aligned :(
                    21cc5073-d844-4a87-ac13-f40a60c0cea3-image.png

                    3076c24a-05b5-476e-9154-09e658caebab-image.png

                    C Offline
                    C Offline
                    CuriousPan
                    wrote on 12 Nov 2021, 13:57 last edited by
                    #10
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • J JonB
                      12 Nov 2021, 13:57

                      @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 a qDebug() statement (in the delegate) to find out.

                      C Offline
                      C Offline
                      CuriousPan
                      wrote on 12 Nov 2021, 13:58 last edited by
                      #11

                      @JonB, yes, it's defenitely triggered.

                      J 1 Reply Last reply 12 Nov 2021, 14:04
                      0
                      • C CuriousPan
                        12 Nov 2021, 13:58

                        @JonB, yes, it's defenitely triggered.

                        J Offline
                        J Offline
                        JonB
                        wrote on 12 Nov 2021, 14:04 last edited by
                        #12

                        @CuriousPan
                        Find all your occurrences of

                        QStyle::alignedRect(opt.direction, index.data(Qt::TextAlignmentRole).value<Qt::Alignment>(), rect.size(), opt.rect)
                        

                        and qDebug() << index.data(Qt::TextAlignmentRole).value<Qt::Alignment>().

                        C 1 Reply Last reply 12 Nov 2021, 14:07
                        0
                        • J JonB
                          12 Nov 2021, 14:04

                          @CuriousPan
                          Find all your occurrences of

                          QStyle::alignedRect(opt.direction, index.data(Qt::TextAlignmentRole).value<Qt::Alignment>(), rect.size(), opt.rect)
                          

                          and qDebug() << index.data(Qt::TextAlignmentRole).value<Qt::Alignment>().

                          C Offline
                          C Offline
                          CuriousPan
                          wrote on 12 Nov 2021, 14:07 last edited by
                          #13

                          @JonB, it always retruns QFlags<Qt::AlignmentFlag>() 1 which is Qt::DecorationRole, right?

                          R J 2 Replies Last reply 12 Nov 2021, 14:12
                          0
                          • C CuriousPan
                            12 Nov 2021, 14:07

                            @JonB, it always retruns QFlags<Qt::AlignmentFlag>() 1 which is Qt::DecorationRole, right?

                            R Offline
                            R Offline
                            raven-worx
                            Moderators
                            wrote on 12 Nov 2021, 14:12 last edited by
                            #14

                            @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 contain Qt::DecorationRole, so no
                            https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            C 2 Replies Last reply 12 Nov 2021, 14:15
                            0
                            • R raven-worx
                              12 Nov 2021, 14:12

                              @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 contain Qt::DecorationRole, so no
                              https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum

                              C Offline
                              C Offline
                              CuriousPan
                              wrote on 12 Nov 2021, 14:15 last edited by
                              #15

                              @raven-worx, oh, I see. You're right. Anyway, why is that so?

                              1 Reply Last reply
                              0
                              • R raven-worx
                                12 Nov 2021, 14:12

                                @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 contain Qt::DecorationRole, so no
                                https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum

                                C Offline
                                C Offline
                                CuriousPan
                                wrote on 12 Nov 2021, 14:20 last edited by
                                #16

                                @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>()

                                J 1 Reply Last reply 12 Nov 2021, 14:26
                                0
                                • C CuriousPan
                                  12 Nov 2021, 14:20

                                  @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>()

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 12 Nov 2021, 14:26 last edited by JonB 11 Dec 2021, 14:26
                                  #17

                                  @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 value 4.

                                  C 1 Reply Last reply 12 Nov 2021, 14:28
                                  0
                                  • J JonB
                                    12 Nov 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 value 4.

                                    C Offline
                                    C Offline
                                    CuriousPan
                                    wrote on 12 Nov 2021, 14:28 last edited by
                                    #18

                                    @JonB, yeah, it is, but as I said, no numbers are printed.
                                    f3a89a98-7f28-4581-955b-721b64fbfc0e-image.png

                                    1 Reply Last reply
                                    0
                                    • C CuriousPan
                                      12 Nov 2021, 14:07

                                      @JonB, it always retruns QFlags<Qt::AlignmentFlag>() 1 which is Qt::DecorationRole, right?

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 12 Nov 2021, 14:29 last edited by JonB 11 Dec 2021, 14:30
                                      #19

                                      @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.

                                      C 1 Reply Last reply 12 Nov 2021, 14:30
                                      0
                                      • J JonB
                                        12 Nov 2021, 14:29

                                        @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.

                                        C Offline
                                        C Offline
                                        CuriousPan
                                        wrote on 12 Nov 2021, 14:30 last edited by
                                        #20

                                        @JonB, yep, I can. I'm really sorry for confusing you. No 1 is printed, it's just my mistake.

                                        J 1 Reply Last reply 12 Nov 2021, 14:34
                                        0
                                        • C CuriousPan
                                          12 Nov 2021, 14:30

                                          @JonB, yep, I can. I'm really sorry for confusing you. No 1 is printed, it's just my mistake.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 12 Nov 2021, 14:34 last edited by
                                          #21

                                          @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 the Qt::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.

                                          C 1 Reply Last reply 12 Nov 2021, 14:41
                                          0

                                          11/25

                                          12 Nov 2021, 13:58

                                          • Login

                                          • Login or register to search.
                                          11 out of 25
                                          • First post
                                            11/25
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved