《QT实用小工具·五十三》会跑走的按钮

1、概述
源码放在文章末尾

该项目实现了会逃跑的按钮:

两个按钮,一个为普通按钮,另一个为会跑走的按钮

鼠标移到上面时,立刻跑掉

针对鼠标、键盘、触屏进行优化

随机交换两个按钮的文字、偶尔钻到另一个按钮下面、鼠标移开自动重置到原来状态

项目demo演示如下:

项目部分代码如下所示:

cpp 复制代码
#ifndef ESCAPEDIALOG_H
#define ESCAPEDIALOG_H

#include <QObject>
#include <QDialog>
#include <QLabel>
#include <QVBoxLayout>
#include <QDateTime>
#include <random>
#include <QDebug>
#include <QPropertyAnimation>
#include <QTimer>
#include <QKeyEvent>
#include "hoverbutton.h"

class EscapeDialog : public QDialog
{
#define MARGIN 20
    Q_OBJECT
public:
    EscapeDialog(QString title, QString msg, QString esc, QString nor, QWidget* parent = nullptr);

protected:
    void resizeEvent(QResizeEvent *event) override;
    void leaveEvent(QEvent* event) override;

private:
    void resetBtnPos();
    void recoverEscBtnPos();
    void moveEscBtnAni(QPoint aim);
    qint64 getTimestamp();
    int getRandom(int min, int max);
    bool isEqual(int a, int b);

public slots:
    void slotPosEntered(QPoint point);           // 鼠标进入事件:移动按钮或者交换按钮
    void slotEscapeButton(QPoint p = QPoint());  // 移动按钮
    void slotExchangeButton();                   // 交换按钮

private:
    QLabel* msg_lab;
    HoverButton* esc_btn/*accept*/, *nor_btn/*reject*/;

    std::random_device rd;
    std::mt19937 mt;

    bool exchanged; // 两个按钮是否交换了位置
    int escape_count; // 跑动的次数(包括交换)
    int last_escape_index; // 上次交换位置的次数(免得经常性的交换)
    bool has_overlapped; // 是否和另一个按钮进行重叠
};

#endif // ESCAPEDIALOG_H

源码下载

相关推荐
博客180020 小时前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴1 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨2 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
xcyxiner3 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner4 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner4 天前
DicomViewer (添加模型类)3
qt
xcyxiner5 天前
DicomViewer (目录调整) 2
qt
xcyxiner5 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
clint4566 天前
C++进阶(1)——前景提要
c++
夜悊6 天前
C++代码示例:进制数简单生成工具
c++