我如何才能在Qt中返回到main
How can I return to main in Qt?
我最近一直在做一个项目,这是一个简单的游戏。我在主函数(文件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();之后;,如果用户单击标记为"是"的按钮,程序将执行函数restartProgram到底,但之后它就不会继续返回到函数main中(换句话说,它会被卡住)。我该如何解决这个问题?
尝试将quit()
或exit()
作为静态类成员调用(不需要传递QApplication
):
对于任何使用Qt的GUI应用程序,无论应用程序在任何给定时间是否具有0、1、2或更多窗口,都只有一个QApplication对象。对于非GUI Qt应用程序,请改用QCoreApplication,因为它不依赖于QtGui库。可以通过instance()函数访问QApplication对象,该函数返回一个相当于全局qApp指针的指针。
void End_Page::restartProgram(){
flag = 1;
QApplication::quit();
}
但是,您的应用程序中的主要问题是,您在closeEvent()
中使用closeSignal()
,并且连接到它的插槽将调用exit(0);
系统调用,我认为这是完全不必要的,这将"扼杀"当前进程。
下面是一个完全有效的例子:
#include <QApplication>
#include <qtimer>
#include <iostream>
/* Move this into h file and moc it! */
class Window:public QObject
{
Q_OBJECT
public slots:
void closeApp(){ QApplication::quit(); flag = 500; }
public:
int flag;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window win;
QTimer::singleShot(5000, &win, SLOT(closeApp()));
a.exec();
std::cout << "Flag: " << win.flag << std::endl;
return 0;
}
编辑
你为什么这么做:
if(end.flag == 1) // flag is set to 1 in restartProgram slot
{
return 1;
} //end if
这将退出您的main()
功能,并且不会重新启动程序。
相关文章:
- 为什么要从main()返回NULL?
- 使用 Main() 返回值作为 python 脚本的参数
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 我的运算符重载没有在我的 int main 中返回?
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 动态数组传递给 main 中用随机值填充的函数,返回值3221226356
- 内存浪费?如果main()应该只返回0或1,那么为什么main是用int而不是短int甚至char声明的
- 无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值
- 如何使类函数返回数组,然后将其调用并存储在 main 中
- 程序流并以安全而雄辩的方式正确返回Main()
- (C )当在同一类中调用时,污点指针返回正确的值,而从MAIN调用时为0
- 返回C 中的Main()
- 为什么当从递归函数返回字符串到C 中的Main时,我会得到分割错误
- 如何从子例程函数获取到 main 函数的返回值
- 即使将返回 0 作为 main 的第一行,程序也无法退出C++
- 在方法中更改类的私人价值不会将更改返回main()
- Bon appetit :从 int 返回类型函数在 main() 中打印字符串
- 为什么在C程序中从main()返回0
- 为什么代码不断跳转以返回main();
- c++ostringstream对象在返回main函数后导致未处理的异常