“表白弹窗趣味互动程序“ 【Qt/C++】

一、程序概述

        当你想要跟你的男神/女神表达心意时,又怕被拒绝,于是出现了"表白弹窗神器",这是一款 是一款基于 Qt/C++ 开发的趣味互动程序,通过特殊的交互设计增加表白的趣味性和成功率(仅供娱乐)。程序主要功能是显示一个无法轻易关闭的表白窗口,其中 "不同意" 按钮会智能躲避用户点击,只有点击 "同意" 按钮才能正常关闭窗口,适合用于轻松愉快的表白场景。

二、编译环境

  • 基于 Qt 6.9.1 框架开发,使用 C++ 语言

  • 采用 Qt MainWindow 进行 UI 设计

  • 使用 MinGW 编译器构建

三、界面设计

  • 居中显示的表白文字,字体突出

  • 简洁美观的窗口布局(非常简单的界面)

  • 支持自定义表白内容和图片(需修改源码)

图标资源获取:iconfont-阿里巴巴矢量图标库

四、具体实现

4.1 界面展示

4.2 MainWindow类的构造函数

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    
    // 设置窗口标题和固定大小
    setWindowTitle("特别的表白");
    setFixedSize(500, 400);
    
    // 窗口居中显示
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenGeometry = screen->geometry();
    int x = (screenGeometry.width() - width()) / 2;
    int y = (screenGeometry.height() - height()) / 2;
    move(x, y);
    
    // 设置表白文本
    ui->confessionLabel->setText("亲爱的xxx,我是xxx \n\n我喜欢你很久了 \n你愿意做我的xxx吗?");
    ui->confessionLabel->setStyleSheet("font-size: 16pt; color: #e74c3c;");
}

4.3 不同意按钮槽函数

void MainWindow::on_disagreeButton_clicked()
{
    // 点击不同意时移动按钮
    moveDisagreeButton();
    QMessageBox::warning(this, "再想想", "不同意嘛?给你机会再考虑一下~");
}

4.4 移动不同意按钮

void MainWindow::moveDisagreeButton()
{
    // 获取窗口客户区范围
    int maxX = width() - ui->disagreeButton->width() - 20;
    int maxY = height() - ui->disagreeButton->height() - 50;
    
    // 生成随机位置,但确保不会和同意按钮太近
    QRandomGenerator *generator = QRandomGenerator::global();
    int x, y;
    QRect agreeRect = ui->agreeButton->geometry();
    
    do {
        x = generator->bounded(20, maxX);
        y = generator->bounded(50, maxY);
    } while (QRect(x, y, ui->disagreeButton->width(), 
                  ui->disagreeButton->height()).intersects(agreeRect));
    
    ui->disagreeButton->move(x, y);
}

bool MainWindow::isMouseNearDisagreeButton(const QPoint &mousePos)
{
    // 转换为窗口坐标
    QPoint localPos = mapFromGlobal(mousePos);
    // 检查鼠标是否在按钮附近
    QRect buttonRect = ui->disagreeButton->geometry();
    buttonRect.adjust(-50, -50, 50, 50); // 扩大检测范围
    
    return buttonRect.contains(localPos);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    // 当鼠标靠近不同意按钮时,移动按钮
    if (isMouseNearDisagreeButton(event->globalPos())) {
        moveDisagreeButton();
    }
    QMainWindow::mouseMoveEvent(event);
}

4.5 同意按钮槽函数

void MainWindow::on_agreeButton_clicked()
{
    flag=1;//标志位置1
    this->close();
}

4.6 关闭窗口

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(flag==0)
    {
        // 阻止用户关闭窗口
        event->ignore();
        QMessageBox::warning(this, "不行哦", "嗯?想跑?先答应我!!");
        moveDisagreeButton(); // 同时移动不同意按钮
    }
    else
    {
        // 同意后显示甜蜜消息并关闭窗口
        QMessageBox::information(this, "太棒了!", "我就知道你会同意的!爱你哟!❤️");
        QMessageBox::information(this, "你好!", "余生请多多指教!❤️");
        flag=0;
    }
}

4.6.1 标志位flag=0

4.6.2 标志位flag=1

五、打包

  1. 编译发布版本:在 Qt Creator 中切换至Release模式编译

  2. 复制依赖库:使用 Qt 提供的windeployqt工具自动复制所需的文件

  3. 打包步骤

    • 打开 Qt 命令行工具

    • 切换到 Release 目录

    • 执行命令:windeployqt xxx.exe

具体操作参考这位“北去踏歌行”博主的文章(很详细):QT 项目打包成 EXE 文件详细教程_qt打包成可执行程序-CSDN博客 

 (打包好就可以直接发.exe可执行文件给朋友啦)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值