app is closing after going back to main window / c++

Suthura Sudharaka

In my main window i used this code to open my game application

void MainWindow::on_playButton_clicked(){   
    CSpaceInwaders* pGame = new CSpaceInwaders(qApp->screens()[0]->size());
    pGame->showFullScreen();
    pGame->Run();
}

Then in there this is the run function that I called

void CSpaceInwaders::Run(){
scene()->clear();
setCursor(Qt::BlankCursor);

m_pCannon =new CCannon(EColor::Red);
m_pCannon->setPos(m_onScreenSize.width()/2,m_onScreenSize.height()-gCannonSize.height());
m_pCannon->setFlag(QGraphicsItem::ItemIsFocusable);
m_pCannon->setFocus();
scene()->addItem(m_pCannon);

connect(m_pCannon, &CCannon::sigIncreaseScore,this,&CSpaceInwaders::onIncreaseScore);
connect(m_pCannon, &CCannon::sigDecreseScore,this,&CSpaceInwaders::onDecreseScore);

m_pPoints = new CPoints();
scene()->addItem(m_pPoints);

QTimer* pTimer = new QTimer(this);
connect(pTimer, &QTimer::timeout,this,&CSpaceInwaders::onCreateEnemy);
pTimer->start(2000);}

after the game over I want to go back to my main window. So I used this function

void CSpaceInwaders::onGameOver(){
scene()->clear();
QMessageBox msgBox;
msgBox.setText("Game.");
msgBox.setInformativeText("You got hit ! Game Over");
msgBox.setStandardButtons(QMessageBox::Ok);
int ret = msgBox.exec();
switch (ret) {
  case QMessageBox::Ok:
    close();
    MainWindow w;
    w.show();
}}

This takes me back to the main window but after few seconds it closes.

I want to know how to fix this

Note : Created using QT

Some programmer dude

The problem is with the code in the case:

case QMessageBox::Ok:
    close();
    MainWindow w;
    w.show();

The problem is two-fold: First of all you can't actually define variables inside a case like that. You need to add a scope. I'm surprised the compiler doesn't yell at you for that.

The second problem (and that's causing your problem) is that the variable w is a local variable inside the switch statement. Once the statement ends so does the life-time and w and it's destructed and ceases to exist.

The solution (as far as I know) seems to be simple: Don't create and open a new main window! When you start the "space invaders" game you never close the original main window, it's should still be running in the background.

However this is a very bad way to "run" what should essentially be either a separate program or at the very least part of your normal program flow and event loop. Either extract the mini-game into its own program that you then load and execute, or don't create a separate application object and just open a normal window and let the main application event loop handle it.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

app closes after closing an activity

분류에서Dev

Closing a window after the page is fully loaded

분류에서Dev

Java Swing: How to keep parent window open after closing JFileChooser

분류에서Dev

A way for saving data after closing a C program

분류에서Dev

App Force Closing after adding AdMob on a full screen image view

분류에서Dev

Win10 snipping tool: clipboard empty after closing app

분류에서Dev

Switch back to main text after writing a comment in Word 2010/2013

분류에서Dev

app crashes when going back from gallery without selecting any image

분류에서Dev

Close Modal Window with back button on Cordova Android app

분류에서Dev

IOS Back to main page

분류에서Dev

Can't go back to wxpython main frame after creating and destroying Dialogue using ShowModal via Pubsub

분류에서Dev

Circles in a slideshow not going back to non-active

분류에서Dev

Closing browser window using VBA, excel

분류에서Dev

c# odbcconnection not closing

분류에서Dev

Storing a Image in Same folder in C# Window Form App

분류에서Dev

To disable back button in popup window

분류에서Dev

Layout only shows children after screen turn off or leaving the app and coming back

분류에서Dev

listbox highlights the selecteditem even after coming back in mvvm windows phone app

분류에서Dev

Redirect to logout link after closing the last tab

분류에서Dev

Keep Application running after Closing Event in WPF

분류에서Dev

Preg match all after closing img

분류에서Dev

Closing file after using to_csv()

분류에서Dev

view job in unix after closing putty

분류에서Dev

Can an embedded cocos2d-js app call back out to c++?

분류에서Dev

prevent back not working after refresh

분류에서Dev

Why does my C++ function, only when it's placed after main(), not work?

분류에서Dev

Code after showDialog is not execute when the modal window is closed by the other thread -- C#

분류에서Dev

C # Form Closing 이벤트

분류에서Dev

Extjs window is skewed after destoy

Related 관련 기사

  1. 1

    app closes after closing an activity

  2. 2

    Closing a window after the page is fully loaded

  3. 3

    Java Swing: How to keep parent window open after closing JFileChooser

  4. 4

    A way for saving data after closing a C program

  5. 5

    App Force Closing after adding AdMob on a full screen image view

  6. 6

    Win10 snipping tool: clipboard empty after closing app

  7. 7

    Switch back to main text after writing a comment in Word 2010/2013

  8. 8

    app crashes when going back from gallery without selecting any image

  9. 9

    Close Modal Window with back button on Cordova Android app

  10. 10

    IOS Back to main page

  11. 11

    Can't go back to wxpython main frame after creating and destroying Dialogue using ShowModal via Pubsub

  12. 12

    Circles in a slideshow not going back to non-active

  13. 13

    Closing browser window using VBA, excel

  14. 14

    c# odbcconnection not closing

  15. 15

    Storing a Image in Same folder in C# Window Form App

  16. 16

    To disable back button in popup window

  17. 17

    Layout only shows children after screen turn off or leaving the app and coming back

  18. 18

    listbox highlights the selecteditem even after coming back in mvvm windows phone app

  19. 19

    Redirect to logout link after closing the last tab

  20. 20

    Keep Application running after Closing Event in WPF

  21. 21

    Preg match all after closing img

  22. 22

    Closing file after using to_csv()

  23. 23

    view job in unix after closing putty

  24. 24

    Can an embedded cocos2d-js app call back out to c++?

  25. 25

    prevent back not working after refresh

  26. 26

    Why does my C++ function, only when it's placed after main(), not work?

  27. 27

    Code after showDialog is not execute when the modal window is closed by the other thread -- C#

  28. 28

    C # Form Closing 이벤트

  29. 29

    Extjs window is skewed after destoy

뜨겁다태그

보관