PDA

View Full Version : Linking issues with emiting a custom signal from a QGraphicsScene subclass?



mrtof
31st August 2011, 20:20
Hi, I am trying to emit a signal from a custom subclass of QGraphicsScene. I get the following errors:

"BoardScene::selected(int)", referenced from:
BoardScene::select(int) in boardscene.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [Msg.app/Contents/MacOS/Msg] Error 1
make: Leaving directory `/Users/tom/Documents/Qt/Msg-build-desktop'
The process "/usr/bin/make" exited with code 2.
Error while building project Msg (target: Desktop)
When executing build step 'Make'

Why am I getting this error? What I want is that when a GraphicsPin is clicked, that it notifies the BoardScene (through bs->select(num)) of a click and then BoardScene emits the selected(num) signal.

boardscene.h:



#ifndef BOARDSCENE_H
#define BOARDSCENE_H
#include <QGraphicsScene>
#include <QGraphicsItem>


class BoardScene;

class GraphicsPin : public QGraphicsRectItem
{

public:
GraphicsPin();
~GraphicsPin();
void set(BoardScene* bs, int num,qreal x, qreal y,qreal size);
private:
int num;
QPen pen;
QBrush brush;
BoardScene* bs;
protected :
void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
void mousePressEvent ( QGraphicsSceneMouseEvent * event );



};

class BoardScene : public QGraphicsScene
{
// Should I put Q_OBJECT here?
//Q_OBJECT


public:
BoardScene();
~BoardScene();
protected:
QPixmap pixmap;
QGraphicsPixmapItem board;
void load(QString file);
void addPin(int num, qreal x, qreal y);
int pinSize;
GraphicsPin graphicsPin[128];
int pins;
int selectedPin;

private:
QPen penNone;
QBrush brushNone;
signals:
void selected(int num);


public slots:
void select(int num);
};



#endif // BOARDSCENE_H



boardscene.cpp:


#include "boardscene.h"
#include "qdebug.h"

////// GRAPHICSPIN //////

GraphicsPin::GraphicsPin() {
setAcceptHoverEvents(true);
pen.setStyle(Qt::SolidLine);
pen.setWidth(1);
pen.setBrush(QColor(255,255,255));
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(255,255,255));

setPen(pen);
setBrush(brush);
}

GraphicsPin::~GraphicsPin() {

}

void GraphicsPin::set(BoardScene *bs,int num,qreal x, qreal y,qreal size) {
setRect(x,y,size,size);
this->num = num;
this->bs = bs;
}

void GraphicsPin::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) {
qDebug() << "GraphicsPin::hoverEnterEvent";
}

void GraphicsPin::mousePressEvent(QGraphicsSceneMouseEv ent * event ) {
qDebug() << "GraphicsPin::mousePressEvent";
if ( bs ) bs->select(num);

}

////// BOARDSCENE //////

BoardScene::BoardScene()
{

addItem(&board);
selectedPin = -1;

}


BoardScene::~BoardScene() {

}


void BoardScene::select(int num) {
if ( num != selectedPin ) {
qDebug() << "BoardScene::select" << num;
selectedPin = num;
emit selected(num);
}
}

void BoardScene::load(QString file) {
pixmap.load(file);
board.setPixmap(pixmap);

}

void BoardScene::addPin(int num, qreal x, qreal y) {
graphicsPin[num].set(this,num,x,y,pinSize);
addItem(&graphicsPin[num]);
}


Thank you for your help.

wysota
31st August 2011, 23:39
You are missing the Q_OBJECT macro in your scene class.

mrtof
1st September 2011, 07:57
You are missing the Q_OBJECT macro in your scene class.

With the macro I get even more errors:

Undefined symbols:
"vtable for BoardScene", referenced from:
BoardScene::~BoardScene()in boardscene.o
BoardScene::~BoardScene()in boardscene.o
BoardScene::~BoardScene()in boardscene.o
BoardScene::BoardScene()in boardscene.o
BoardScene::BoardScene()in boardscene.o
"BoardScene::selected(int)", referenced from:
BoardScene::select(int) in boardscene.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Added after 15 minutes:

I removed a class that I was conflicting with the build. Everything works now, thanks!

ChrisW67
1st September 2011, 08:04
Re-run qmake and then rebuild so that moc is run.