Answer the question
In order to leave comments, you need to log in
Animation in qt widget on qpainter - how to pause?
Tell me how to pause between frames. OS - Linux?
With the help of google, I came across various implementations of sleep () on the network, but with their application only the final result was displayed.
#include "qpaintwidget.h"
#include <QPainter>
QPaintWidget::QPaintWidget(QWidget * parent) : QWidget(parent)
{
}
void QPaintWidget::paintEvent(QPaintEvent *) {
QColor whitebrush = Qt::white;
QColor blackbrush = Qt::black;
QColor redbrush = Qt::darkRed;
QPainterPath path;
QPainter painter(this); // Создаём новый объект рисовальщика
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
for (int i = 0; i <= 40; i++){
//Sleep(1000);
painter.translate(1, 0);
painter.setBrush(whitebrush);
painter.drawRect(0, 0, width(), height());
QPoint triangle[3] = {
QPoint(180, 450),
QPoint(220, 450),
QPoint(200, 555),
};
painter.setBrush(blackbrush);
painter.drawEllipse(10, 15, 380, 380);
painter.setBrush(redbrush);
painter.drawEllipse(35, 8, 330, 330);
painter.setBrush(whitebrush);
painter.drawEllipse(50, -5, 300, 330);
painter.setBrush(blackbrush);
painter.drawPolygon(triangle, 3);
path.setFillRule(Qt::WindingFill);
path.addRect(180, 250, 40, 200);
path.addRect(150, 250, 100, 20);
painter.drawPath(path);
painter.setBrush(whitebrush);
painter.drawEllipse(120, 260, 60, 20);
painter.drawEllipse(220, 260, 60, 20);
}
}
Answer the question
In order to leave comments, you need to log in
You are drawing wrong. Try this
qpaintwidget.cpp:
#include "qpaintwidget.h"
#include <QPainter>
#include <QDebug>
QPaintWidget::QPaintWidget(QWidget *parent)
: QWidget(parent)
{
offset = 0;
resize(800, 600);
paintTimer = new QTimer(this);
paintTimer->start(10);
connect(paintTimer, SIGNAL(timeout()), this, SLOT(updatePixmap()));
}
void QPaintWidget::paintEvent(QPaintEvent *)
{
QColor whitebrush = Qt::white;
QColor blackbrush = Qt::black;
QColor redbrush = Qt::darkRed;
QPainterPath path;
QPainter painter(this); // Создаём новый объект рисовальщика
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.translate(1 + offset, 0);
painter.setBrush(whitebrush);
painter.drawRect(0, 0, width(), height());
QPoint triangle[3] = {
QPoint(180, 450),
QPoint(220, 450),
QPoint(200, 555),
};
painter.setBrush(blackbrush);
painter.drawEllipse(10, 15, 380, 380);
painter.setBrush(redbrush);
painter.drawEllipse(35, 8, 330, 330);
painter.setBrush(whitebrush);
painter.drawEllipse(50, -5, 300, 330);
painter.setBrush(blackbrush);
painter.drawPolygon(triangle, 3);
path.setFillRule(Qt::WindingFill);
path.addRect(180, 250, 40, 200);
path.addRect(150, 250, 100, 20);
painter.drawPath(path);
painter.setBrush(whitebrush);
painter.drawEllipse(120, 260, 60, 20);
painter.drawEllipse(220, 260, 60, 20);
}
void QPaintWidget::updatePixmap()
{
(offset >= 810) ? offset = 0 : offset+=5;
qDebug() << offset;
repaint();
}
QPaintWidget::~QPaintWidget()
{
}
#ifndef QPAINTWIDGET_H
#define QPAINTWIDGET_H
#include <QWidget>
#include <QTimer>
class QPaintWidget : public QWidget
{
Q_OBJECT
int offset;
QTimer *paintTimer;
protected:
void paintEvent(QPaintEvent *);
private slots:
void updatePixmap();
public:
QPaintWidget(QWidget *parent = 0);
~QPaintWidget();
};
#endif // QPAINTWIDGET_H
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question