簡體   English   中英

跳過qgraphics場景中qgraphicsitem的mouseEvent

[英]skipping the mouseEvents of the qgraphicsitem in a qgraphics scene

我知道如何將事件從qgraphics場景傳遞到q圖形項,但是問題出在項目上,正在執行場景的鼠標事件。

例如在下面的代碼中,當按下該項目時,輸出為“按下了自定義場景”

 #include <QtGui>
class CustomScene : public QGraphicsScene
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if(itemAt(event->pos()))
            QGraphicsScene::mousePressEvent((event));
        else
        qDebug() << "Custom scene clicked.";
    }
};
class CustomItem : public QGraphicsRectItem
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug() << "Custom item clicked.";
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CustomItem item;
    item.setRect(20, 20, 60, 60);
    CustomScene scene;
    //scene().set
    scene.addItem(&item);
    QGraphicsView view;
    view.setScene(&scene);
    view.show();
    return a.exec();
}

請參閱QGraphicsSceneMouseEvent :: pos的文檔:

返回項目坐標中的鼠標光標位置。

這意味着,如果鼠標距離項目的頂部和左側邊界10像素,則無論項目在場景中的什么位置,您都將獲得(10,10)作為坐標。

您需要的是QGraphicsSceneMouseEvent :: scenePos

返回場景坐標中的鼠標光標位置。

將您的if -statement更改為:

 if(itemAt(event->scenePos()))
    QGraphicsScene::mousePressEvent((event));
 else
    qDebug() << "Custom scene clicked.";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM