Search code examples
qtqt5signals-slots

How to emit signal from QGraphicsScene?


I have a QGraphicsScene subclass, if no QGraphicsItems are selected on mouseReleaseEvent I would like to emit a signal signaling that.

For some reason, if I create a signal in my scene subclass and emit it in mouseReleaseEvent I get a link error, I've encountered that before so I just figured you can't send new signals from QGraphicsScene.

Another solution was to make the signal in QGraphicsItem, but then if there are no items on the scene I can't send the signal.

There are various workarounds and hacks I have tried but I would like to know if it's possible to define a new signal and send it on mouseReleaseEvent all in my QGraphicsScene subclass.

I can paste the code if needed, I make the signal like this

class Scene : public QGraphicsScene
{
public:
    Scene(QObject* parent = 0);
    ...
signals:
    void nothingSelected();
protected:
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    ...
};

and emit the signal like this

void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene::mouseReleaseEvent(event);
    if(selectedItems().length() == 0)
        emit nothingSelected();
}

Errors I get:

1:

scene.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl Scene::nothingSelected(void)" (?nothingSelected@Scene@@QEAAXXZ) referenced in function "protected: virtual void __cdecl Scene::mouseReleaseEvent(class QGraphicsSceneMouseEvent *)" (?mouseReleaseEvent@Scene@@MEAAXPEAVQGraphicsSceneMouseEvent@@@Z)

2:

C:\path_to_project\build-app-Desktop_Qt_5_15_1_MSVC2019_64bit-Debug\debug\app.exe:-1: error: LNK1120: 1 unresolved externals

Solution

  • You forgot to add Q_OBJECT macro to class Scene