【发布时间】:2014-11-25 17:04:49
【问题描述】:
所以我正在构建这个小项目(我是 qt 新手)。我想在我点击的地方添加一个新的椭圆。 我只知道它与“点击”事件有关,但我不知道如何将这两件事联系起来。 有人可以帮忙吗?
【问题讨论】:
标签: qt click qgraphicsscene ellipse
所以我正在构建这个小项目(我是 qt 新手)。我想在我点击的地方添加一个新的椭圆。 我只知道它与“点击”事件有关,但我不知道如何将这两件事联系起来。 有人可以帮忙吗?
【问题讨论】:
标签: qt click qgraphicsscene ellipse
创建自定义场景并重新实现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));
它会绘制椭圆,但该椭圆的中心将位于您单击的位置。
【讨论】: