Created
August 10, 2017 11:54
-
-
Save Soulstorm50/b14f2cf0d7de770629a1f7decfddf24f to your computer and use it in GitHub Desktop.
src on screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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