【问题标题】:Qt QGraphicsScene clicked eventQt QGraphicsScene 点击事件
【发布时间】:2014-11-25 17:04:49
【问题描述】:

所以我正在构建这个小项目(我是 qt 新手)。我想在我点击的地方添加一个新的椭圆。 我只知道它与“点击”事件有关,但我不知道如何将这两件事联系起来。 有人可以帮忙吗?

【问题讨论】:

    标签: qt click qgraphicsscene ellipse


    【解决方案1】:

    创建自定义场景并重新实现mousePressEvent。例如:

    标题:

    #ifndef GRAPHICSSCENE_H
    #define GRAPHICSSCENE_H
    
    #include <QGraphicsScene>
    #include <QPoint>
    #include <QMouseEvent>
    class GraphicsScene : public QGraphicsScene
    {
        Q_OBJECT
    public:
        explicit GraphicsScene(QObject *parent = 0);
        ~GraphicsScene();
    
    signals:
    
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
    
    };
    
    #endif // GRAPHICSSCENE_H
    

    Cpp:

    void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    
          if (mouseEvent->button() == Qt::LeftButton)
          {
              QPointF p = mouseEvent->scenePos();
              int l = 20;
              addEllipse(p.x()-l/2,p.y()-l/2,l,l,QPen(Qt::green));//you can use another approach to create ellipse
          }
          QGraphicsScene::mousePressEvent(mouseEvent);
    }
    

    这条线是什么? addEllipse(p.x()-l/2,p.y()-l/2,l,l,QPen(Qt::green));

    它会绘制椭圆,但该椭圆的中心将位于您单击的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多