jakethesnake86 0 Newbie Poster

Below I have a small portion of the code I have been writing for QT GUI. It is important for my design that I am able to use signals and slots within my GamePanel class, but for some reason it will not work there! When I click on the button I have created within the "new game" panel (open it by using the game drop down menu and clicking on "new game"), it should display a message to console saying "clicked!!!!!".

I can make it work fine within my TicTacToeWindow class, but for some reason it will not work within the GamePanel class and I cannot figure out why!!

Any help would be greatly appreciated!!!

Thanks,
Jake

/********************************************************************************************************************************
* @file: hw5.h
* @purpose: Specification for tic-tac-toe application 
* @author: Jake Bruce
* @date: 3/19/09
* @version: N/A
*******************************************************************************************************************************/
#include "QtGui"

enum CurPanel {REGUSER, LOGIN, WELCOME, PASSWORD, GAME};

/*******************************************************************************************************************************
* @class: TicTacToeWindow
* @purpose: The main window for the Tic-Tac-Toe Application, inside of which all five of the application's panels reside. This
* window also consists of a tool bar with drop-down menus through which the user can acess the different panels.
*******************************************************************************************************************************/
class TicTacToeWindow : public QMainWindow
{
		Q_OBJECT
 public:
	/********************************************************************************************************************************
	* @purpose: Constructor which creates the application and carries out all tasks necessary in doing so
	*******************************************************************************************************************************/
	TicTacToeWindow();

	/********************************************************************************************************************************
	* @purpose: Create the five panels to be used in this application
	*******************************************************************************************************************************/
	void createPanels();	

	/********************************************************************************************************************************
	* @purpose: Set up the actions of the "User" drop-down menu
	*******************************************************************************************************************************/
	void setUserMenu();

	/********************************************************************************************************************************
	* @purpose: Set up the actions of the "Game" drop-down menu
	*******************************************************************************************************************************/
	void setGameMenu();

	/********************************************************************************************************************************
	* @purpose: Set up the actions of the "Edit" drop-down menu
	*******************************************************************************************************************************/
	void setEditMenu();

	/********************************************************************************************************************************
	* @purpose: Add an action to the drop down menu and to the action group
	* @param: The text to be displayed on the menu with the action and the menu to which the action will be added
	* @return: The created action so that it can be stored and used when needed
	*******************************************************************************************************************************/
	QAction* addMenuAction(QString actiontext, QMenu* actionmenu);

	/********************************************************************************************************************************
	* @purpose: Set the appropriate panel (one of the five) to be the central widget of the mainwindow
	* @param: One of the five panels' parent widgets, the selected panel depends upon user selections from the drop down menus
	*******************************************************************************************************************************/
	void setPanel(QWidget* parent);
 
 public slots:
	/********************************************************************************************************************************
	* @purpose: Respond appropriately when the user clicks any drop down menu item
	* @param: The action which the user has selected to have carried out (by clicking on the respective element of one of the 
	* drop-down menus)
	*******************************************************************************************************************************/
	void actionResponse(QAction* action);

 private:
	/* parent of all five Panels */
	QWidget* m_ParentWidget;
	
	/* layout associated with the parent of all five panels is QStackedLayout so only one panel's parent appears at a given time (based on QActions) */
	QStackedLayout* m_ParentLayout;

	/* integer array representation of five possible widgets that can appear on the stacked layout at any given time based on user input */
	int m_CurWidgetNum[5]; 

	/* the parents of the five panels (children of each respective parent are the user defined classes derived from QWidget)*/
	QWidget* m_LoginPanel;	
	QWidget* m_RegPanel;
	QWidget* m_WelcomePanel;
	QWidget* m_PasswordPanel;
	QWidget* m_GamePanel;	

	/* the User drop down menu and its associated actions */
	QMenu* m_User;
	QAction* m_RegisterUser;
	QAction* m_LogOutUser;
	QAction* m_Exit;

	/* the Game drop down menu and its associated actions */
	QMenu* m_Game;
	QAction* m_EndGame;
	QAction* m_NewGame;

	/* the Edit drop down menu and its associated action */
	QMenu* m_Edit;
	QAction* m_ChangePassword;
	
	/* the action group */
	QActionGroup* m_ActionGroup;
};


/*******************************************************************************************************************************
* @class: GamePanel
* @purpose: One of the application's five panels that allows a user to play the Tic-Tac-Toe game
*******************************************************************************************************************************/
class GamePanel : public QWidget
{
		Q_OBJECT
 public:
	friend class TicTacToeWindow;
	/********************************************************************************************************************************
	* @purpose: The GamePanel's constructor. Creates the entire game panel.  
	* @param: The GamePanel's parent. The panel's contents are added to the parent's layout. The parent is added to the stacked layout
	* to be displayed when the user requests it.
	*******************************************************************************************************************************/
	GamePanel(QWidget* parent);

