Amal Raj
Amal Raj

Reputation: 13

QT c++ crashes while calling method of class from another class using signals and slots

I am trying to create QQuickWidget based application.

What i am trying to do:

Class A(game.h) and Class B(gamestate.h) are forward declared. Class A is main QQuickWidget class with methods. Class B QObject derived class contains signals, slots, variables and methods.

Class B Variable values can set from class A -- Working

When variable value changes signal should be emitted -- working

when signal was emitted slot method should be called in class B -- working

Class B should invoke a method in class A -- working

Class A should create another qquickwidget -- NOT WORKING (No compiling error. Application crashes on load)

I tried to call from class A and showIntro() function working fine. But when tried to call from class B its not working.

Game.h

#ifndef GAME_H
#define GAME_H
#include <QQuickWidget>
class GameState;

class Game: public QQuickWidget
{
Q_OBJECT
public:
   Game();
   GameState *gameState;
   void showIntro();
public slots:
   void onStatusChanged(QQuickWidget::Status);
};

#endif // GAME_H

Game.cpp

#include "game.h"
#include <QQuickWidget>
#include <QDebug>
#include "gamestate.h"

Game::Game(): QQuickWidget()
{
   gameState = new GameState(this);
   mainScreen = new QQuickWidget();
   connect(this, SIGNAL(statusChanged(QQuickWidget::Status)), this,    SLOT(onStatusChanged(QQuickWidget::Status)));

   setFixedSize(450, 710);
   setSource(QUrl("qrc:/EmptyScreen.qml"));

}

void Game::onStatusChanged(QQuickWidget::Status status)
{

switch(status)
{
    case QQuickWidget::Ready:
        qDebug() << "hi";
        gameState->setValue(1);
        //showIntro();
        break;
    case QQuickWidget::Error:
        qDebug() << "Error";
        break;
}
}
void Game::showIntro()
{
  mainScreen->setSource(QUrl("qrc:/MainScreen.qml"));
  mainScreen->setAttribute(Qt::WA_TranslucentBackground);
  mainScreen->setParent(this);
}

Here is my Gamestate.h

#ifndef GAMESTATE_H
#define GAMESTATE_H

#include <QObject>


class Game;


class GameState : public QObject
{
 Q_OBJECT
public:
   explicit GameState(QObject *parent = 0);

   int value() const {return m_value; }
   Game *game;
signals:
   void valueChanged(int newValue);

public slots:
   void setValue(int value);
   void stateChanged(int value);
private:
   int m_value;
};

#endif // GAMESTATE_H

GameState.cpp

#include "gamestate.h"
#include "game.h"

GameState::GameState(QObject *parent) : QObject(parent)
{
   m_value = 0;
   connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}

void GameState::setValue(int value)
{
  if(value != m_value)
{
   m_value = value;
   emit valueChanged(value);
}

}

void GameState::stateChanged(int value)
{
   if(value == 1)
{
    game->showIntro();
}

}

and my final main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include "game.h"

Game *game;

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

game = new Game();
game->show();
return app.exec();
}

Pls suggest me what could be the issue.

Upvotes: 1

Views: 994

Answers (1)

m.s.
m.s.

Reputation: 16354

Member variable Game* game of class GameState is not initialized and therefore the program crashes when trying to dereference the pointer within GameState::stateChanged().

Change the constructor of GameState to the following:

// in gamestate.h
explicit GameState(Game *parent = 0);

// in gamestate.cpp
GameState::GameState(Game *parent) : QObject(parent), game(parent)
{
   m_value = 0;
   connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}

Upvotes: 2

Related Questions