我最近一直在做一个简单的游戏项目。我在 main 函数(文件 main.cpp)中编写了以下代码:
ending_note = "Draw.";
End_Page end(ending_note, a);
end.show();
(*a).exec();
if(end.flag == 1)
{
return 1;
} //end if
其中 a 是 Qapplication 对象。End_Page 类定义如下(文件 end_page.cpp):
End_Page::End_Page(string _winner, QApplication* _a, QWidget *parent):QWidget(parent){
a = _a;
this->setFixedSize(900, 675);
this->move(350, 50);
flag = 0;
//------------------- background label
background = new QLabel(this);
QMovie* movie2 = new QMovie("..\\project\\Data\\pic\\7.jpeg");
movie2->setScaledSize(QSize(this->width(), 600));
background->setMovie(movie2);
background->setGeometry(0, 0, this->width(), 600);
movie2->start();
//-------------------- set label
QString s;
label = new QLabel(s.fromStdString(_winner), this);
label->setStyleSheet("QLabel { color : rgb(200, 0, 30); qproperty-alignment: AlignCenter; }");
QFont f( "MV Boli", 32, QFont::Bold);
label->setFont(f);
label->setGeometry(0,this->height() - 400, this->width(), 160);
question = new QLabel("Do you want to play again?\n", this);
question->setStyleSheet("QLabel { color : black;}");
question->setGeometry(375, 610, 200, 30);
accept = new QPushButton("Yes", this);
accept->setGeometry(300, 630, 80, 40);
decline = new QPushButton("No", this);
decline->setGeometry(500, 630, 80, 40);
//-------------------- connect
connect(this,SIGNAL(closeSignal()), this, SLOT(closeProgram()));
connect(decline, SIGNAL(clicked()), this, SLOT(closeProgram()));
connect(accept, SIGNAL(clicked()), this, SLOT(restartProgram()));
}
End_Page::~End_Page(){}
void End_Page::closeEvent(QCloseEvent* event){
emit closeSignal();
event->accept();
}
void End_Page::EndGame(){
a->exit();
}
void End_Page::closeProgram(){
exit(0);
}
void End_Page::restartProgram(){
flag = 1;
a->exit();
}
我的问题是,在程序执行语句 (*a).exec(); 之后,如果用户单击标记为 Yes 的按钮,程序将执行函数 restartProgram 到最后,但之后它不会继续回到函数 main (换句话说,它卡在那里)。我怎么解决这个问题?