主界面
程序的主界面显示如图所示,含有菜单栏和按钮控件。
实现的内容有:
-
程序的子菜单有“退出”下拉框选项,退出关闭程序。
-
“START”按钮:进入选择关卡界面。
/*主界面*/
setFixedSize(320,588);//设置固定大小
setWindowIcon(QIcon(":/icon/icno/Coin0001.png"));//设置图标
setWindowTitle("主界面");//设置标题
背景
重写==paintEvent(QPaintEvent *event)==事件,绘制背景图片。
/*重写绘图事件,添加背景图*/
void MainPage::paintEvent(QPaintEvent *event)
{
QPainter paninter(this);//创建画家,指定当前界面为绘图设备
QPixmap pix;//创建QPixmap对象
pix.load(":/icon/icno/PlayLevelSceneBg.png");//加载图像
paninter.drawPixmap(0,0,this->width(),this->height(),pix);//绘制背景图
pix.load(":/icon/icno/Title.png");//绘制背景标题
pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);//按宽度、高度的大小*倍数缩放图片
paninter.drawPixmap(10,30,pix);
}
菜单栏
添加菜单栏,并添加一个“退出”子菜单栏,实现退出程序。
/*退出游戏*/
connect(ui->actionQuit,&QAction::triggered,[=](){
if(QMessageBox::Yes==QMessageBox::question(this,"提示","是否确定退出游戏?",QMessageBox::Yes|QMessageBox::No))
{
//点击确定
this->close();
}
});
关卡界面
创建MyPushButton类,自定义按钮控件,继承QPushButton类,
#include "mypushbutton.h"
#include "QDebug"
//声明中有设置默认参数,实现中就不能写有默认参数
MyPushButton:: MyPushButton(QString normalImg,QString pressImg)
{
this->normalImgPath=normalImg;
this->pressImgPath=pressImg;
QPixmap pix;
bool ret=pix.load(normalImg);
if(!ret)
{
qDebug()<<"图片加载失败!";//测试输出内容
return;
}
//加载成功,设置图片固定大小
this->setFixedSize(pix.width(),pix.height());
//设置不规则图片样式
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pix);
//设置图标
this->setIcon(pix);
this->setIconSize(QSize(pix.width(),pix.height()));
}
在mainpage.cpp中创建并实例化开始按钮,设置大小等属性。
/*开始按钮*/
MyPushButton * startBtn=new MyPushButton(":/icon/icno/MenuSceneStartButton.png");
startBtn->setParent(this);
startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);
创建关卡界面selectlevelpage.cpp,点击“START”按钮进入选择关卡界面。
//监听点击事件
connect(startBtn,&MyPushButton::clicked,[=](){
//延时进入关卡页面
QTimer::singleShot(500,this,[=](){
this->hide();//当前页面隐藏
selectLevePage->show();//显示选择关卡页面
});
});