Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qml with QQuickPaintedItem will slow down the ui operation
Forum Updated to NodeBB v4.3 + New Features

qml with QQuickPaintedItem will slow down the ui operation

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 253 Views
  • 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by QtTester
    #1

    hey, guys,
    I want to display camera picture throuth QML with QQuickPaintedItem, but I found a few problem.
    the update timer (about 45ms to 33ms):

    void MainWindow::onLoadTimer()
    {
        static int idx = 1;
    
        QString file = QString("%1.jpg").arg(idx,2,10,QChar('0'));
        QImage img(file);
    
        m_iv->updateImage(img);
        ++idx;
        if(idx > FPS)
            idx = 1;
    }
    

    the update func:

    void CamImageItem::updateImage(const QImage &img)
    {
        if(img.isNull()){
            return;
        }
        m_bgImg = std::move(img);
    
        if(m_lastImgHeight!=img.height() || m_lastImgWidth!=img.width()){
            auto map = fitToShowAll(m_lastWinWidth,m_lastWinHeight);
    
            emit sigImageChanged(map);
        }
        update();
    }
    

    Solution1, only draw qimage in qwidget's paintEvent, the cpu usage is up to 15%, but draging window seems no affection.
    Solution2, use QQuickPaintedItem class :

    class CamImageItem : public QQuickPaintedItem
    {
        Q_OBJECT
    ...
    protected:
        void paint(QPainter *painter)override;
    ...
    }
    
    void CamImageItem::paint(QPainter *painter)
    {
        auto *img = &m_bgImg;
    
        // image and cross
        painter->drawImage(0,0,*img);
        painter->setPen(QPen(Qt::red,1));
        painter->drawLine(0,img->height()/2.0,img->width(),img->height()/2.0);
        painter->drawLine(img->width()/2.0,0,img->width()/2.0,img->height());
    }
    

    the cpu usage is up to 16%, GPU up to 10~20%. but draging window seems slow down:
    alt text
    or see it in : https://ibb.co/G4vsQFS1

    Solution3. donot draw image in paint Event, still the cpu's usage is up to 15%:

    void CamImageItem::paint(QPainter *painter)
    {
        auto *img = &m_bgImg;
    
        // do not draw image still occupy cpu 15% and gpu to 10%
        // painter->drawImage(0,0,*img);
        painter->setPen(QPen(Qt::red,1));
        painter->drawLine(0,img->height()/2.0,img->width(),img->height()/2.0);
        painter->drawLine(img->width()/2.0,0,img->width()/2.0,img->height());
    }
    

    录制_2025_02_24_19_46_14_659.gif

    so How do i optimise the draging perfermance or the cpu usage? thanks.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtTester
      wrote on last edited by
      #2

      I found QQuickFramebufferObject may be is the solution.

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        Solution 4: use VideoOutput and feed frames to its QVideoSink.
        The red cursor can be made in simple QtQuick with 2 Rectangles, or a QQuickPaintedItem if you prefer it for some reason.

        Q 1 Reply Last reply
        0
        • GrecKoG GrecKo

          Solution 4: use VideoOutput and feed frames to its QVideoSink.
          The red cursor can be made in simple QtQuick with 2 Rectangles, or a QQuickPaintedItem if you prefer it for some reason.

          Q Offline
          Q Offline
          QtTester
          wrote on last edited by
          #4

          @GrecKo thanks ,i will try video output later, now i use QQuickFramebufferObject.

          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