[英]How to completely close the window in qt?
我正在使用C ++和Qt Creator創建游戲。 當我的健康狀況降至零以下時,我想退出當前應用程序並顯示另一個屏幕,該屏幕顯示退出或再次播放。
我可以通過使用hide()
方法,然后使用show()
方法show()
新屏幕來做到這一點。
但是,如果在游戲仍在后台運行時執行此操作,即使隱藏了音樂,我仍然可以聽到播放的音樂。 這樣做的主要問題是,當我單擊“再次玩”按鈕並再次在新窗口中加載游戲時,得分和健康狀況會受到我隱藏的舊游戲中仍然發生的情況的影響。
有沒有一種方法可以完全關閉窗口,使其退出游戲,但仍然加載我想要的下一個窗口?
刪除窗口實例,並為下一個游戲創建一個新的實例。 或者保留它並實現適當的應用程序狀態,這些狀態可控制在游戲不處於運行狀態時關閉音樂之類的內容。
聽起來像一個實現問題,這是您可以做的一個例子:
//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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.