Skip to content

Instantly share code, notes, and snippets.

@EXL
Last active May 11, 2022 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EXL/8e4f46b2a6572ccd7c09 to your computer and use it in GitHub Desktop.
Save EXL/8e4f46b2a6572ccd7c09 to your computer and use it in GitHub Desktop.
PushMe is fun elusive button
#include "Widget.h"
#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qsrand(time(0));
QTranslator appTranslator;
appTranslator.load("PushMe_" + QLocale::system().name(), "://");
a.installTranslator(&appTranslator);
Widget w;
w.show();
return a.exec();
}
#-------------------------------------------------
#
# Project created by QtCreator 2014-07-29T21:37:03
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = PushMe
TEMPLATE = app
SOURCES += main.cpp\
Widget.cpp
HEADERS += Widget.h
FORMS += Widget.ui
TRANSLATIONS += PushMe_en.ts \
PushMe_ru.ts \
PushMe_es.ts
RESOURCES += \
PushMe.qrc
<RCC>
<qresource prefix="/">
<file>PushMe_es.qm</file>
<file>PushMe_ru.qm</file>
<file>PushMe_en.qm</file>
</qresource>
</RCC>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>Widget</name>
<message>
<location filename="Widget.ui" line="14"/>
<location filename="Widget.ui" line="26"/>
<source>Push Me!</source>
<translation>Push Me!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Win!</source>
<translation>Win!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Oh Yeah! You win!</source>
<translation>Oh Yeah! You win!</translation>
</message>
</context>
</TS>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="es_ES">
<context>
<name>Widget</name>
<message>
<location filename="Widget.ui" line="14"/>
<location filename="Widget.ui" line="26"/>
<source>Push Me!</source>
<translation>Push Me!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Win!</source>
<translation>¡Victoria!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Oh Yeah! You win!</source>
<translation>Oh sí! Usted gana!</translation>
</message>
</context>
</TS>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="ru_RU">
<context>
<name>Widget</name>
<message>
<location filename="Widget.ui" line="14"/>
<location filename="Widget.ui" line="26"/>
<source>Push Me!</source>
<translation>Нажми меня!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Win!</source>
<translation>Победа!</translation>
</message>
<message>
<location filename="Widget.cpp" line="101"/>
<source>Oh Yeah! You win!</source>
<translation>О да! Вы выиграли!</translation>
</message>
</context>
</TS>
#include "Widget.h"
#include "ui_Widget.h"
#include <QCursor>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pushButton->installEventFilter(this);
startTimer(10);
}
void Widget::timerEvent(QTimerEvent *)
{
if (buttonCoords().contains(QCursor::pos())) {
moveButton();
}
}
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->pushButton) {
if (event->type() == QEvent::KeyPress) {
return true;
}
}
return QWidget::eventFilter(obj, event);
}
QRect Widget::buttonCoords() const
{
QPoint globalCoords = ui->pushButton->mapToGlobal(QPoint(0,0));
int x = globalCoords.x();
int y = globalCoords.y();
QPoint lastCorner;
lastCorner.setX(x + ui->pushButton->width());
lastCorner.setY(y + ui->pushButton->height());
return QRect(globalCoords, lastCorner);
}
void Widget::moveButton()
{
ui->pushButton->move(generateValidPoint());
}
QPoint Widget::generateValidPoint() const
{
int eps = 5;
int x = ui->pushButton->pos().x();
int y = ui->pushButton->pos().y();
if (buttonCoords().center().x() < QCursor::pos().x()) {
x = qrand() % eps - ui->pushButton->width() / 4 + ui->pushButton->pos().x();
}
if (buttonCoords().center().y() < QCursor::pos().y()) {
y = qrand() % eps - ui->pushButton->height() / 2 + ui->pushButton->pos().y();
}
if (buttonCoords().center().x() >= QCursor::pos().x()) {
x = qrand() % eps + ui->pushButton->width() / 4 + ui->pushButton->pos().x();
}
if (buttonCoords().center().y() >= QCursor::pos().y()) {
y = qrand() % eps + ui->pushButton->height() / 2 + ui->pushButton->pos().y();
}
if (!rect().contains(QRect(x, y, ui->pushButton->width(), ui->pushButton->height()))) {
if (rect().x() < x) {
x = qrand() % eps - ui->pushButton->width() / 4 + ui->pushButton->pos().x();
}
if (rect().y() < y) {
y = qrand() % eps - ui->pushButton->height() / 2 + ui->pushButton->pos().y();
}
if (rect().x() >= x) {
x = qrand() % eps + ui->pushButton->width() / 4 + ui->pushButton->pos().x();
}
if (rect().y() >= y) {
y = qrand() % eps + ui->pushButton->height() / 2 + ui->pushButton->pos().y();
}
}
return QPoint(x, y);
}
void Widget::on_pushButton_clicked()
{
QMessageBox::information(this, tr("Win!"), tr("Oh Yeah! You win!"));
}
Widget::~Widget()
{
delete ui;
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
private:
QRect buttonCoords() const;
void moveButton();
QPoint generateValidPoint() const;
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void timerEvent(QTimerEvent *);
bool eventFilter(QObject *, QEvent *);
private:
Ui::Widget *ui;
private slots:
void on_pushButton_clicked();
};
#endif // WIDGET_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Push Me!</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>150</x>
<y>150</y>
<width>80</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Push Me!</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment