move photo from right to left on widget
-
I want to move a photo from right to left on Widget continually,but I find flicker appearance.
Widget::Widget(QWidget *parent) : QWidget(parent) { auto tm = new QTimer(this); connect(tm, &QTimer::timeout, [this]{ this->update();}): tm->start(100); } void Widget::paintEvent(QPaintEvent* event) { QPainter pt(this); auto w = this->width(); auto h = this->height(); auto imgw = this->_img.width(); //_img is QImage auto imgh = this->_img.height(); if(this->_pos >= imgw) this->_pos = 0; this->_pos += 10; // init 0 if(this->_pos >= imgw) this->_pos = imgw; auto x = (w - this->_pos) / 2.0; auto y = (h - imgh) / 2.0; pt.drawImage(QPointF(x, y), _img, QRectF(0, 0, this->_pos, imgh)); }
How to avoid the flicker ? or is there other better way to achieve function?
Attention: The initial requirement was to merge the received image slices and move them continuously from right to left on widget -
@camel-cn
And so? Maybe I do not understand, but you have aQTimer
moving something, which is what the animation framework does for you. Maybe it does not work fordrawImage()
, I'm not sure. Another possibility is to use aQGraphicsScene
instead, I don't know if that would "flicker" less than drawing on a widget. -
Hi,
How do you merge them ?
-
@camel-cn said in move photo from right to left on widget:
try the following:
void Widget::paintEvent(QPaintEvent* event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.save(); auto w = this->width(); auto h = this->height(); auto imgw = this->_img.width(); //_img is QImage auto imgh = this->_img.height(); if(this->_pos >= imgw) this->_pos = 0; this->_pos += 10; // init 0 if(this->_pos >= imgw) this->_pos = imgw; auto x = (w - this->_pos) / 2.0; auto y = (h - imgh) / 2.0; painter.drawImage(QPointF(x, y), _img, QRectF(0, 0, this->_pos, imgh)); painter.restore(); }
-
@camel-cn said in move photo from right to left on widget:
@SGaist Mainly through the function drawImage of QPainter
You move your image in that function, it does not look like you do any merging there.