我已经在 QGraphicsScene 子类中为 mousePressEvent() 实现了信号,但我不知道如何在 UI 中使用该类。我可以将 QGraphicsView 小部件添加到我的 UI,但是我如何访问场景?
GraphicsScene *scene = new QGraphicsScene(this);
// Add pixmap, etc
ui->graphicsView->setScene(scene);
// Here's where I'm stuck
connect(ui->whereIsTheScene?, SIGNAL(clicked(QPoint)), this, SLOT(someSlot(QPoint));
编辑:这是编译,但鼠标按下事件被忽略。我认为这是一个单独的问题,所以我发布了 another question
最佳答案
在您的示例中:使用您已有的 scene
指针:
connect(scene, SIGNAL(clicked(QPoint)), this, SLOT(someSlot(QPoint));
或者,如果您不再有指针,请使用 this function :
connect(ui->graphicsView->scene(), SIGNAL(clicked(QPoint)), this, SLOT(someSlot(QPoint));
(未经测试,但我看不出它为什么不起作用)
关于c++ - 将 QGraphicsScene 子类与 ui 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3548290/