我如何才能在Qt中返回到main

How can I return to main in Qt?

本文关键字:返回 main Qt      更新时间:2023-10-16

我最近一直在做一个项目,这是一个简单的游戏。我在主函数(文件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()功能,并且不会重新启动程序。