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.
//GameLogic.h
#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include <QObject>
#include "PlayerMenu.h"
#include "AI.h"
#include "GameBoard.h"
#include <QtGui/QApplication>
#include <qpointer.h>
using namespace std;
{
Q_OBJECT
QPointer<PlayerMenu> player_menu; //the menu for the player
Player* human_player;
public:
//constructor
GameLogic
(string player_name,
int num_of_AI,
QWidget *parent
= 0);
//destructor
~GameLogic();
};//end of Class
#endif
//GameLogic.h
#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include <QObject>
#include "PlayerMenu.h"
#include "AI.h"
#include "GameBoard.h"
#include <QtGui/QApplication>
#include <qpointer.h>
using namespace std;
class GameLogic : public QObject
{
Q_OBJECT
QPointer<PlayerMenu> player_menu; //the menu for the player
Player* human_player;
public:
//constructor
GameLogic(string player_name, int num_of_AI, QWidget *parent = 0);
//destructor
~GameLogic();
};//end of Class
#endif
To copy to clipboard, switch view to plain text mode
// GameLogic.cpp : Liars Dice
#include "GameLogic.h"
#include "time.h"
#include <iostream>
//Constructor with user defined number of AI players 1-4
GameLogic
::GameLogic (string player_name,
int num_of_ai,
QWidget *parent
){
player_menu = new PlayerMenu(parent);
//set up the rest of the connectors
connect(player_menu, SIGNAL(sig_makeBet(int, int)), this, SLOT(bet(int, int)));
connect(player_menu, SIGNAL(sig_startRound()), this, SLOT(slot_startRound()));
connect(player_menu, SIGNAL(sig_moveMade()),this, SLOT(moveMade()));
player_menu->show();
}
// GameLogic.cpp : Liars Dice
#include "GameLogic.h"
#include "time.h"
#include <iostream>
//Constructor with user defined number of AI players 1-4
GameLogic::GameLogic (string player_name, int num_of_ai, QWidget *parent)
{
player_menu = new PlayerMenu(parent);
//set up the rest of the connectors
connect(player_menu, SIGNAL(sig_makeBet(int, int)), this, SLOT(bet(int, int)));
connect(player_menu, SIGNAL(sig_startRound()), this, SLOT(slot_startRound()));
connect(player_menu, SIGNAL(sig_moveMade()),this, SLOT(moveMade()));
player_menu->show();
}
To copy to clipboard, switch view to plain text mode
#ifndef PLAYERMENU_H
#define PLAYERMENU_H
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "ui_PlayerMenu.h"
#include <qdialog.h>
#include "GameBoard.h"
//class GameLogic;
using namespace std;
{
Q_OBJECT
public:
~PlayerMenu();
...
....
}#endif
#ifndef PLAYERMENU_H
#define PLAYERMENU_H
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "ui_PlayerMenu.h"
#include <qdialog.h>
#include "GameBoard.h"
//class GameLogic;
using namespace std;
class PlayerMenu : public QDialog
{
Q_OBJECT
public:
PlayerMenu(QWidget *parent=0);
~PlayerMenu();
...
....
}#endif
To copy to clipboard, switch view to plain text mode
//PlayerMenu.cpp : Liar's Dice
#include "PauseMenu.h"
#include <qpushbutton.h>
#include <qmessagebox.h>
#include <iostream>
#include <sstream>
//Default constructor
PlayerMenu
::PlayerMenu(QWidget *parent
){
//set board
ui.setupUi(this);//playermenu
//initialize variable
move_not_made = true;
player_move = 0;
sec = 0;
//set these values to prevent players from
//making illegal moves.
minDie = 0;
minVal = 0;
ui.sb_diceCount->setMinimum(minDie);
ui.tb_newsFeed->setPlainText("");
//set listenens for buttons
connect(ui.btn_Bet, SIGNAL(clicked()), this, SLOT(Bet()));
connect(ui.btn_callBluff, SIGNAL(clicked()), this, SLOT(callBluff()));connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(pushedOne()));
connect(ui.btn_callSpotOn, SIGNAL(clicked()), this, SLOT(callSpotOn()));connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(pushedTwo()));
connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(pushedThree()));
connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(pushedFour()));
connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(pushedFive()));
connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(pushedSix()));
}
//PlayerMenu.cpp : Liar's Dice
#include "PauseMenu.h"
#include <qpushbutton.h>
#include <qmessagebox.h>
#include <iostream>
#include <sstream>
//Default constructor
PlayerMenu::PlayerMenu(QWidget *parent)
: QDialog(parent)
{
//set board
ui.setupUi(this);//playermenu
//initialize variable
move_not_made = true;
player_move = 0;
sec = 0;
//set these values to prevent players from
//making illegal moves.
minDie = 0;
minVal = 0;
ui.sb_diceCount->setMinimum(minDie);
ui.tb_newsFeed->setPlainText("");
//set listenens for buttons
connect(ui.btn_Bet, SIGNAL(clicked()), this, SLOT(Bet()));
connect(ui.btn_callBluff, SIGNAL(clicked()), this, SLOT(callBluff()));connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(pushedOne()));
connect(ui.btn_callSpotOn, SIGNAL(clicked()), this, SLOT(callSpotOn()));connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(pushedTwo()));
connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(pushedThree()));
connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(pushedFour()));
connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(pushedFive()));
connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(pushedSix()));
}
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.
Bookmarks