寫在前面
此篇開始是對旅行模擬系統(tǒng)中遇到的問題的總結(jié)記錄炭玫,以及相關(guān)知識點的記錄奈嘿,以備后續(xù)查看。
按鈕封裝是比較基礎(chǔ)的內(nèi)容吞加,此部分學(xué)習(xí)自bilibili用戶“寒風(fēng)落輕塵”上載的視頻——黑馬2018年qt視頻裙犹,是我認(rèn)為B站qt基礎(chǔ)教學(xué)非常好的視頻,老師幽默風(fēng)趣且教學(xué)安排較好衔憨。
下面開始內(nèi)容:
要實現(xiàn)的功能
qt自帶的按鈕是比較一般叶圃,尤其在win10我認(rèn)為按鈕做的反而越來越丑,因此實現(xiàn)按鈕的封裝對于美化一個軟件的ui界面是非常有必要的巫财,并且要有按鈕點擊的特效盗似,如實現(xiàn)點擊后按鈕向下跳躍后返回原位等哩陕。
流程
//mypushbutton.h
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QPushButton>
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
MyPushButton(QString normalImg, QString pressImg = "");
QString normalImgPath;
QString pressedImgPath;
//向下跳躍
void zoom1();
//向上跳躍
void zoom2();
signals:
public slots:
};
#endif // MYPUSHBUTTON_H
//mypushbutton.cpp
#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>
//封裝一個自己的按鈕平项,該按鈕能實現(xiàn)點擊圖片彈出對話框
//傳入兩個參數(shù)赫舒,第一個是正常的圖片,第二個是按下后顯示的圖片
//因為需求的按鈕只需要實現(xiàn)動畫效果即可闽瓢,不需要第二張圖片接癌,僅傳入一個參數(shù)即可
MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
//保存正常圖片路徑和 選中后顯示的路徑
this->normalImgPath = normalImg;
this->pressedImgPath = pressImg;
QPixmap pix;
bool ret = pix.load(this->normalImgPath);
if(!ret)
{
//此行代碼實現(xiàn)幾個Qstring之間 的拼接
QString str = QString("1%圖片加載失敗").arg(this->normalImgPath);
qDebug()<<str;
return;
}
//設(shè)定大小為正常的0.5倍
this->setFixedSize(pix.width()*0.5,pix.height()*0.5);
//設(shè)置圖片
this->setIcon(pix);
//設(shè)置圖片大小為正常的0.5倍
this->setIconSize(QSize(pix.width()*0.5,pix.height()*0.5));
}
//向下跳躍
void MyPushButton::zoom1()
{
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
//設(shè)定時間間隔
animation->setDuration(100);
//設(shè)置動畫對象起始位置
animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height() ));
//設(shè)置動畫對象結(jié)束位置
animation->setEndValue(QRect(this->x(),this->y()+8 ,this->width(),this->height() ));
//設(shè)置曲線
animation->setEasingCurve(QEasingCurve::Linear);
//執(zhí)行動畫
animation->start();
}
//向上跳躍
void MyPushButton::zoom2()
{
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
//設(shè)定時間間隔
animation->setDuration(100);
//設(shè)置動畫對象起始位置
animation->setStartValue(QRect(this->x(),this->y()+8,this->width(),this->height() ));
//設(shè)置動畫對象結(jié)束位置
animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height() ));
//設(shè)置曲線
animation->setEasingCurve(QEasingCurve::Linear);
//執(zhí)行動畫
animation->start();
}
如何使用
在使用該封裝的按鈕時,如下所示:
//成都按鈕創(chuàng)建
MyPushButton *Chengdu = new MyPushButton(":/city/Chengdu.png");
Chengdu->setParent(this); Chengdu->move(100,280);
connect(Chengdu,&QPushButton::clicked,[=](){
//點擊按鈕扣讼,實現(xiàn)彈起特效
Chengdu->zoom1(); Chengdu->zoom2();
//點擊按鈕缺猛,彈出該城市時刻表
city_clicked = 0; //0代表成都
tree = new ScheduelTree;//創(chuàng)建出新的窗口,顯示時刻表信息
tree->FromA(city_clicked);//調(diào)用該函數(shù)將點擊的城市city_clicked值賦給tree中的city椭符,將Map1中的***head給tree中的quote荔燎,并在此設(shè)置窗口標(biāo)題
//************注意此處*************
//如何傳遞一個指針數(shù)組的頭指針?將widget中的tmp1賦給FromA销钝,F(xiàn)romA中以schedule ***tmp接收有咨,并令那個類中聲明的schedule ***quote接收tmp,才可以使用
tree->VehicalTime(); //調(diào)用該函數(shù)顯示窗口中的各種信息(時刻表等)
tree->exec(); //顯示該窗口(模態(tài))
});
END