C++ 如何在 QGraphicsScene 上绘制一个点(鼠标单击)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7830054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a point (on mouseclick) on a QGraphicsScene?
提问by phyatt
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:
我有以下代码来设置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);
}
回答by phyatt
UPDATE: There is a new class called QGraphicsSceneMouseEvent
that makes this a little easier.
I just finished an example using it here:
更新:有一个名为的新类QGraphicsSceneMouseEvent
使这更容易一些。我刚刚在这里完成了一个使用它的例子:
https://stackoverflow.com/a/26903599/999943
https://stackoverflow.com/a/26903599/999943
It differs with the answer below in that it subclasses QGraphicsScene
, not QGraphicsView
, and it uses mouseEvent->scenePos()
so there isn't a need to manually map coordinates.
它与以下答案的不同之处在于它子类化QGraphicsScene
,而不是QGraphicsView
,并且它使用,mouseEvent->scenePos()
因此不需要手动映射坐标。
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 using QMouseEvent
.
您需要子类化QGraphicsView
才能使用QMouseEvent
.
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QMouseEvent>
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyQGraphicsView(QWidget *parent = 0);
signals:
public slots:
void mousePressEvent(QMouseEvent * e);
// void mouseReleaseEvent(QMouseEvent * e);
// void mouseDoubleClickEvent(QMouseEvent * e);
// void mouseMoveEvent(QMouseEvent * e);
private:
QGraphicsScene * scene;
};
QGraphicsView
doesn't natively have dimension-less points. You will probably want to use QGraphicsEllipse
item or simply, scene->addEllipseItem()
with a very small radius.
QGraphicsView
本身没有无量纲点。您可能想要使用QGraphicsEllipse
item 或简单地scene->addEllipseItem()
使用非常小的半径。
#include "myqgraphicsview.h"
#include <QPointF>
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
scene = new QGraphicsScene();
this->setSceneRect(50, 50, 350, 350);
this->setScene(scene);
}
void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
{
double rad = 1;
QPointF pt = mapToScene(e->pos());
scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,
QPen(), QBrush(Qt::SolidPattern));
}
Note the usage of mapToScene()
to make the pos()
of the event map correctly to where the mouse is clicked on the scene.
请注意mapToScene()
使pos()
事件映射到鼠标在场景上单击的位置的用法。
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.
QGraphicsView
如果要使用表单,则需要将子类的实例添加到 ui 的 centralWidget 布局中。
QGridLayout * gridLayout = new QGridLayout(ui->centralWidget);
gridLayout->addWidget( new MyQGraphicsView() );
or if your ui has a layout already it will look like this:
或者如果您的 ui 已经有布局,它将如下所示:
ui->centralWidget->layout()->addWidget( new MyGraphicsView() );
If you don't use a QMainWindow
and a form, you can add it to a QWidget
if you set a layout for it and then add your QGraphicsView
to that layout in a similar manner. If you don't want a margin around your QGraphicsView
, just call show on it and don't put it inside a different layout.
如果您不使用 aQMainWindow
和一个表单,则可以将其添加到 aQWidget
如果您为其设置布局,然后QGraphicsView
以类似的方式将您的添加到该布局。如果您不想在您的 周围QGraphicsView
留出边距,只需在其上调用 show 并且不要将其放在不同的布局中。
#include <QtGui/QApplication>
#include "myqgraphicsview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyQGraphicsView view;
view.show();
return a.exec();
}
And that's it. Now you are dangerous with QGraphicsView
's and their interaction with the mouse.
就是这样。现在您对QGraphicsView
's 及其与鼠标的交互很危险。
Be sure to read and study about Qt's Graphics View Frameworkand the related examplesto be effective when using QGraphicsView
and QGraphicsScene
. They are very powerful tools for 2D graphics and can have a bit of a learning curve but they are worth it.
请务必阅读和学习 Qt 的图形视图框架和相关示例,以便在使用QGraphicsView
和时有效QGraphicsScene
。它们是非常强大的 2D 图形工具,可能需要一些学习曲线,但它们值得。