I
I
Ivan Tishchenko2014-03-22 23:01:55
linux
Ivan Tishchenko, 2014-03-22 23:01:55

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.

Contents of qpaintwidget.cpp
#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

1 answer(s)
E
EXL, 2014-03-23
@Tihon_V

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()
{

}

qpaintwidget.h:
#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

And some advice:
1. First, never use sleep() or Sleep() (on Windows) in Qt programs. Since when this function is called, the main thread is frozen, in which the GUI is rendered. Hence the brakes of the program, etc.
2. Never start the names of your classes with the letter Q or q, because then they are easy to confuse with those classes that are included in the library. If you really need a prefix in the class name, use a capital C (Class), i.e. your class should be called CPaintWidget or just PaintWidget.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question