0

i have a program that needs to paint some text with paintEvent(). i've tried this:

1. QPainter painter; // painter for the canvas
2. painter.drawText(QPoint location, QString canvasText);

where

3. QString canvasText = variablesText.append("< b >");
4. variablesText.append((*fieldIter).second.c_str());
5. variablesText.append(":< /b > ");
6. variablesText.append(someValue);
7. variablesText.append("\n");

I need the text to be formated, canvasText should look like:

Some bold text: some not bold text. (newLine)

Some bold text 2: some not bold text2. (newLine) and that goes on for a while.

The problem i'm having is that a QString can't have HTML code in it, so the text is displayed like:

< b>Some bold text:< /b> some not bold text. < b>Some bold text 2: < /b>
some not bold text2.\n

Is there a way to use draw in paintEvent to show the text the way i need it? with a QString (or QLabel or something)

I'm using Qt4.

Thanks for the help =)

acerqueiro
  • 29
  • 5

2 Answers2

3

QTextDocument seems like a good fit for what you're after, specifically setHtml(). A QTextDocument can format your text and paint the result to the screen via your widget's QPainter. Something like the below is the simplest possible solution:

void Test::paintEvent(QPaintEvent *)
{
  QPainter painter(this);

  QTextDocument doc;
  doc.setHtml("<b>Title</b><p>Body Text</p>");

  doc.drawContents(&painter, rect());
}

Bear in mind, however, that this is likely to be very inefficient. You'll probably want to drawContents() to a cached QPixmap only when your source html changes...

sam-w
  • 7,478
  • 1
  • 47
  • 77
  • Thanks @sjwarner, however, i need to give it an specific location to where it needs to be drawn I did this `document->drawContents(&painter, QRect(QPoint.x(), QPoint.y(), rect().width(), rect().height()));` but it doesn't show anything. – acerqueiro Jul 31 '12 at 16:54
  • @sjtaheri seems to have got you on the right track, so I'll leave you to it ;-) – sam-w Aug 01 '12 at 09:37
2

Suppose you are trying to print HTML formatted text contents in str on QRect rect via QPainter painter. Do as follows:

QString str; // this string contains HTML formatted contents 
QTextDocument * document = new QTextDocument(this) ;
document->setHtml(str) ;

painter.translate( rect.left() , rect.top() ) ;
document->drawContents( &painter , QRect( 0 , 0,  rect.width() , rect.height() ) ;
sjtaheri
  • 4,409
  • 3
  • 19
  • 15
  • Thanks @sjtaheri! that works just fine, but i need to give it an specific location that i have stored in a QList. I did this `document->drawContents(&painter, QRect(QPoint.x(), QPoint.y(), rect().width(), rect().height()));` but it doesn't show anything. If i use numbers like `document->drawContents(&painter, QRect(20, 20, rect().width(), rect().height()));` it shows the rect but trimmed. Any thoughts? – acerqueiro Jul 31 '12 at 16:52
  • For every point, first, translate it using painter::translate method with point's x and y position as arguments. Then draw the document by calling document->drawContents( &painter , QRect( 0 , 0, width , height ). width and height should be large enough so the whole text fit in. Finally restore the painter using QPainter::restore method. Repeat this procedure for all points. – sjtaheri Jul 31 '12 at 18:13
  • Thanks again @sjtaheri =D I tried that, but it won't show the rest of the qpixmaps i have on the painter, not even if I do the restore :S this is my code: `for (int i = 0; i < pieceRects.size(); ++i) { painter.drawPixmap(pieceRects[i], iconList[i]->getIcon()); QTextDocument *document = new QTextDocument(this); document->setHtml(iconList[i]->getVariablesText()); painter.translate(iconList[i]->getVariablesLocation()); document->drawContents(&painter, rect()); painter.restore();}` The qpixmap is invisible, if i start clicking, i eventually grab the qpixmap but when i release it fades again. =( – acerqueiro Jul 31 '12 at 19:00