Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Tryin to understand how QTextDocument works, have a question
Forum Updated to NodeBB v4.3 + New Features

Tryin to understand how QTextDocument works, have a question

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 2.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.
  • M Offline
    M Offline
    mmesarina
    wrote on last edited by
    #1

    Hi,

    In the code below, what I wanted to do is to use QTextDocument to put some text inside a rectangle.
    The documentation states that the method "drawContents ( QPainter * p, const QRectF & rect = QRectF() )" will use painter to paint the text inside the rectangle.

    But in my code the text is being placed one half outside rectangle and one half inside the rectangle.
    Can someone spot what I am doing wrong?

    Here is the code. I created a widget called "DocWidget" which I set as the centralWidget in the MainWindow constructor. So when I instantiate DocWidget, the paintEvent gets called.

    thanks

    -Malena

    @
    void DocWidget::paintEvent(QPaintEvent *pe)
    {
    QPainter *painter = new QPainter(this);

    //Color: #333
    QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine);
    
    QRectF rectangle(10.0, 20.0, 150.0, 60.0);
    
    painter->drawRoundedRect(rectangle, 20.0, 15.0);
    
    painter->setPen(fontPen);
    QTextDocument document;
    document.setHtml("<p>HELLO</p>");
    
    document.drawContents( painter, rectangle );
    

    }
    @

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zaphod
      wrote on last edited by
      #2

      Hi Malena,

      to my knowledge, the rectangle passed to drawContents() describes which part of the TextDocument is actually to be drawn.

      To define WHERE to draw it, you must use translate():

      @
      painter->save();
      painter->translate(10, 20);
      document.drawContents(painter, QRect(0,0, 150, 60));
      painter->restore();
      @

      To do clipping, you need to set up an appropriate clipping region in your QPainter.

      Hope this works for you.

      -zaphod

      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