Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell

How can I replce Qpainter::drawText with a more effecient way to draw text in a table cell

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 6 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xce1
    wrote on last edited by
    #9

    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!"));
    }
    

    test4.gif

    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!"));
    }
    

    test5.gif

    X 1 Reply Last reply
    0
    • JonBJ JonB

      @xce1
      Your post is hurting my eyes! :( :)

      X Offline
      X Offline
      xce1
      wrote on last edited by
      #10

      @JonB Sorry about that. I've removed the colorful gifs.

      @jsulm Perhaps It's not drawText problem.
      To better demonstrate the situation, I've upload two new comparable gray gifs with a simpler QWidget subclass.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #11

        Hi,

        Something just came to mind: maybe QStaticText might be of interest.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        X 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Something just came to mind: maybe QStaticText might be of interest.

          X Offline
          X Offline
          xce1
          wrote on last edited by
          #12

          @SGaist Hi, thanks. I just tried that, It's still the same.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #13

            I've no performance problem with your code on linux and windows7 (both in a VM, 64bit, Qt5.15.x)

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            X 1 Reply Last reply
            0
            • X xce1

              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!"));
              }
              

              test4.gif

              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!"));
              }
              

              test5.gif

              X Offline
              X Offline
              xce1
              wrote on last edited by
              #14
              This post is deleted!
              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                I've no performance problem with your code on linux and windows7 (both in a VM, 64bit, Qt5.15.x)

                X Offline
                X Offline
                xce1
                wrote on last edited by
                #15

                @Christian-Ehrlicher

                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!"));
                }
                

                test4.gif

                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!"));
                }
                

                test5.gif

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #16

                  @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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  X 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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.

                    X Offline
                    X Offline
                    xce1
                    wrote on last edited by
                    #17

                    @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.

                    J.HilkJ 1 Reply Last reply
                    0
                    • X xce1

                      @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.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #18

                      @xce1 have you tried a release build with O3 ? maybe its just a debug/development issue


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      X 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @xce1 have you tried a release build with O3 ? maybe its just a debug/development issue

                        X Offline
                        X Offline
                        xce1
                        wrote on last edited by
                        #19

                        @J-Hilk I just tried that. It's the same as no debug.
                        There is a visible blank background before "Hello World" shows up
                        if I didn't add a QLabel in QtClass constructor.
                        Thank you!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved