Unsolved QTBUG-54180 havn't been resolved in 5.9
-
In QTBUG-54180 described a bug about "QPainter drawText() combined with setFont() very slow". The Link is https://bugreports.qt.io/browse/QTBUG-54180.
I think this bug havn't been resolved. If somebody can call the bug reporter Chris, please ask him the solution with this bug for me.
I use VS2015 and Qt5.9.0. When use QPainter drawText() combined with setFont(), I found it very slow at the first time after the widget new. The function of drawText() costs 300ms. Besides, I delete the widget, then new another one, and drawText() with setFont(). It costs longer and longer time.
To resolve this problem. I test Qt5.9.6, Qt5.9.7, Qt5.9.0, Qt5.6.2, Qt5.6.3 and Qt5.6.0. But no one can do it as better as MFC.
Qt5.6 is better then Qt5.9.
Who can help me? Thanks very much! -
The fix for QTBUG-54180 is contained in 5.9 and all upward versions. So I think your problem is something else.
Can you show us your code?
-
This post is deleted! -
just an example code.
//// widget.h/////////////////////////
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QFont>
#include <QPen>
#include <QPaintEvent>namespace Ui {
class Widget;
}class Widget : public QWidget
{
Q_OBJECTpublic:
explicit Widget(QWidget *parent = nullptr);
~Widget();private:
QString m_s1;
QString m_s2;
QString m_s3;
QString m_s4;QFont m_Font; QPen m_Pen;
private:
Ui::Widget *ui;protected:
void resizeEvent(QResizeEvent* event);
virtual void paintEvent(QPaintEvent* event);
};#endif // WIDGET_H
///////////////////widget.cpp///////////////////////
#include "widget.h"
#include "ui_widget.h"
#include <QtFontDatabaseSupport/QtFontDatabaseSupport>Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QString fontFile = "C:/Windows/Fonts/msyh.ttf";
int fontID = QFontDatabase::addApplicationFont(fontFile);
QString msyh = QFontDatabase::applicationFontFamilies(fontID).at(0);
m_Font = QFont(msyh);
m_Pen.setColor(QColor(255,0,0));
m_s1 = "test1";
m_s2 = "test2";
m_s3 = "test3";
m_s4 = "test4";
}Widget::~Widget()
{
delete ui;
}void Widget::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
QRect rc = this->rect();
int iFontSize = rc.width() * 0.01 + 10;
m_Font.setPixelSize(iFontSize);
update();
}
void Widget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setPen(m_Pen);
painter.setFont(m_Font);
QRect painterRect = painter.window();
QTime t1;
t1.start();
painter.drawText(painterRect, Qt::AlignLeft | Qt::AlignVCenter, m_s1);
painter.drawText(painterRect, Qt::AlignRight | Qt::AlignVCenter, m_s2);
painter.drawText(painterRect, Qt::AlignBottom | Qt::AlignHCenter, m_s3);
painter.drawText(painterRect, Qt::AlignTop | Qt::AlignHCenter, m_s4);
qDebug() << t1.elapsed();
}////////////////////main.cpp//////////////////////////////////////////
#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();return a.exec();
}
-
please help me
-
Fact is that it is slow. You have following options.
- If the text what you are drawing is static(remains same always), then you can draw the text on the Pixmap and draw the pixmap in paintEvent(..). This way it will be one time performance issue.
- Using the GraphicsView & drawing the text may improve the performance
- If the bug fixed, you need to have that patch. It should work for you.
-
@Henry-Li said in QTBUG-54180 havn't been resolved in 5.9:
please help me
Please submit a report to https://bugreports.qt.io/.
In your report, include your test code and benchmarks (How fast was it in Qt 5.6? How fast was it in Qt 5.11?)
Note: The Qt developers will be more likely to investigate if you show that Qt 5.11 is slower than Qt 5.6.
-
@dheerendra Thanks very much. I will try the options. They are very helpful I thought.
-
@JKSH Yes, I want to report it. But for now, I have to do my work first. And mybe later, I will submit the porblem with more test code.
-
What I have noticed is that drawText(..) for the first time is slow. After that it is very fast. As work-around, you can try like follows. Add one dummy method drawMe() like this. Call this method once before you call show() method. So now drawText(..) inside paintEvent(..) will be fast. It hardly takes anytime. So performance issue is only at startup. After that it is nothing/negligible.
void MyWidget::drawMe() { pix = QPixmap(400,400); QPainter painter(&pix); QTime t1; t1.start(); QRect r2(100,100,100,100); painter.drawText(r2, Qt::AlignRight, "pthinks.com"); qDebug() << Q_FUNC_INFO << "Time Taken to Draw " << t1.elapsed(); } // === main.cpp ====== MyWidget w; w.drawMe(); w.show();
-
@dheerendra
Thank you. Your solution is good. But don't resolved my problem absolutely. My project is complicated and high-performance request. I still hope Qt itself can do it better.
Thank you again. -
Hi @Henry-L,
as said before, please comment at the bugreport if you want an improvement.
The discussion here will be forgotten in a couple of days.