Here is a full example using rich text of QTextDocument.
mainWindow.cpp:
#include "mainWindow.h"
void MainWindow::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QTextDocument td;
td.setHtml("K<sub>max</sub>=K<sub>2</sub> · 3");
td.drawContents(&painter);
}
If you need to draw the text at specific point, translate the coordinate system of the painter before drawing:
painter.translate(QPointF(50, 50));
mainWindow.cpp - Another solution:
#include "mainWindow.h"
void MainWindow::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QTextDocument td;
td.setHtml("K<sub>max</sub>=K<sub>2</sub> · 3");
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.clip = QRectF( 0, 0, 400, 100 );
td.documentLayout()->draw( &painter, ctx );
}
mainWindow.h:
#include <QtGui>
class MainWindow: public QWidget
{
protected:
void paintEvent(QPaintEvent*);
};
main.cpp:
#include <QtGui>
#include "mainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
The project file:
TEMPLATE = app
QT += gui
HEADERS = mainWindow.h
SOURCES = main.cpp mainWindow.cpp
Result:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…