I want to create a class method for drawing rich text at a specific position with a specific QPainter object:
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:
{
td.setHtml(html);
qp->translate(position); // Move the painter to the desired location
td.drawContents(qp);
qp->translate(-position); // Move the painter back to where it was before
}
void MyWidget::drawRichText(QString html, QPointF position, QPainter* qp)
{
QTextDocument td;
td.setHtml(html);
qp->translate(position); // Move the painter to the desired location
td.drawContents(qp);
qp->translate(-position); // Move the painter back to where it was before
}
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)?
Bookmarks