欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Qt桌面应用开发 第十天(综合项目二 翻金币)

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

2025/11/10 2:14:17 来源:https://blog.csdn.net/m0_53349772/article/details/144201059  浏览:    关键词:Qt桌面应用开发 第十天(综合项目二 翻金币)

目录

1.主场景搭建

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

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

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

2.开始按钮创建

2.1封装MyPushButton类

2.2加载按钮上的图片

3.开始按钮跳跃效果

3.1按钮向上跳动

3.2按钮向下跳动


1.主场景搭建

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

void MainScene::paintEvent(QPaintEvent *event)
{QPainter painter(this);QPixmap pix;//背景图pix.load(":/CoinRes/2.png");painter.drawPixmap(0,0,this->width(),this->height(),pix);//加载图片pix.load(":/CoinRes/2.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/1.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(":/CoinRes/1.png");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);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词