Results 1 to 7 of 7

Thread: How to write rich text at a specific position given a const QPainter object?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default How to write rich text at a specific position given a const QPainter object?

    I want to create a class method for drawing rich text at a specific position with a specific QPainter object:

    Qt Code:
    1. void MyWidget::drawRichText(QString html, QPointF position, const QPainter* qp);
    To copy to clipboard, switch view to plain text mode 

    How do I implement this method? The solution I was able to come up with by searching on Google was the following:

    Qt Code:
    1. void MyWidget::drawRichText(QString html, QPointF position, QPainter* qp)
    2. {
    3. td.setHtml(html);
    4. qp->translate(position); // Move the painter to the desired location
    5. td.drawContents(qp);
    6. qp->translate(-position); // Move the painter back to where it was before
    7. }
    To copy to clipboard, switch view to plain text mode 

    which I already find quite distasteful as I make changes to a painter object which may be used after me (which is why I discretely move the painter back after I have used it, but I realize that the more changes I make to it that I don't want to be permanent, the greater the risk is that I will forget to change some of them back). I cannot make a personal copy of it either, since QPainter uses the Q_DISABLE_COPY macro.

    Actually, the proposed solution won't even work in my case since I need to pass qp as const, and the translate method cannot be called on const QPainter objects.

    (Besides, I realized that if someone has already moved the pointer before me, the pointer will move to the wrong location. But maybe that's a secondary issue. I don't know. Anyway...)

    So, how do I implement this function assuming that the QPainter object is declared const (and is located at (0, 0) when the method is called)?
    Last edited by TriKri; 7th October 2016 at 18:13.

Similar Threads

  1. Replies: 3
    Last Post: 19th January 2016, 18:22
  2. Positioning Rich Text when using QPainter
    By millsks in forum Qt Programming
    Replies: 8
    Last Post: 23rd June 2009, 08:21
  3. Replies: 13
    Last Post: 29th April 2009, 16:51
  4. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 20:59
  5. Rich text with QPainter?
    By sarefo in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2008, 15:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.