Skip to content

Instantly share code, notes, and snippets.

@EXL
Last active May 11, 2022 01:57
Show Gist options
  • Select an option

  • Save EXL/8e4f46b2a6572ccd7c09 to your computer and use it in GitHub Desktop.

Select an option

Save EXL/8e4f46b2a6572ccd7c09 to your computer and use it in GitHub Desktop.

Revisions

  1. EXL renamed this gist May 11, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. EXL revised this gist May 11, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Warning!
    ========

    This gist was moved to:

    **[https://github.com/EXL/Stuff/tree/master/Examples/Qt-PushMe](https://github.com/EXL/Stuff/tree/master/Examples/Qt-PushMe)**
  3. EXL revised this gist May 20, 2018. 1 changed file with 0 additions and 0 deletions.
    Binary file modified demonstration.gif
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  4. EXL revised this gist Oct 10, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.cpp
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@
    #include <QTranslator>
    #include <QLocale>

    #include <ctime>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
  5. EXL created this gist Jul 29, 2014.
    27 changes: 27 additions & 0 deletions PushMe.pro
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #-------------------------------------------------
    #
    # 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
    7 changes: 7 additions & 0 deletions PushMe.qrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <RCC>
    <qresource prefix="/">
    <file>PushMe_es.qm</file>
    <file>PushMe_ru.qm</file>
    <file>PushMe_en.qm</file>
    </qresource>
    </RCC>
    Binary file added PushMe_en.qm
    Binary file not shown.
    23 changes: 23 additions & 0 deletions PushMe_en.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?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>
    Binary file added PushMe_es.qm
    Binary file not shown.
    23 changes: 23 additions & 0 deletions PushMe_es.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?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>
    Binary file added PushMe_ru.qm
    Binary file not shown.
    23 changes: 23 additions & 0 deletions PushMe_ru.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?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>
    107 changes: 107 additions & 0 deletions Widget.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    #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;
    }
    33 changes: 33 additions & 0 deletions Widget.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #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
    33 changes: 33 additions & 0 deletions Widget.ui
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?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>
    Binary file added demonstration.gif
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    21 changes: 21 additions & 0 deletions main.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #include "Widget.h"

    #include <QApplication>
    #include <QTranslator>
    #include <QLocale>

    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();
    }