如何在 QGraphicsScene 上绘制一个点(通过鼠标单击)?
我有以下代码来设置QGraphicsScene
。我希望单击场景并在我单击的位置绘制一个点。我怎么能这样做呢?这是我当前的代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene *scene;
QGraphicsView *view = new QGraphicsView(this);
view->setGeometry(QRect(20, 50, 400, 400));
scene = new QGraphicsScene(50, 50, 350, 350);
view->setScene(scene);
}
I have the following code to set up a QGraphicsScene
. I wish to click on the scene and draw a point at the location I've clicked. How could I do this? This is my current code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene *scene;
QGraphicsView *view = new QGraphicsView(this);
view->setGeometry(QRect(20, 50, 400, 400));
scene = new QGraphicsScene(50, 50, 350, 350);
view->setScene(scene);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:有一个名为 QGraphicsSceneMouseEvent 的新类,它使这变得更容易一些。
我刚刚在这里完成了一个使用它的示例:
https://stackoverflow.com/a/26903599/999943
它与下面的答案是它是 QGraphicsScene 的子类,而不是 QGraphicsView 的子类,并且它使用
mouseEvent->scenePos()
因此不需要手动映射坐标。您走在正确的道路上,但还有一些路要走。
您需要子类化
QGraphicsView
,以便能够使用QMouseEvent
通过按下鼠标或释放鼠标来执行某些操作。QGraphicsView
本身没有无维度点。您可能想要使用QGraphicsEllipse
项,或者简单地使用半径非常小的scene->addEllipseItem()
。请注意使用
mapToScene()
来使事件的pos()
正确映射到场景中鼠标单击的位置。如果您要使用表单,则需要将子类
QGraphicsView
的实例添加到 ui 的centralWidget 布局中。或者如果你的用户界面已经有一个布局,它看起来像这样:
如果你不使用
QMainWindow
和表单,你可以将它添加到QWidget
如果你设置为其创建一个布局,然后以类似的方式将您的 QGraphicsView 添加到该布局中。如果您不希望QGraphicsView
周围有边距,只需在其上调用 show 并且不要将其放在不同的布局中。就是这样。现在,
QGraphicsView
及其与鼠标的交互很危险。请务必阅读和研究 Qt 的 图形视图框架 以及相关的示例在使用
QGraphicsView
时有效并且QGraphicsScene
。它们是非常强大的 2D 图形工具,可能有一些学习曲线,但它们是值得的。UPDATE: There is a new class called
QGraphicsSceneMouseEvent
that makes this a little easier.I just finished an example using it here:
https://stackoverflow.com/a/26903599/999943
It differs with the answer below in that it subclasses
QGraphicsScene
, notQGraphicsView
, and it usesmouseEvent->scenePos()
so there isn't a need to manually map coordinates.You are on the right track, but you still have a little more to go.
You need to subclass
QGraphicsView
to be able to do something with mouse presses or with mouse releases usingQMouseEvent
.QGraphicsView
doesn't natively have dimension-less points. You will probably want to useQGraphicsEllipse
item or simply,scene->addEllipseItem()
with a very small radius.Note the usage of
mapToScene()
to make thepos()
of the event map correctly to where the mouse is clicked on the scene.You need to add an instance of your subclassed
QGraphicsView
to the centralWidget's layout of your ui if you are going to use a form.or if your ui has a layout already it will look like this:
If you don't use a
QMainWindow
and a form, you can add it to aQWidget
if you set a layout for it and then add yourQGraphicsView
to that layout in a similar manner. If you don't want a margin around yourQGraphicsView
, just call show on it and don't put it inside a different layout.And that's it. Now you are dangerous with
QGraphicsView
's and their interaction with the mouse.Be sure to read and study about Qt's Graphics View Framework and the related examples to be effective when using
QGraphicsView
andQGraphicsScene
. They are very powerful tools for 2D graphics and can have a bit of a learning curve but they are worth it.