qml with QQuickPaintedItem will slow down the ui operation
Unsolved
QML and Qt Quick
-
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:
or see it in : https://ibb.co/G4vsQFS1Solution3. 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()); }
so How do i optimise the draging perfermance or the cpu usage? thanks.