I can't believe that such a big difference in speed has to do with different fonts.
I've made a little program so you can try it yourself:
main.cpp:
#include <QApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
Widget widget;
widget.show();
return app.exec();
}
#include <QApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget widget;
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
{
protected:
};
#endif
#ifndef WIDGET_H
#define WIDGET_H
class QPaintEvent;
#include <QWidget>
class Widget : public QWidget
{
protected:
virtual void paintEvent(QPaintEvent* event);
};
#endif
To copy to clipboard, switch view to plain text mode
Widget.cpp
#include "Widget.h"
#include <QtGui>
{
lines << "safsdafsdaf asdflkj a sdfjöl akj " \
<< "asdflkj asdföjl asdf asdkj ösa" \
<< "asödlfjk lkajs dfa asdfjö asöld föl" \
<< "saödlfkj lkj asdf asdfkj klsjd f " \
<< "asdölfkj lökj asdfasdf sakdj" \
<< "asödlfkj ölkj asdf ajsklö fs" \
<< "asdöflkj lökj ölkasjdföalkj ölkj asdf";
font.setPointSize(30);
int lineHeight = fontMetrics.height();
int x = 10;
int y = lineHeight;
{
path.addText(x, y, font, line);
y += lineHeight;
}
QPen pen
(QColor("#000000"),
0.5, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
p.
setRenderHint(QPainter::Antialiasing);
p.setPen(pen);
p.setBrush(brush);
p.setFont(font);
time.start();
p.drawPath(path);
qDebug() << "Time elapsed for drawing:" << time.elapsed() << "ms.";
}
#include "Widget.h"
#include <QtGui>
void Widget::paintEvent(QPaintEvent* /* event */)
{
QStringList lines;
lines << "safsdafsdaf asdflkj a sdfjöl akj " \
<< "asdflkj asdföjl asdf asdkj ösa" \
<< "asödlfjk lkajs dfa asdfjö asöld föl" \
<< "saödlfkj lkj asdf asdfkj klsjd f " \
<< "asdölfkj lökj asdfasdf sakdj" \
<< "asödlfkj ölkj asdf ajsklö fs" \
<< "asdöflkj lökj ölkasjdföalkj ölkj asdf";
QFont font;
font.setPointSize(30);
QPainter p(this);
QFontMetrics fontMetrics(font, p.device());
int lineHeight = fontMetrics.height();
int x = 10;
int y = lineHeight;
QPainterPath path;
foreach(QString line, lines)
{
path.addText(x, y, font, line);
y += lineHeight;
}
QBrush brush(QColor("#FFFFFF"));
QPen pen(QColor("#000000"), 0.5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(pen);
p.setBrush(brush);
p.setFont(font);
QTime time;
time.start();
p.drawPath(path);
qDebug() << "Time elapsed for drawing:" << time.elapsed() << "ms.";
}
To copy to clipboard, switch view to plain text mode
Either there is another, faster way to draw antialiased, outlined text on Windows with Qt or it needs to be improved... If there isn't another way I would even try to use some text rendering outside of Qt. Is there some free (GPL compatible) library or so?
Bookmarks