Last active
May 11, 2022 01:57
-
-
Save EXL/8e4f46b2a6572ccd7c09 to your computer and use it in GitHub Desktop.
Revisions
-
EXL renamed this gist
May 11, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
EXL revised this gist
May 11, 2022 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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)** -
EXL revised this gist
May 20, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
EXL revised this gist
Oct 10, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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); -
EXL created this gist
Jul 29, 2014 .There are no files selected for viewing
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 charactersOriginal 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 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 charactersOriginal 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 not shown.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 charactersOriginal 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 not shown.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 charactersOriginal 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 not shown.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 charactersOriginal 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> 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 charactersOriginal 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; } 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 charactersOriginal 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 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 charactersOriginal 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> LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed.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 charactersOriginal 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(); }