 public slots:
	/********************************************************************************************************************************
	* @purpose: Respond to reparameterized button click signals sent from the QSignalMapper
	*******************************************************************************************************************************/
	void clickResponse(const QString& buttonclicked);

 private:
	QWidget* m_Parent;
	QWidget* m_GameBoard;

  QToolButton *m_Square1;

	QSignalMapper* m_SignalMapper;
};
/********************************************************************************************************************************
* @file: hw5.cpp
* 
* @purpose: Implementation for tic-tac-toe application
*  
* @author: Jake Bruce
* 
* @date: 3/19/09
* 
* @version: N/A
*******************************************************************************************************************************/

#include "hw5.h"

TicTacToeWindow::TicTacToeWindow()
{
	setWindowTitle("Tic-Tac-Toe Application");
	
	m_ActionGroup = new QActionGroup(this);	
		
	createPanels();

	setUserMenu();

	setGameMenu();

	setEditMenu();

	connect(m_ActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionResponse(QAction*)));

	this->show();
}

void TicTacToeWindow::createPanels()
{
	m_ParentWidget = new QWidget(this);

	m_ParentLayout = new QStackedLayout(m_ParentWidget);

	setCentralWidget(m_ParentWidget);

	m_GamePanel = new QWidget(m_ParentWidget);
	GamePanel gamechild(m_GamePanel); 
	m_CurWidgetNum[GAME] = m_ParentLayout->addWidget(m_GamePanel);
}

void TicTacToeWindow::setUserMenu()
{
	m_User = new QMenu("User", this);
	menuBar()->addMenu(m_User);

	m_RegisterUser = addMenuAction("Register User", m_User);
	m_LogOutUser = addMenuAction("Logout User", m_User);
	m_Exit = addMenuAction("Exit", m_User);
}

void TicTacToeWindow::setGameMenu()
{
	m_Game = new QMenu("Game", this);
	menuBar()->addMenu(m_Game);

	m_NewGame = addMenuAction("New game", m_Game);
	m_EndGame = addMenuAction("End game", m_Game);	
}

void TicTacToeWindow::setEditMenu()
{
	m_Edit = new QMenu("Edit", this);
	menuBar()->addMenu(m_Edit);

	m_ChangePassword = addMenuAction("Change Password", m_Edit);
}

QAction* TicTacToeWindow::addMenuAction(QString actiontext, QMenu* actionmenu)
{
	QAction* newaction = new QAction(actiontext, this);
	actionmenu->addAction(newaction);
	m_ActionGroup->addAction(newaction);
	return newaction;
}

void TicTacToeWindow::actionResponse(QAction* action)
{
	if(action == m_LogOutUser)
	{
		qDebug() << "log out";
	}
	if(action == m_Exit)
	{
		qDebug() << "exit";
	}
	if(action == m_NewGame)
	{
		qDebug() << "new game";
		m_ParentLayout->setCurrentWidget(m_GamePanel);
	}
	if(action == m_EndGame)
	{
		qDebug() << "end game";
	}
	if(action == m_ChangePassword)
	{
		qDebug() << "change password";
	}
}
/********************************************************************************************************************************
* @file: panels.cpp
* 
* @purpose: Implementation for the QWidget derived classes that will serve as the five central windows used by the Tic-Tac-Toe
* Application. These include the panels: Main Login, Register User, Welcome User, Change Password, and Tic-Tac-Toe Game.
*  
* @author: Jake Bruce
* 
* @date: 3/19/09
* 
* @version: N/A
*******************************************************************************************************************************/

#include "hw5.h"

GamePanel::GamePanel(QWidget* parent) : m_Parent(parent)
{
	QHBoxLayout* layout = new QHBoxLayout(parent); //layout for the entire panel

	//the game
	m_GameBoard = new QWidget(parent); //the game's widget
	
	layout->addWidget(m_GameBoard);
	
	m_SignalMapper = new QSignalMapper(m_GameBoard);

  m_Square1 = new QToolButton(m_GameBoard);

	connect(m_Square1, SIGNAL(clicked()), m_SignalMapper, SLOT(map()));
	m_SignalMapper->setMapping(m_Square1, "square1");
	connect(m_SignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(clickResponse(const QString &)));
}

void GamePanel::clickResponse(const QString& buttonclicked)
{
	qDebug() << "click!!!!!!!!!!!!!!!!!!!";
}
/********************************************************************************************************************************
* @file: hw5-client.cpp
* 
* @purpose: Client code for tic-tac-toe application
*  
* @author: Jake Bruce
* 
* @date: 3/19/09
* 
* @version: N/A
*******************************************************************************************************************************/

#include "hw5.h"

int main(int argc, char* argv[])
{
	QApplication myApp(argc, argv);
	TicTacToeWindow obj1;
	return myApp.exec();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.