柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語(yǔ)言 c++ Qt
柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語(yǔ)言 c++ Qt
開始按鈕的實(shí)現(xiàn)效果:
????????1.制作按鈕
? ? ? ? 2.點(diǎn)擊之后向下移動(dòng)并返回原位置
? ? ? ? 3.跳轉(zhuǎn)到選擇關(guān)卡界面
具體實(shí)現(xiàn):
一.制作按鈕
? ? ? ? 思路:先創(chuàng)建按鈕,在添加圖片
? ? ? ? 設(shè)置按鈕大小
? ? ? ? 設(shè)置按鈕的不規(guī)則形狀
? ? ? ? 添加圖片
? ? ? ? 設(shè)置按鈕上圖片的大小
mypushbutton::mypushbutton(QString normalImg, QString pressImg)
{
//設(shè)定按鈕的大小,將圖片給到按鈕上
QPixmap pix;
bool ret=pix.load(normalImg);
if(!ret)
{
qDebug()<<"圖片錯(cuò)誤";
}
//設(shè)置按鈕大小
this->setFixedSize(pix.width(),pix.height());
//設(shè)置按鈕為不規(guī)則形狀
this->setStyleSheet("QPushButton{border:0px;}");
//設(shè)置圖標(biāo)
this->setIcon(pix);
//設(shè)置圖標(biāo)的大小
this->setIconSize(QSize(pix.width(),pix.height()));
}
二.點(diǎn)擊之后向下移動(dòng)并返回原位置
????????為了使得編碼的可移植性,將此功能封裝為mypushbutton(QString normalImg,QString pressImg="");normalImg為正常狀態(tài)的圖片,pressImg為按下之后的圖片默認(rèn)為空。
聲明成員屬性
//成員屬性,便于有按下行為按鈕功能的實(shí)現(xiàn)
QString normalImgPath;
QString pressImgPath;
實(shí)現(xiàn)按鈕的按下和返回功能
//按鈕跳躍
void zoomDone();
void zoomUp();
接下來(lái)進(jìn)行上述函數(shù)的具體實(shí)現(xiàn):
? ? ? ? 實(shí)現(xiàn)按下和返回的效果時(shí),應(yīng)該創(chuàng)建動(dòng)態(tài)對(duì)象:
//創(chuàng)建動(dòng)態(tài)對(duì)象.該對(duì)象設(shè)定以矩形框形式移動(dòng) QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
//創(chuàng)建對(duì)象運(yùn)動(dòng)的時(shí)間間隔
//確定運(yùn)動(dòng)對(duì)象的起始位置
//確定運(yùn)動(dòng)對(duì)象的終止位置
//設(shè)置運(yùn)動(dòng)對(duì)象的運(yùn)動(dòng)方式
//運(yùn)動(dòng)對(duì)象開始執(zhí)行
void mypushbutton::zoomDone()//按鈕按下的動(dòng)態(tài)行為
{
//創(chuàng)建動(dòng)態(tài)對(duì)象.該對(duì)象設(shè)定以矩形框形式移動(dòng)
QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
animation->setDuration(200);//設(shè)置運(yùn)動(dòng)時(shí)間
//設(shè)置開始結(jié)束的位置
animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//設(shè)置動(dòng)態(tài)對(duì)象的移動(dòng)形式
animation->setEasingCurve(QEasingCurve::OutBounce);
//執(zhí)行移動(dòng)
animation->start();
}
void mypushbutton::zoomUp()
{
//設(shè)置動(dòng)態(tài)對(duì)象
QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
//設(shè)置運(yùn)動(dòng)時(shí)間
animation->setDuration(200);
//位置
animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
//設(shè)置運(yùn)動(dòng)形式
animation->setEasingCurve(QEasingCurve::OutBounce);
//執(zhí)行運(yùn)動(dòng)
animation->start();
}
三.點(diǎn)擊之后實(shí)現(xiàn)跳轉(zhuǎn)界面
添加新的選擇關(guān)卡文件chooseScence.cpp/.h,設(shè)置新窗口的大小,標(biāo)題,背景圖片
chooseScence::chooseScence(QWidget *parent)
: QMainWindow{parent}
{
this->setFixedSize(320,588);
this->setWindowTitle("選擇關(guān)卡");
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//設(shè)計(jì)菜單欄實(shí)現(xiàn)退出功能
//創(chuàng)建菜單欄
QMenuBar * bar=menuBar();
setMenuBar(bar);
//創(chuàng)建菜單
QMenu * startMenu=bar->addMenu("開始");
//創(chuàng)建菜單項(xiàng)
QAction * quite=startMenu->addAction("退出");
//實(shí)現(xiàn)退出功能
connect(quite,&QAction::triggered,[=](){
this->close();
});
}
void chooseScence::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPixmap pix;
bool ret=pix.load(":/res/OtherSceneBg.png");
if(!ret)
{
qDebug()<<"圖片錯(cuò)誤";
return;
}
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//加載標(biāo)題圖片
pix.load(":/res/Title.png");
painter.drawPixmap( (this->width()-pix.width())*0.5,30,pix );
}
添加背景圖片時(shí),需要使用繪畫事件進(jìn)行實(shí)現(xiàn)
柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語(yǔ)言 c++ Qt
相關(guān)文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。