Zharios
Zharios

Reputation: 181

QGraphicsScene selectionChanged() event

I need to know when a QGraphicItem is selected from my Scene. I'm using the signal from the method selectionChange() but this does nothing. This my code:

scene.h

class Scene : public QGraphicsScene{
public:
    Scene(QGraphicsScene *scene = 0);
    ~Scene();
private slots:
    void test();
};

scene.cpp

Scene::Scene(QGraphicsScene* scene):QGraphicsScene(scene)
{
    connect(this, SIGNAL(QGraphicsScene::selectionChanged()), this, SLOT(test()));
}

void Scene::test() {
    qDebug() << "I'm here ";
}

I suppose that the problem is that my scene inherits from QGraphicScene, or that it's a bad idea define the connection in the constructor.

Upvotes: 3

Views: 1788

Answers (2)

TheDarkKnight
TheDarkKnight

Reputation: 27629

As mentioned by @Angew, the problem is in the text passed to the SIGNAL macro.

If you're using Qt 5, the preferred method would be to use the newer connection syntax, which benefits from compile time error checking

connect(this, &GraphicsScene::SelectionChanged, this, &Scene::Test);

This connection method uses addresses of functions, which has the additional benefit of being able to connect to functions that haven't been declared as a SLOT. However, it may be desirable to still define slots, as discussed here.

Upvotes: 3

SIGNAL and SLOT are macros and thus text-based processing, which makes them quite picky. It's generally a good idea to assert that all your connections succeed. In your case, the problem is the extraneous qualification. Drop it:

connect(this, SIGNAL(selectionChanged()), this, SLOT(test()));

Upvotes: 5

Related Questions