QGraphicsScene 中的 QPushButton 需要双击而不是单击

QPushButton in QGraphicsScene requires doubleClick instead of single click

我需要有人来解释我,当我有 qGraphicsScene 和

void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event){
 qDebug()<<event->scenePos();
}

我在 qGraphicsScene 中有 qPushButton

myGraphicsScene::myGraphicsScene(){
QPushButton* pushButton = new QPushButton();
addWidget(pushButton);
connect(pushButton, SIGNAL(clicked()), this,SLOT(doSomething()))
}

当我点击按钮时:

当我双击按钮时:

谁能给我解释一下语义?谢谢。

Qt 文档中的 mousePressEvent :

The default implementation depends on the state of the scene. If there is a mouse grabber item, then the event is sent to the mouse grabber. Otherwise, it is forwarded to the topmost visible item that accepts mouse events at the scene position from the event, and that item promptly becomes the mouse grabber item.

因此,如果您像在代码中那样重新实现它,事件将不再发送到鼠标抓取器(您的按钮),但是当您双击时,此事件不会被 mousePressEvent 捕获(但通常由 mouseDoubleClickEvent 捕获) 并且按钮只被激活一个,因为第一次鼠标按下被忽略以检测它是简单的单击还是双击。

希望对您有所帮助。


更新:要解决您的问题,只需将您的 mouseMoveEvent 更改为:

void mousePressEvent(QGraphicsSceneMouseEvent* event){
    qDebug()<<event->scenePos();
    QGraphicsScene::mousePressEvent(event);
}

但我建议您继承 QGraphicsView 并重载他的方法 mousePressEvent。

希望对您有所帮助。