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. [~Solved] Set Alignment in QGraphicsTextItem?
Qt 6.11 is out! See what's new in the release blog

[~Solved] Set Alignment in QGraphicsTextItem?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 7.1k 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    How do I properly align a multi-line text in a QGraphicsTextItem?
    The text seems to be always left-aligned, despite specifying

    @ QTextOption option = pTextItem->document()->defaultTextOption();
    option.setAlignment(Qt::AlignCenter);
    pTextItem->document()->setDefaultTextOption(option);@

    I have also tried assigning an alignment to all text blocks - no effect.

    Ideas?

    Edit: I only use plain text. I have found "this Bug":https://bugreports.qt-project.org/browse/QTBUG-312 regarding html text.

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

      Hi,

      Just a question, do you set the option before or after setting the text ?

      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
        Asperamanca
        wrote on last edited by
        #3

        I've tried setting the default text alignment

        • when the object is created (when text is still empty)
        • on every contentsChanged() signal

        I've tried setting the text alignment for all text blocks

        • whenver the block count changes

        Any other places I should try?

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

          I would also do it in the constructor. As a workaround, did you try to resize the item as suggested in the bug report ?

          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
            Asperamanca
            wrote on last edited by
            #5

            It seems, the alignment only works if the document's textWidth is set.

            I expected it to work without the text width, based on the length of the longest text line.

            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