How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell
-
I've replaced all std::strings to QString, and change VS2019 charset to utf, and only use ASCII chars in string.
Still there is a visible blank before the custom tables shows.Even there is only one column, one row.
-
I've tested it with a single drawText call in paintEvent, the problem still exists.
But if I put a label on widget, the problem disappears!
main.cpp#include <QtWidgets/QApplication> #include "QPlainStyleTable.h" #include "QtClass.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // auto _m_pTable = new QPlainStyleTable(); // _m_pTable->SetHeaderColor(Qt::red, Qt::green); // _m_pTable->SetContentColor(Qt::gray, Qt::yellow, Qt::blue); // _m_pTable->SetBorderColor(Qt::white); // //_m_pTable->SetFont(QFont(u8"微软雅黑"), QFont(u8"微软雅黑")); // // _m_pTable->SetDimension(1, 1); // // _m_pTable->SetTitle({ "a", "b", "c", "d", "e" }); // _m_pTable->AddData({ "a1", "b1", "c1", "d1", "e1`" }); // _m_pTable->AddData({ "a2", "b2", "c2", "d2", "e2" }); // _m_pTable->AddData({ "a3", "b3", "c3", "d3", "e3" }); // _m_pTable->AddData({ "a4", "b4", "c4", "d4", "e4" }); // // _m_pTable->show(); auto _m_pQClass = new QtClass(nullptr); _m_pQClass->setFixedSize(500, 400); _m_pQClass->show(); return a.exec(); }
QtClass.h
#pragma once #include <QWidget> class QtClass : public QWidget { Q_OBJECT public: QtClass(QWidget *parent); ~QtClass(); void paintEvent(QPaintEvent* e) override; };
QtClass.cpp with QLabel commented out.
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { // auto _pLabel = new QLabel(this); // _pLabel->setText("HELLO WORLD!"); // _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
QtClass.cpp with QLabel .
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { auto _pLabel = new QLabel(this); _pLabel->setText("HELLO WORLD!"); _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
-
Hi,
Something just came to mind: maybe QStaticText might be of interest.
-
Hi,
Something just came to mind: maybe QStaticText might be of interest.
-
I've no performance problem with your code on linux and windows7 (both in a VM, 64bit, Qt5.15.x)
-
I've tested it with a single drawText call in paintEvent, the problem still exists.
But if I put a label on widget, the problem disappears!
main.cpp#include <QtWidgets/QApplication> #include "QPlainStyleTable.h" #include "QtClass.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // auto _m_pTable = new QPlainStyleTable(); // _m_pTable->SetHeaderColor(Qt::red, Qt::green); // _m_pTable->SetContentColor(Qt::gray, Qt::yellow, Qt::blue); // _m_pTable->SetBorderColor(Qt::white); // //_m_pTable->SetFont(QFont(u8"微软雅黑"), QFont(u8"微软雅黑")); // // _m_pTable->SetDimension(1, 1); // // _m_pTable->SetTitle({ "a", "b", "c", "d", "e" }); // _m_pTable->AddData({ "a1", "b1", "c1", "d1", "e1`" }); // _m_pTable->AddData({ "a2", "b2", "c2", "d2", "e2" }); // _m_pTable->AddData({ "a3", "b3", "c3", "d3", "e3" }); // _m_pTable->AddData({ "a4", "b4", "c4", "d4", "e4" }); // // _m_pTable->show(); auto _m_pQClass = new QtClass(nullptr); _m_pQClass->setFixedSize(500, 400); _m_pQClass->show(); return a.exec(); }
QtClass.h
#pragma once #include <QWidget> class QtClass : public QWidget { Q_OBJECT public: QtClass(QWidget *parent); ~QtClass(); void paintEvent(QPaintEvent* e) override; };
QtClass.cpp with QLabel commented out.
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { // auto _pLabel = new QLabel(this); // _pLabel->setText("HELLO WORLD!"); // _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
QtClass.cpp with QLabel .
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { auto _pLabel = new QLabel(this); _pLabel->setText("HELLO WORLD!"); _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
-
I've no performance problem with your code on linux and windows7 (both in a VM, 64bit, Qt5.15.x)
Hi, Thank you for your attention.
Would you please take a look at the situation below ?
@xce1 said in How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell:
I've tested it with a single drawText call in paintEvent, the problem still exists.
But if I put a label on widget, the problem disappears!
main.cpp#include <QtWidgets/QApplication> #include "QPlainStyleTable.h" #include "QtClass.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // auto _m_pTable = new QPlainStyleTable(); // _m_pTable->SetHeaderColor(Qt::red, Qt::green); // _m_pTable->SetContentColor(Qt::gray, Qt::yellow, Qt::blue); // _m_pTable->SetBorderColor(Qt::white); // //_m_pTable->SetFont(QFont(u8"微软雅黑"), QFont(u8"微软雅黑")); // // _m_pTable->SetDimension(1, 1); // // _m_pTable->SetTitle({ "a", "b", "c", "d", "e" }); // _m_pTable->AddData({ "a1", "b1", "c1", "d1", "e1`" }); // _m_pTable->AddData({ "a2", "b2", "c2", "d2", "e2" }); // _m_pTable->AddData({ "a3", "b3", "c3", "d3", "e3" }); // _m_pTable->AddData({ "a4", "b4", "c4", "d4", "e4" }); // // _m_pTable->show(); auto _m_pQClass = new QtClass(nullptr); _m_pQClass->setFixedSize(500, 400); _m_pQClass->show(); return a.exec(); }
QtClass.h
#pragma once #include <QWidget> class QtClass : public QWidget { Q_OBJECT public: QtClass(QWidget *parent); ~QtClass(); void paintEvent(QPaintEvent* e) override; };
QtClass.cpp with QLabel commented out.
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { // auto _pLabel = new QLabel(this); // _pLabel->setText("HELLO WORLD!"); // _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
QtClass.cpp with QLabel .
#include "QtClass.h" #include <QPainter> #include <QLabel> QtClass::QtClass(QWidget *parent) : QWidget(parent) { auto _pLabel = new QLabel(this); _pLabel->setText("HELLO WORLD!"); _pLabel->move(200, 300); } QtClass::~QtClass() { } void QtClass::paintEvent(QPaintEvent* e) { QPainter painter(this); painter.drawText(0, 0, 100, 200, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextDontClip | Qt::TextWordWrap, QString("Hello World!")); }
-
@xce1 said in How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell:
Would you please take a look at the situation below ?
That's the same code as above.
-
@xce1 said in How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell:
Would you please take a look at the situation below ?
That's the same code as above.
@Christian-Ehrlicher
Yes. It is. I thought maybe you missed the last post. Sorry about that.I used Qt5.14.2 on Win10 VS2019.
Since no one else has the same problem. I'm about to just put a
QLabel
or other Qt widget to bypass it.Thank you anyway.
-
@Christian-Ehrlicher
Yes. It is. I thought maybe you missed the last post. Sorry about that.I used Qt5.14.2 on Win10 VS2019.
Since no one else has the same problem. I'm about to just put a
QLabel
or other Qt widget to bypass it.Thank you anyway.