当前位置: 首页 > article >正文

Qt桌面应用开发 第十天(综合项目二 翻金币)

目录

1.主场景搭建

1.1重载绘制事件,绘制背景图和标题图片

1.2设置窗口标题,大小,图片

1.3退出按钮对应关闭窗口,连接信号

2.开始按钮创建

2.1封装MyPushButton类

2.2加载按钮上的图片

3.开始按钮跳跃效果

3.1按钮向上跳动

3.2按钮向下跳动

4.选择关卡场景搭建

4.1创建选择关卡场景的菜单栏,包括开始,退出

4.2重载绘制事件,绘制背景图和标题图

5.主场景进入选择关卡场景

5.1mainScene.h中添加选择关卡场景的对象,且构造函数中创建该对象

5.2开始按钮动画执行完毕后,延时0.5秒,隐藏当前窗口,显示选择关卡页面


1.主场景搭建

1.1重载绘制事件,绘制背景图和标题图片

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    //背景图
    pix.load(":/CoinRes/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载图片
    pix.load(":/CoinRes/Title.png");
    //缩放图片
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);
}

1.2设置窗口标题,大小,图片

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //设置固定大小
    this->setFixedSize(320,580);
    //设置应用图片
    this->setWindowIcon(QPixmap(":/CoinRes/Coin0001.png"));
    //设置窗口标题
    this->setWindowTitle("翻金币游戏");
}

1.3退出按钮对应关闭窗口,连接信号

    //退出按钮,退出程序
    connect(ui->actionQuit,&QAction::triggered,[=]{
        this->close();
    });

2.开始按钮创建

需求如下:开始按钮,初始时为一个图片,按下显示为另一个图片

2.1封装MyPushButton类

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyPushButton(QWidget *parent = nullptr);

    MyPushButton(QString normalImg,QString pressImg="");

    //默认显示图片路径
    QString normalImgPath;
    //按下后显示的图片路径
    QString pressedImgPath;

signals:

};

2.2加载按钮上的图片

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    normalImgPath=normalImg;
    pressedImgPath=pressImg;

    QPixmap pix;
    bool ret=pix.load(normalImgPath);
    if(false==ret)
    {
        qDebug()<<normalImg<<"图片加载失败";
    }

    //设置图片的固定尺寸
    this->setFixedSize(pix.width(),pix.height());
    //设置不规则图片的样式表,将背景多余部分取消掉
    this->setStyleSheet("QPushButton{border:0px;}");
    //设置图标
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

3.开始按钮跳跃效果

需求:按钮点击后,可以向上向下跳动

3.1按钮向上跳动

void MyPushButton::zoom1()
{
    //创建动画对象,在当前按钮用几何图形
    QPropertyAnimation* animation1=new QPropertyAnimation(this,"geometry");
    //设置动画的维持时间
    animation1->setDuration(200);
    //设置起始位置
    animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置结束位置
    animation1->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置缓和曲线,设为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画,设置属性,动画执行结束后销毁对象
    animation1->start(QAbstractAnimation::DeleteWhenStopped);
}

3.2按钮向下跳动

void MyPushButton::zoom2()
{
    //创建动画对象,在当前按钮用几何图形
    QPropertyAnimation* animation1=new QPropertyAnimation(this,"geometry");
    //设置动画的维持时间
    animation1->setDuration(200);
    //设置起始位置
    animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置结束位置
    animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线,设为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画,设置属性,动画执行结束后销毁对象
    animation1->start(QAbstractAnimation::DeleteWhenStopped);
}

4.选择关卡场景搭建

4.1创建选择关卡场景的菜单栏,包括开始,退出

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/CoinRes/Coin001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");

    //创建菜单栏
    QMenuBar* bar=this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu* startMenu=bar->addMenu("开始");
    //创建按钮菜单项
    QAction* quitAction=startMenu->addAction("退出");
    //点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=]{this->close();});
}

4.2重载绘制事件,绘制背景图和标题图

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/CoinRes/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/CoinRes/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);

}

5.主场景进入选择关卡场景

5.1mainScene.h中添加选择关卡场景的对象,且构造函数中创建该对象

    //选择关卡场景
    ChooseLevelScene* chooseScene;
    //创建选择关卡场景
    this->chooseScene=new ChooseLevelScene;

5.2开始按钮动画执行完毕后,延时0.5秒,隐藏当前窗口,显示选择关卡页面

    //监听事件,执行特效
    connect(starBtn,&MyPushButton::clicked,[=]{
        //向下跳跃
        starBtn->zoom1();
        //向上跳跃
        starBtn->zoom2();

        //延时0.5秒之后,隐藏当前窗口,显示选择关卡页面
        QTimer::singleShot(500,this,[=]{
            this->hide();
            chooseScene->show();
        });


http://www.kler.cn/a/421733.html

相关文章:

  • Spring 邮件发送
  • 基于开源云原生数据仓库 ByConity 体验多种数据分析场景
  • ThingsBoard集成外部工单系统方案
  • 数据仓库的概念
  • Qt 2D绘图之三:绘制文字、路径、图像、复合模式
  • Vue3可以不用Pinia了?
  • es 3期 第12节-选择合适的数据查询方式
  • 【Go底层】time包中Timer定时器原理
  • 【分组去重】.NET开源 ORM 框架 SqlSugar 系列
  • 论文阅读:Generating Synthetic Data for Medical Imaging
  • 企业AI助理在数据分析与决策中扮演的角色
  • B树与B+树的区别,为什么MySQL使用B+树不使用B树
  • Elasticsearch在liunx 中单机部署
  • ElasticSearch QueryDSL详解
  • 通过JS逆向,爬取音乐(仅供学习交流,严禁非法使用)
  • Kubernetes集群添加主机名解析
  • Node.js 实战: 爬取百度新闻并序列化 - 完整教程
  • c++预编译头文件
  • java调用ai模型:使用国产通义千问完成基于知识库的问答
  • 详解日志格式配置:XML 与 Spring Boot 配置文件格式