Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QT c++;使用信号和插槽从另一个类调用类的方法时崩溃_C++_Qt_Class_Qt Signals - Fatal编程技术网

C++ QT c++;使用信号和插槽从另一个类调用类的方法时崩溃

C++ QT c++;使用信号和插槽从另一个类调用类的方法时崩溃,c++,qt,class,qt-signals,C++,Qt,Class,Qt Signals,我正在尝试创建基于QQuickWidget的应用程序 我想做的是: 类A(game.h)和类B(gamestate.h)是向前声明的。类A是带有方法的主要QQuickWidget类。类QObject派生类包含信号、插槽、变量和方法 B类变量值可以从A类-工作 当变量值改变时,应发出信号——工作 当信号被发出时,槽方法应该在类B——工作中被调用 类B应该调用类a——working中的方法 A类应创建另一个qquickwidget--不工作 (无编译错误。应用程序在加载时崩溃) 我试着从类A调用sh

我正在尝试创建基于QQuickWidget的应用程序

我想做的是:

类A(game.h)和类B(gamestate.h)是向前声明的。类A是带有方法的主要QQuickWidget类。类QObject派生类包含信号、插槽、变量和方法

B类变量值可以从A类-工作

当变量值改变时,应发出信号——工作

当信号被发出时,槽方法应该在类B——工作中被调用

类B应该调用类a——working中的方法

A类应创建另一个qquickwidget--不工作 (无编译错误。应用程序在加载时崩溃)

我试着从类A调用showIntro()函数,结果很好。但当试图从B班打电话时,它不起作用

游戏

#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
还有我的最后一个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();
}
#包括
#包括
#包括“game.h”
游戏*游戏;
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
游戏=新游戏();
游戏->表演();
返回app.exec();
}

请告诉我可能的问题。

GameState
的成员变量
Game*Game
未初始化,因此程序在尝试取消引用
GameState::stateChanged()中的指针时崩溃

GameState
的构造函数更改为以下内容:

// 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)));
}
#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();
}

}
#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();
}
// 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)));
}