本文共 2371 字,大约阅读时间需要 7 分钟。
目录~
设计这个界面,其实也不难。
设置标题,图标,尺寸
首先新建类,就叫 c h o o s e s e n c e choosesence choosesence吧
上来先设置一下标题,图标和窗口大小
this->setFixedSize(QSize(520,688)); this->setWindowIcon(QPixmap(":/res/Coin0001.png")); this->setWindowTitle("选择关卡");
设置菜单退出
然后设置一下菜单的退出选项
按照 Q M e n u B a r − Q M e n u − Q A c t i o n QMenuBar-QMenu-QAction QMenuBar−QMenu−QAction的层级关系一个一个设置下来
最后用 c o n n e c t connect connect给退出 a c t i o n action action绑定事件,触发时让当前窗口关闭
//下面是设置菜单 QMenuBar *Men = new QMenuBar(); this->setMenuBar(Men); QMenu *option = Men->addMenu(QString("开始")); QAction *exit = option->addAction(QString("退出")); connect(exit,&QAction::triggered,[=](){ this->close(); });
设置窗口背景
然后设置一下窗口的背景
还是一样重写 p a i n t E v e n t paintEvent paintEvent方法
void Choosescence::paintEvent(QPaintEvent *){ QPainter painter(this); QPixmap map; map.load(":/res/OtherSceneBg.png"); painter.drawPixmap(0,0,this->width(),this->height(),map); map.load(":/res/Title.png"); painter.drawPixmap( (this->width()-map.width())*0.5,30,map);}
设置20个选关按钮
最后来到了我们的核心,中间的 20 20 20个选关按钮
但是这是不规则按钮??没关系,我们写好了自己的不规则按钮类
那么我们直接 f o r for for循环 20 20 20次,计算当前按钮属于第几行第几列
然后移动到相应的位置即可
但是按钮中间还有一个数字表示关卡号
加入直接用按钮的 s e t T e x t setText setText方法太丑了(因为按钮不规则导致的)
没关系,我们用 l a b e l label label设置数字,然后移动到相同坐标就可以啦!!!
但是这样发现点击按钮没反应了!!原因是 l a b e l label label直接挡住了按钮
我们还需要设置 l a b e l label label的属性,鼠标穿透性
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//文字的居中表示 //设置鼠标进行穿透 label->setAttribute(Qt::WA_TransparentForMouseEvents);//鼠标穿透
//设置4*5个按钮 int x = 0,y = 0; //监听每个按钮的点击事件 for(int i=0;i<20;i++) { MyPushButton *circle = new MyPushButton(":/res/LevelIcon.png");//新建按钮 circle->setParent(this);//绑定父亲 x = 70+i%4*100, y = 144+i/4*100;//计算坐标 circle->move(x,y); connect(circle,&MyPushButton::clicked,[=](){ //触发按钮打开游戏 qDebug() << QString("你选择了第%1关").arg(i+1) ; this->hide(); play = new playScence(i+1); play->show(); }); QLabel *label = new QLabel; label->setParent(this); label->setFixedSize(circle->width(),circle->height()); label->setText(QString::number(i+1) ); label->move(x,y); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置鼠标进行穿透 label->setAttribute(Qt::WA_TransparentForMouseEvents); }
这样就大功告成啦!!!
转载地址:https://issue-is-vegetable.blog.csdn.net/article/details/113662283 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!