【问题标题】:How to completely close the window in qt?如何在qt中完全关闭窗口?
【发布时间】:2015-06-15 14:30:03
【问题描述】:

我正在使用 C++ 和 Qt Creator 创建游戏。当我的生命值低于零时,我想退出当前应用程序并显示另一个屏幕,显示退出或再次播放。

我可以通过使用hide() 方法然后使用show() 方法显示新屏幕来做到这一点。

但是,如果我在游戏仍在后台运行时执行此操作,即使音乐被隐藏,我仍然可以听到播放的音乐。这样做的主要问题是,当我单击再次播放按钮并在新窗口中再次加载游戏时,分数和健康受到我隐藏的旧游戏中仍在发生的事情的影响。

有没有办法完全关闭窗口,让它退出游戏,但仍然加载我想要的下一个窗口?

【问题讨论】:

  • 这不太合理。如果您“退出应用程序”,那么无论您是否正常关闭,应用程序中播放的任何音乐都应该消失。 hide() 不会退出应用程序。

标签: c++ qt


【解决方案1】:

删除窗口实例并为下一个游戏创建一个新实例。或者保留它并实现适当的应用程序状态,以控制在游戏未处于运行状态时关闭音乐等内容。

【讨论】:

  • 如何删除窗口实例?
  • @user3019111 delete window;,类型为QWidget * window。如果窗口在堆栈上,则需要将其移动到堆中。一般而言,您的设计被破坏了,窗口应该知道应用程序处于什么状态 - 它应该知道在您希望它安静时如何安静。
【解决方案2】:

这听起来像是一个实现问题,这里有一个你可以做的例子:

//in the main header declare a pointer to your game.
GameClass *game;

//in the main class when the game is supposed to start.
game = new GameClass(parent);
connect(game, SIGNAL(GameOver(int)), this, SLOT(cleanUpGame(int)));
game->show();

//define the slot in the main class
function cleanUpGame(int code){
    switch(code) .... //check the game over code
    case USER_KILLED:
        if(game != NULL){
            delete game;
            game=NULL;
        }

    break;
}

//in the game class header class description add
signals:
void GameOver(int);

//in the game class when you are finished.
 emit GameOver(USER_KILLED);
 this->close();

【讨论】:

  • 不需要测试游戏是否为NULL。 “delete 0”被定义为什么都不做。设置game=0是一个好习惯;空值;或者 nullptr,如果你想看中 c++11。
猜你喜欢
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 2010-10-14
  • 2021-04-09
  • 2015-08-10
相关资源
最近更新 更多