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 set QRect in the center of cell in QListVIew with QAbstractItemDelegate's paint?

How to set QRect in the center of cell in QListVIew with QAbstractItemDelegate's paint?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 2.9k 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.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #1

    Hello

    I have QListVIew and delegate to paint the list view. I paint some text int the center of cell.
    so I do it:
    @
    void Delegate::paint(QPainter *painter, const QStyledOptionViewItem &option, const QModelIndex &index )
    {
    .
    .
    .
    QRect textRect(option.rect.center(),QSize(option.rect.width(),option.rect.height());

    paiter->drawText(textRect,text,QTextOption());
    @

    but it starts to paint from the center. how can I center this output?
    thank you

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Would it not be easier to just set the right text option? Center alignment is a standard option...

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tokafr
        wrote on last edited by
        #3

        I tried moveCenter and why it doesn't do it?
        @
        QRect textName(option.rect.topLeft(),QSize(option.rect.width(),option.rect.height());

        textName.moveCenter(option.rect.center());
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Because moving the center of a rect does not center text you render in that rect.

          Try this instead:
          @
          paiter->drawText(option.rect(), Qt::AlignCenter, text );
          @

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tokafr
            wrote on last edited by
            #5

            what if the text is longer than the width of cell?

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

              Hi,

              Use the Qt::TextWordWrap option

              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
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="tokafr" date="1422290145"]what if the text is longer than the width of cell?
                [/quote]

                Either word wrap, or elide the text, or determine that it is too wide, and dynamically find a font size that will fit (within reasonable limits, of course).

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tokafr
                  wrote on last edited by
                  #8

                  Thank you all!!!

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tokafr
                    wrote on last edited by
                    #9

                    and how can I set the text in the top but aligned in the center( so topCenter)?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="tokafr" date="1422350579"]and how can I set the text in the top but aligned in the center( so topCenter)?
                      [/quote]

                      Did you have a look at the documentation for the function that I used in my code? It lists all the possible flags you can pass...

                      1 Reply Last reply
                      0
                      • ? This user is from outside of this forum
                        ? This user is from outside of this forum
                        Guest
                        wrote on last edited by
                        #11
                        This post is deleted!
                        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