【问题标题】:QGraphicsScene selectionChanged() eventQGraphicsScene selectionChanged() 事件
【发布时间】: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::

标签: c++ qt


【解决方案1】:

SIGNALSLOT 是宏,因此是基于文本的处理,这使得它们非常挑剔。对于assert,您的所有连接都成功通常是一个好主意。在您的情况下,问题是无关的资格。放下它:

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

【讨论】:

  • 他用新的语法在不正确的时候得到一个编译时错误不是更好吗?
  • @scopchanov 如果他们可能正在使用 Qt5。我仍然停留在 Qt4 上,还没有研究 Qt5 的细节,所以我留空等待更好的答案......
  • 好点!我总是忘记,人们使用不同的版本。 :)
【解决方案2】:

正如@Angew 所说,问题出在传递给SIGNAL 宏的文本中。

如果您使用的是 Qt 5,首选方法是使用较新的连接语法,这会受益于编译时错误检查

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

这种连接方法使用函数的地址,它的另一个好处是能够连接到尚未声明为SLOT 的函数。但是,可能仍需要定义槽,as discussed here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多