Unsolved QWidget - the drawn image is erased
-
Hi, everybody!
Host OS: Linux x2004d 5.13.0-27-generic #29~20.04.1-Ubuntu SMP Fri Jan 14 00:32:30 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Target Hardware: IMX8 Toradex Ixora V1.2A
Target OS: Linux apalis-imx8-06543230 5.4.154-5.5.0+git.c65f1622951c #1 SMP PREEMPT Mon Jan 3 15:58:01 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
KIT(SDK-Toolchain):
/opt/tdx-xwayland-rt/5.5.0/sysroots/x86_64-tdxsdk-linux/usr/bin/qmake
/opt/tdx-xwayland-rt/5.5.0/sysroots/x86_64-tdxsdk-linux/usr/bin/aarch64-tdx-linux/aarch64-tdx-linux-g++
Example: /home/mihail/Qt/Examples/Qt-5.14.2/widgets/painting/gradients/I changed:
ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent) : QWidget(parent), m_shade_type(type), m_alpha_gradient(QLinearGradient(0, 0, 0, 0)) { ... setAttribute(Qt::WA_OpaquePaintEvent, true); // Should not erase the previous one ! QTimer *timer = new QTimer(); connect(timer, &QTimer::timeout, this, &ShadeWidget::timerWork); timer->start(100); } void ShadeWidget::timerWork() { update(); } void ShadeWidget::paintEvent(QPaintEvent *) { QPainter p(this); static int i = 0; if(i > width()) i = 0; p.setPen(Qt::red); p.drawLine(i, 0, i, height() - 1); // draw the added line by timer i++; }
Problem: Old drawn lines are erased! Why? How to fix it?
Please help. -
@mihail said in QWidget - the drawn image is erased:
How to fix it?
Draw all lines including old lines. Qt will not redraw old lines for you.
-
@mihail said in QWidget - the drawn image is erased:
Why?
because paintEvent() is used to redraw the widget.
How to fix it?
Remember the old lines and draw them in paintEvent()
-
On a desktop PC (Xubuntu) and on NanoPi M4V 2 (Lubuntu) everything is OK. If you draw the old graphics, processor thread every time, it load up to 100%.
-
You can also draw on an image, remember this and only draw the new line in the next run. Then paint this image in paintEvent to your widget. This will reduce the #lines to paint.
-
?:
painter.drawPixmap(rect, QPixmap::fromImage(image));
The graphic stream is also loaded up to 100% at Full HD.
-
Hi,
Keep the QPixmap rather than execute the conversion each time.