I try to connect two classes and this error marks me, I want to connect the signal void puntaje (int )
of duGRaphicsscene.h
with that slot
void mostrar_puntos (int puntos)
of game.h
these are my classes:
dugraphicsscene.h
#ifndef DUGRAPHICSSCENE_H
#define DUGRAPHICSSCENE_H
#include <QObject>
#include <QGraphicsScene>
class DuThreadTimer;
class DuBallItem;
class DuPlatformItem;
class game;
class DuGraphicsScene : public QGraphicsScene
{
public:
DuGraphicsScene(QObject *parent = nullptr);
void startScene();
void stopScene();
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
private:
void createObjects();
void configureObjects();
void connectObjects();
void uodateScene();
void checkCollitions();
DuThreadTimer *mThreadTimer;
DuBallItem *mBallItem;
DuPlatformItem *mPlatformItem;
game *obj_game;
int puntos_niv = 0;
int puntos = 0;
int nivel = 1;
signals:
void puntaje(int );
};
game.h:
#ifndef GAME_H
#define GAME_H
#include <QMainWindow>
#include "dugraphicsscene.h"
namespace Ui {
class game;
}
class DuArkanoid;
class game : public QMainWindow
{
Q_OBJECT
public:
explicit game(QWidget *parent = nullptr);
~game();
bool IF_juego=false;
private slots:
void on_actionIniciar_juego_triggered();
void on_actionquitar_triggered();
void on_bt_IFJuego_clicked();
public slots:
void mostrar_puntos(int puntos);
signals:
void most_vent(int);
private:
Ui::game *ui;
DuArkanoid *mArkanoid;
};
#endif // GAME_H
this is the dugraphicsscene.cpp
, where I make the connection and send the signalemit puntaje(puntos)
#include "dugraphicsscene.h"
#include "duGlobalDefines.h"
#include "duthreadtimer.h"
#include "duballitem.h"
#include "duplatformitem.h"
#include "duutil.h"
#include "Comunes.h"
#include "game.h"
#include <QtDebug>
#include <QKeyEvent>
DuGraphicsScene::DuGraphicsScene(QObject *parent)
:QGraphicsScene(0.0, 0.0, XSIZE,YSIZE, parent)
{
createObjects();
configureObjects();
connectObjects();
}
void DuGraphicsScene::startScene()
{
mThreadTimer->start();
}
void DuGraphicsScene::stopScene()
{
mThreadTimer->terminate();
}
void DuGraphicsScene::keyPressEvent(QKeyEvent *event)
{
mPlatformItem->move(event->key());
}
void DuGraphicsScene::keyReleaseEvent(QKeyEvent *event)
{
event->accept();
}
void DuGraphicsScene::createObjects()
{
mThreadTimer= new DuThreadTimer(MILISECONDS,this);
mBallItem = new DuBallItem(XBALL+100,YBALL,WBALL,HBALL,VXBALL,VYBALL);
mPlatformItem = new DuPlatformItem(XPLATFORM, YPLATFORM,WPLATFORM,HPLATFORM,VXPLATFORM,VYPLATFORM );
obj_game = new game;
}
void DuGraphicsScene::configureObjects()
{
addItem(mBallItem);
addItem(mPlatformItem);
addRect(0.0,0.0,XSIZE,YSIZE,QPen(QColor(Qt::black)));
}
void DuGraphicsScene::connectObjects()
{
connect(mThreadTimer,&DuThreadTimer::timeOut,this,&DuGraphicsScene::uodateScene);
connect(this,&DuGraphicsScene::puntaje,obj_game, &game::mostrar_puntos);
}
void DuGraphicsScene::uodateScene()
{
for(int k=0;k<nivel;k++)
{
mBallItem->move();
}
if(mBallItem->collidesWithItem(mPlatformItem))//colision con item
{
puntos += 200;
puntos_niv +=200;
//qDebug()<<puntos;
checkCollitions();
emit puntaje(puntos);
if(puntos_niv>=1000)
{
puntos_niv=0;
nivel++;
qDebug()<<nivel;
}
}
update();
}
and this is the game.cpp where I display the points withui->lbl_puntos->setText(QString::number( puntos));
#include "game.h"
#include "ui_game.h"
#include "duakanoid.h"
#include <QGraphicsItem>
#include <QDebug>
#include<QApplication>
game::game(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::game)
{
ui->setupUi(this);
mArkanoid = new DuArkanoid(ui->graphicsView,this);
//setCentralWidget(ui->graphicsView);
ui->graphicsView->setBackgroundBrush(QPixmap("C:/Users/SNK93/Documents/Interfaz_Museo/resources/img/fondo_juego.png"));
}
game::~game()
{
delete ui;
}
void game::on_actionIniciar_juego_triggered()
{
mArkanoid->startGame();
}
void game::on_actionquitar_triggered()
{
mArkanoid->stopGame();
close();
}
void game::mostrar_puntos(int puntos)
{
qDebug()<<puntos;
ui->lbl_puntos->setText(QString::number( puntos));
}
void game::on_bt_IFJuego_clicked()
{
if(IF_juego == false)
{
mArkanoid->startGame();
ui->bt_IFJuego->setText("Detener");
IF_juego=true;
}
else
{
mArkanoid->stopGame();
ui->bt_IFJuego->setText("Iniciar");
IF_juego=false;
emit most_vent(3);
}
}
but it ends up showing me this error:
C:\Qt\5.15.1\mingw81_64\include\QtCore\qglobal.h:121: error: static assertion failed: No Q_OBJECT in the class with the signal
In file included from C:/Qt/5.15.1/mingw81_64/include/QtCore/qnamespace.h:43,
from C:/Qt/5.15.1/mingw81_64/include/QtCore/qobjectdefs.h:48,
from C:/Qt/5.15.1/mingw81_64/include/QtCore/qobject.h:46,
from C:/Qt/5.15.1/mingw81_64/include/QtCore/QObject:1,
from ..\Interfaz_Museo\dugraphicsscene.h:4,
from ..\Interfaz_Museo\dugraphicsscene.cpp:1:
C:/Qt/5.15.1/mingw81_64/include/QtCore/qobject.h: In instantiation of 'static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = void (DuGraphicsScene::*)(int); Func2 = void (game::*)(int); typename QtPrivate::FunctionPointer<Func>::Object = DuGraphicsScene; typename QtPrivate::FunctionPointer<Func2>::Object = game]':
..\Interfaz_Museo\dugraphicsscene.cpp:60:75: required from here
C:/Qt/5.15.1/mingw81_64/include/QtCore/qglobal.h:121:63: error: static assertion failed: No Q_OBJECT in the class with the signal
# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
^~~~~~~~~~~~~~~
C:/Qt/5.15.1/mingw81_64/include/QtCore/qobject.h:249:9: note: in expansion of macro 'Q_STATIC_ASSERT_X'
Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,
^~~~~~~~~~~~~~~~~
The function
connect(&Q_OBJECT,SIGNAL,&Q_OBJECT,SIGNAL o SLOT)
,The first thing you have to do is that your DuGraphicsScene function is defined together with the Q_OBJECT macro, just like the game.h.
You also have to define the signals and slots of each class ( DuGraphicsScene and Game)