我需要知道何时从我的QGraphicItem中选择一个Scene。我使用的是来自selectionChange()方法的信号,但这没有任何作用。这是我的代码:
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 ";
}我认为问题在于我的场景继承自QGraphicScene,或者在构造函数中定义连接是个坏主意。
发布于 2017-11-20 16:15:50
正如@Angew所提到的,问题在传递给SIGNAL宏的文本中。
如果您使用的是Qt 5,最好的方法是使用更新的连接语法,这将从编译时错误检查中获益
connect(this, &GraphicsScene::SelectionChanged, this, &Scene::Test);此连接方法使用函数的地址,这有一个额外的好处,即能够连接到尚未声明为SLOT的函数。但是,仍然需要定义时隙,as discussed here。
发布于 2017-11-20 11:14:57
SIGNAL和SLOT是宏,因此是基于文本的处理,这使得它们非常挑剔.通常对assert来说,所有的连接都成功是个好主意。在你的情况下,问题是额外的资格。放下它:
connect(this, SIGNAL(selectionChanged()), this, SLOT(test()));https://stackoverflow.com/questions/47390499
复制相似问题