Qt中不知道没循环还是没执行
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
death=0;//判断游戏是否结束
/*qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
button1=new QPushButton;
icon=new QIcon("C:\\Users\\Seffrui_M\\Desktop\\mouse.png");
icon1=new QIcon("C:\\Users\\Seffrui_M\\Desktop\\deathmouse.png");
button1->setIcon(*icon);
button1->setIconSize(QSize(50,50));
button2=new QPushButton;
button2->setIcon(*icon);
button2->setIconSize(QSize(50,50));
button3=new QPushButton;
button3->setIcon(*icon);
button3->setIconSize(QSize(50,50));
button4=new QPushButton;
button4->setIcon(*icon);
button4->setIconSize(QSize(50,50));
button5=new QPushButton;
button5->setIcon(*icon);
button5->setIconSize(QSize(50,50));
button6=new QPushButton;
button6->setIcon(*icon);
button6->setIconSize(QSize(50,50));
button7=new QPushButton;
button7->setIcon(*icon);
button7->setIconSize(QSize(50,50));
button8=new QPushButton;
button8->setIcon(*icon);
button8->setIconSize(QSize(50,50));
button9=new QPushButton;
button9->setIcon(*icon);
button9->setIconSize(QSize(50,50));
h1=new QHBoxLayout;
h2=new QHBoxLayout;
h3=new QHBoxLayout;
v1=new QVBoxLayout;
h1->addWidget(button7);
h1->addWidget(button8);
h1->addWidget(button9);
h2->addWidget(button4);
h2->addWidget(button5);
h2->addWidget(button6);
h3->addWidget(button1);
h3->addWidget(button2);
h3->addWidget(button3);
v1->addLayout(h1);
v1->addLayout(h2);
v1->addLayout(h3);
setLayout(v1);*//游戏界面
connect(this,SIGNAL(GameOver()),this,SLOT(close()));
}
void Widget::keyPressEvent(QKeyEvent *event) //键盘响应
{
if(event->key()==Qt::Key_1)
emit button1->clicked();
else if(event->key()==Qt::Key_2)
emit button2->clicked();
else if(event->key()==Qt::Key_3)
emit button3->clicked();
else if(event->key()==Qt::Key_4)
emit button4->clicked();
else if(event->key()==Qt::Key_5)
emit button5->clicked();
else if(event->key()==Qt::Key_6)
emit button6->clicked();
else if(event->key()==Qt::Key_7)
emit button7->clicked();
else if(event->key()==Qt::Key_8)
emit button8->clicked();
else if(event->key()==Qt::Key_9)
emit button9->clicked();
}
void Widget::buttonchange(QPushButton *button)//将产生的老鼠图标转换
{
button->setIcon(*icon1);
connect(button,SIGNAL(clicked()),this,SLOT(changeback()));
}
void Widget::Creat()//随机产生老鼠
{
death=1;
int which_one=qrand()%9+1;
switch (which_one)
{
case 1:buttonchange(button1);break;
case 2:buttonchange(button2);break;
case 3:buttonchange(button3);break;
case 4:buttonchange(button4);break;
case 5:buttonchange(button5);break;
case 6:buttonchange(button6);break;
case 7:buttonchange(button7);break;
case 8:buttonchange(button8);break;
case 9:buttonchange(button9);break;
}
}
void Widget::changeback()//如果击中老鼠 就换回原来的图标
{
QPushButton *button=qobject_cast<QPushButton* >(sender());
button->setIcon(*icon);
death=0;
}
void Widget::play() //这里 只执行一次 不知道是什么问题
{
while(!death)
{
show();
Creat();
}
emit GameOver();//发出游戏结束的信号
}