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 to change the position of the text of a spanned Cell in a QTableView
Forum Updated to NodeBB v4.3 + New Features

How to change the position of the text of a spanned Cell in a QTableView

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 3.2k 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.
  • G Offline
    G Offline
    geracmos
    wrote on last edited by
    #1

    I have a QTableView with big cells (I use span). The cells are so big, that the text of them is not visible unless I reach the middle of them with the scrollbars. So I need to change the position of the text of a cell.

    I think that I have 2 options here.

    1. Create QLabels and set them somehow over the QTableview at certain positions (How to do that???)

    2. Since I have span, can I place the cell's text not at the center of the cell, but at a certain position that I want?

    Thanks in advance!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      I think you have two options:

      1.) give the cell an alignment
      2.) write a QStyledItemDelegate derived class

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        geracmos
        wrote on last edited by
        #3

        1st option doesn't look good since I want the text to be placed at the center of the visible part of a cell.

        As for the 2nd can you give me some more info about how to do that?
        I suppose that I have to reimplement paint(), but how could I set the text position there?

        And getting Labels over the QTableView is impossible?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          I don't know how the widgets are positioned, but adding widgets is possible.
          Yes, you have to reimplement paint of the delegate, how to find the position, I don't know. I never did such stuff.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NicuPopescu
            wrote on last edited by
            #5

            if you only need static content then QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget ) would be enough ... even in this case you should use a subclass of QLabel and reimplement paintEvent in order to draw text at a certain position

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              If you reimplement paint, you can even use a QWidget subclass as painting there is the same effort, you only need an additional property text.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                geracmos
                wrote on last edited by
                #7

                Is there an example to help me with that?

                Lets say that I only have one row that has rowSpan=4 and I want its text to be shown at the index=(2,0)
                Something like that:

                [..........]
                [..text..]
                [..........]
                [..........]

                Is there really no other way to change the current cells text position, except for setting an alignment?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  geracmos
                  wrote on last edited by
                  #8

                  Labels worked for me, but I think just changing the position of the cells' text would be a much more efficient implementation.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    I think you would have to modify the option passed to your paintEvent and setup the rectangle where the text is drawn, then call the base implementation.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NicuPopescu
                      wrote on last edited by
                      #10

                      Hi,

                      beside setting item's widget and QStyledItemDelegate and overriding their painting methods, I think a style sheet is also an option, and by ::item and padding you can change the text's position: afaik testing ::item in designer does not work but at runtime would not be a problem as I use them a lot

                      with QStyledItemDelegate you can use option.rect directly in paint(), in widget's paintEvent accessing style option is trickier

                      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