Results 1 to 6 of 6

Thread: Dialog Class inheriting QDialog Disappears Quickly After Loading.

  1. #1
    Join Date
    May 2011
    Location
    Utah
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Dialog Class inheriting QDialog Disappears Quickly After Loading.

    Hi Guys,

    So this problem has stumped me for a while. I have a class that inherits QDialog. When I call it from my class that inherits QObject, it disappears quickly after loading.

    I have read many posts about making sure you create the dialog on the heap rather than the stack. I am following this advice but am still having troubles.

    I have included my relevant code as well as a diagram showing the hierarchy of our program.



    Qt Code:
    1. //GameLogic.h
    2.  
    3. #include <vector>
    4. #include <string>
    5. #include <iostream>
    6. #include <ctime>
    7. #include <QObject>
    8. #include "PlayerMenu.h"
    9. #include "AI.h"
    10. #include "GameBoard.h"
    11. #include <QtGui/QApplication>
    12. #include <qpointer.h>
    13. using namespace std;
    14.  
    15. class GameLogic : public QObject
    16. {
    17. Q_OBJECT
    18. QPointer<PlayerMenu> player_menu; //the menu for the player
    19. Player* human_player;
    20.  
    21. public:
    22. //constructor
    23. GameLogic(string player_name, int num_of_AI, QWidget *parent = 0);
    24.  
    25. //destructor
    26. ~GameLogic();
    27. };//end of Class
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // GameLogic.cpp : Liars Dice
    2. #include "GameLogic.h"
    3. #include "time.h"
    4. #include <iostream>
    5.  
    6. //Constructor with user defined number of AI players 1-4
    7. GameLogic::GameLogic (string player_name, int num_of_ai, QWidget *parent)
    8. {
    9. player_menu = new PlayerMenu(parent);
    10.  
    11. //set up the rest of the connectors
    12. connect(player_menu, SIGNAL(sig_makeBet(int, int)), this, SLOT(bet(int, int)));
    13. connect(player_menu, SIGNAL(sig_startRound()), this, SLOT(slot_startRound()));
    14. connect(player_menu, SIGNAL(sig_moveMade()),this, SLOT(moveMade()));
    15.  
    16. player_menu->show();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef PLAYERMENU_H
    2. #define PLAYERMENU_H
    3.  
    4. #include <iostream>
    5. #include <stdio.h>
    6. #include <stdlib.h>
    7. #include <time.h>
    8. #include <string>
    9. #include "ui_PlayerMenu.h"
    10. #include <qdialog.h>
    11. #include "GameBoard.h"
    12.  
    13. //class GameLogic;
    14.  
    15. using namespace std;
    16. class PlayerMenu : public QDialog
    17. {
    18. Q_OBJECT
    19. public:
    20. PlayerMenu(QWidget *parent=0);
    21. ~PlayerMenu();
    22. ...
    23. ....
    24. }#endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //PlayerMenu.cpp : Liar's Dice
    2. #include "PauseMenu.h"
    3. #include <qpushbutton.h>
    4. #include <qmessagebox.h>
    5. #include <iostream>
    6. #include <sstream>
    7.  
    8. //Default constructor
    9. PlayerMenu::PlayerMenu(QWidget *parent)
    10. : QDialog(parent)
    11. {
    12. //set board
    13. ui.setupUi(this);//playermenu
    14.  
    15. //initialize variable
    16. move_not_made = true;
    17. player_move = 0;
    18. sec = 0;
    19.  
    20. //set these values to prevent players from
    21. //making illegal moves.
    22. minDie = 0;
    23. minVal = 0;
    24. ui.sb_diceCount->setMinimum(minDie);
    25. ui.tb_newsFeed->setPlainText("");
    26.  
    27. //set listenens for buttons
    28. connect(ui.btn_Bet, SIGNAL(clicked()), this, SLOT(Bet()));
    29. connect(ui.btn_callBluff, SIGNAL(clicked()), this, SLOT(callBluff()));connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(pushedOne()));
    30. connect(ui.btn_callSpotOn, SIGNAL(clicked()), this, SLOT(callSpotOn()));connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(pushedTwo()));
    31. connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(pushedThree()));
    32. connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(pushedFour()));
    33. connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(pushedFive()));
    34. connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(pushedSix()));
    35. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help!

    Edit:
    I forgot to also include that I want the PlayerMenu (QDialog) to be modeless. I have the PlayerMenu interacting with the GameLogic through signals.

    The main problem I have is that I want the game to start when I load the dialog.

    However if I put a signal in the constructor of PlayerMenu to start the game, then the game starts before the dialog is loaded.

    If I use exec() It blocks the game logic until the dialog is closed which defeats the purpose.
    Last edited by shandib1823; 12th May 2011 at 00:48. Reason: updated contents

  2. #2
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dialog Class inheriting QDialog Disappears Quickly After Loading.

    Use open with a "new" pointer, and connect the signals of accepted and rejected or something else that you need.
    The dialog is closing when the function reachs it's end. So, if you don't want to block the main thread, you have two options:

    - Open a new thread and run exec;
    - Use Dialog::open with new pointer in the same thread and connect it to slots, check the result and delete it;

    You can try this too:
    Qt Code:
    1. void mySlotThatOpensTheDialog()
    2. {
    3. PlayerMenu menu;
    4. menu.exec();
    5. }
    6.  
    7. void myFunctionThatWillCallTheDialog()
    8. {
    9. QTimer::singleShot(0, this, SLOT(mySlotThatOpensTheDialog()));
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Dialog Class inheriting QDialog Disappears Quickly After Loading.

    With "disappear" you mean it's getting closed or just hides behind other windows? On your code I can't see why it should be closed, the only guess I have is that the parent of GameLogic is being deletes and thus the dialog also.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dialog Class inheriting QDialog Disappears Quickly After Loading.

    When you create the player_menu object (Line 9 listing 2) you give it the parent of the containing GameLogic instance. More typically, it would have the GameLogic instance (i.e. this) as its parent. Was this deliberate?

    Is there a particular reason for using a guarded pointer around player_menu?

  5. #5
    Join Date
    May 2011
    Location
    Utah
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dialog Class inheriting QDialog Disappears Quickly After Loading.

    Thanks for you response! I tried your suggestion and it is still blocking. I am wondering if using a dialog is even the correct way to go. The PlayerMenu (QDialog) is exactly that. It is mean to be the GUI for game play.

    The reason why QMainWindow wasn't used was the because of the same problem we are facing now where the Window was disappearing too quickly.

    What do you suggest?

    Edit:
    Yes passing in parent was deliberate. When we would pass in this it was passing in the Game Logic Object. We used a guarded pointer because it was suggested to us by a fellow classmate.

    In our main.cpp we create the QMainWindow StartMenu which is still alive so the user just exits the dialog to go back to the main menu so we know that the Qapp.exec() is still alive. The GameLogic is also still alive. I think that the dialog is being destroyed.

    Thanks again!
    Last edited by shandib1823; 12th May 2011 at 07:16. Reason: updated contents

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dialog Class inheriting QDialog Disappears Quickly After Loading.

    Is the player_menu object being deleted or just hidden? This example I think captures your structure, and it works:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class PlayerMenu: public QDialog {
    5. Q_OBJECT
    6. public:
    7. PlayerMenu(QWidget *p = 0): QDialog(p) { }
    8. };
    9.  
    10.  
    11. class GameLogic : public QObject
    12. {
    13. Q_OBJECT
    14. PlayerMenu *player_menu; //the menu for the player
    15.  
    16. public:
    17. GameLogic(QString player_name, int num_of_AI, QWidget *parent = 0) {
    18. player_menu = new PlayerMenu(parent);
    19. player_menu->show();
    20. }
    21. ~GameLogic() {
    22. delete player_menu;
    23. }
    24. };
    25.  
    26.  
    27.  
    28. class StartMenu: public QMainWindow {
    29. Q_OBJECT
    30. public:
    31. StartMenu(QWidget *p = 0): QMainWindow(p), gl(0) {
    32. setCentralWidget(new QLabel("Main Window", this));
    33.  
    34. gl = new GameLogic("dummy", 1, this);
    35. }
    36. ~StartMenu() {
    37. delete gl;
    38. }
    39. public slots:
    40. private:
    41. GameLogic *gl;
    42. };
    43.  
    44. int main(int argc, char *argv[])
    45. {
    46. QApplication app(argc, argv);
    47.  
    48.  
    49. StartMenu m;
    50. m.show();
    51. return app.exec();
    52. }
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    The reason I asked about your QPointer is that I think you wanted a QScopedPointer in order that it do the memory deallocation during destruction.

Similar Threads

  1. Replies: 1
    Last Post: 7th March 2011, 14:02
  2. my window disappears quickly
    By Abeer in forum Newbie
    Replies: 2
    Last Post: 30th May 2010, 01:04
  3. Stop QDialog from inheriting parent's palette.
    By Avrohom in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2009, 04:59
  4. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39
  5. QDialog::exec() cereates no modal dialog
    By sboesch in forum Qt Programming
    Replies: 8
    Last Post: 27th March 2006, 17:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.