Animation: Difference between revisions

1,450 bytes added ,  4 years ago
Added C++ solution
(Added C++ solution)
Line 332:
}
}
}</lang>
 
=={{header|C++}}==
{{libheader|Qt}}
Contents of animationwidget.cpp:
<lang cpp>#include "animationwidget.h"
 
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
 
#include <algorithm>
 
AnimationWidget::AnimationWidget(QWidget *parent) : QWidget(parent) {
setWindowTitle(tr("Animation"));
QFont font("Courier", 24);
QLabel* label = new QLabel("Hello World! ");
label->setFont(font);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(label);
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [label,this]() {
QString text = label->text();
std::rotate(text.begin(), text.begin() + (right_ ? text.length() - 1 : 1), text.end());
label->setText(text);
});
timer->start(200);
}
 
void AnimationWidget::mousePressEvent(QMouseEvent*) {
right_ = !right_;
}</lang>
 
Contents of animationwidget.h:
<lang cpp>#ifndef ANIMATIONWIDGET_H
#define ANIMATIONWIDGET_H
 
#include <QWidget>
 
class AnimationWidget : public QWidget {
Q_OBJECT
public:
AnimationWidget(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *event) override;
private:
bool right_ = true;
};
 
#endif // ANIMATIONWIDGET_H</lang>
 
Contents of main.cpp:
<lang cpp>#include "animationwidget.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AnimationWidget w;
w.show();
return a.exec();
}</lang>
 
1,777

edits