【发布时间】:2017-11-20 11:03:06
【问题描述】:
我需要知道何时从我的 Scene 中选择了 QGraphicItem。我正在使用来自selectionChange() 方法的信号,但这没有任何作用。这是我的代码:
场景.h
class Scene : public QGraphicsScene{
public:
Scene(QGraphicsScene *scene = 0);
~Scene();
private slots:
void test();
};
场景.cpp
Scene::Scene(QGraphicsScene* scene):QGraphicsScene(scene)
{
connect(this, SIGNAL(QGraphicsScene::selectionChanged()), this, SLOT(test()));
}
void Scene::test() {
qDebug() << "I'm here ";
}
我想问题是我的场景继承自QGraphicScene,或者在构造函数中定义连接是个坏主意。
【问题讨论】:
-
添加到场景中的项目应该设置了
QGraphicsItem::ItemIsSelectable标志,所以看看setFlags。 -
是的@scopchanov,我在我的所有项目中都设置了这个 item->setFlag(QGraphicsItem::ItemIsSelectable);
-
我认为你应该像这样建立你的连接:
connect(this, SIGNAL(selectionChanged()), this, SLOT(test())); -
@DavidLeonOrtega,对不起,我不明白你的意思:) 你需要什么?请改写句子。
-
@vahancho 首先他用股票 QGraphicsScene 对其进行了测试,然后决定将 QGraphicsScene 子类化,并在其中粘贴
connect行之前没有从代码中删除已经不必要的限定符QGraphicsScene::。