Skip to content

Instantly share code, notes, and snippets.

@Soulstorm50
Created August 10, 2017 11:54
Show Gist options
  • Save Soulstorm50/b14f2cf0d7de770629a1f7decfddf24f to your computer and use it in GitHub Desktop.
Save Soulstorm50/b14f2cf0d7de770629a1f7decfddf24f to your computer and use it in GitHub Desktop.
src on screen
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget mw(0);
mw.resize(500, 500);
mw.show();
return app.exec();
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include "qpainter.h"
#include <QPushButton>
#include <QVariant>
#include <QDebug>
#include <windows.h>
class Widget : public QWidget {
Q_OBJECT
int dx;
int dy;
QRect circleBounds;
public:
Widget(QWidget * parent) : QWidget(parent) {
dx = 1;
dy = 1;
circleBounds.setLeft(34);
circleBounds.setTop(21);
circleBounds.setWidth(50);
circleBounds.setHeight(50);
QTimer * timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
timer->start(10);
}
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::NoPen); // circ color
painter.setBrush(Qt::white); // desktop color
painter.drawRect(this->rect());
if ( ( circleBounds.left() + dx ) < 0 || ( circleBounds.right() + dx ) > this->rect().width() )
dx = -dx;
if ( ( circleBounds.top() + dy ) < 0 || ( circleBounds.bottom() + dy ) > this->rect().height() )
dy = -dy;
circleBounds.setLeft(circleBounds.left() + dx);
circleBounds.setTop(circleBounds.top() + dy);
circleBounds.setWidth(50);
circleBounds.setHeight(50);
painter.setBrush(Qt::blue);
painter.drawEllipse(circleBounds);
}
};
#endif // WIDGET_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment