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. TableView Spinbox HAlignment

TableView Spinbox HAlignment

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 592 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hi,

    I have a table view to which I provide values as doubles and I get the following.
    4c8f4cf1-0b36-4936-bd08-d8f60006eaca-image.png

    I would like the values centered in the column. So I created a new delegate and attached it to the TableView.

    a2159fc6-63e3-4fcd-98c6-7e1dfff81a59-image.png

    Now when I edit the values they are centered in the Spinbox, but when they are not edited they are not.

    Why are the values only centered in edit mode and how can I keep the values centered?

    eyllanescE 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bert59
      wrote on last edited by
      #4

      Hi

      I have written the following:
      model->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);

      and it works.

      Thank you

      1 Reply Last reply
      0
      • B Bert59

        Hi,

        I have a table view to which I provide values as doubles and I get the following.
        4c8f4cf1-0b36-4936-bd08-d8f60006eaca-image.png

        I would like the values centered in the column. So I created a new delegate and attached it to the TableView.

        a2159fc6-63e3-4fcd-98c6-7e1dfff81a59-image.png

        Now when I edit the values they are centered in the Spinbox, but when they are not edited they are not.

        Why are the values only centered in edit mode and how can I keep the values centered?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by eyllanesc
        #2

        @Bert59 please provide a MWE(minimal workable example)

        It seems that your delegate only sets the editor alignment but not the unedited text, a possible solution is to override the initStyleOption method:

        class StyledItemDelegate: public QStyledItemDelegate{
        public:
           using QStyledItemDelegate::QStyledItemDelegate;
        protected:
            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
                QStyledItemDelegate::initStyleOption(option, index);
                option->displayAlignment = Qt::AlignCenter;
            } 
        };
        

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Bert59 said in TableView Spinbox HAlignment:

          but when they are not edited they are not.

          You should take a look at the Qt::TextAlignmentRole

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bert59
            wrote on last edited by
            #4

            Hi

            I have written the following:
            model->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);

            and it works.

            Thank you

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bert59
              wrote on last edited by
              #5

              I just have an additionnal question.

              How can I make the Spinbox to be displayed even if not in edition mode?

              JonBJ 1 Reply Last reply
              0
              • B Offline
                B Offline
                Bert59
                wrote on last edited by
                #6

                I found openPersistenEditor which seems to work.

                Maybe there are othe solutions?

                1 Reply Last reply
                0
                • B Bert59

                  I just have an additionnal question.

                  How can I make the Spinbox to be displayed even if not in edition mode?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @Bert59 said in TableView Spinbox HAlignment:

                  How can I make the Spinbox to be displayed even if not in edition mode?

                  Through a QStyledItemDelegate.

                  1 Reply Last reply
                  0

                  • Login

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