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. Trouble Aligning QGraphicsTextItem when drawing it to a Qimage
Forum Updated to NodeBB v4.3 + New Features

Trouble Aligning QGraphicsTextItem when drawing it to a Qimage

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

    I'm taking a QGraphicsTextItem and drawing it to a QImage. I want to draw it directly underneath the QGraphicsTextItem, but am running into problems with the right align flag or combination of flags. Any Suggestions?

    @
    void drawMyText(const QGraphicsTextItem &txtItem)
    {
    QPainter painter(&myQImage);
    painter.setPen(myPen);
    painter.setBrush(myBrush);
    painter.setFont(txtItem.font());
    painter.setTransform(txtItem.sceneTransform());

    // the below is really close but is not directly underneath the text item
    painter.drawText(txtItem.boundingRect(), Qt::AlignCenter, txtItem.toPlainText());

    }

    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jh224
      wrote on last edited by
      #2

      I found the right combination for my situation was combining AlignCenter with AlignJustify as shown below.

      @ void drawMyText(const QGraphicsTextItem &txtItem)
      {
      QPainter painter(&myQImage);
      painter.setPen(myPen);
      painter.setBrush(myBrush);
      painter.setFont(txtItem.font());
      painter.setTransform(txtItem.sceneTransform());

      // AlignCenter and AlignJustify works for me.
      painter.drawText(txtItem.boundingRect(), Qt::AlignCenter | Qt::AlignJustify , txtItem.toPlainText());
       
      }
      

      @

      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