本项目的下载地址:戳此访问

这一篇实现按钮控件的实现.

                               

实现按钮的响应功能无非就是建立信号与槽,这里不能使用右键->转到槽的操作,原因如下.

首先在mainwindow.cpp中写connect函数,因为按钮按下启动计时器工作,所以接收信号者并非this而是this->scene,

 connect(ui->btn_start,&QPushButton::clicked,this->scene,&myScene::startGame);
 connect(ui->btn_pause,&QPushButton::clicked,this->scene,&myScene::pauseGame);
 connect(ui->btn_end,&QPushButton::clicked,this->scene,&myScene::stopGame);

startGame函数定义如下: 

void myScene::startGame()
{
    this->ptimer->start(800);

}

由此,依次编写停止键和终止键

void myScene::pauseGame()
{
    this->ptimer->stop();

}

void myScene::stopGame()
{
     this->ptimer->stop();
    //每次重置背景
       for(int i = 0; i < 16; i++){
      this->item[i]->setPic(":/background/bg1.png");}
}

这是已经实现了按钮控件的操作,但是鼠标事件应该只发生在开始键按下时,停止键和结束键都不应该产生响应,故添加

          


void myitem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  if(this->isStart()){
      if(this->isMouse()){
          qDebug() << "mouse";
      }else {
          qDebug() << "notmouse";
      }
  }

}

                 

下一篇时打地鼠的终结篇,实现分数的显示,敬请期待