問題描述
我有以下代碼來設置 QGraphicsScene
.我希望單擊場景并在我單擊的位置繪制一個點.我怎么能這樣做?這是我當前的代碼:
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);
}
推薦答案
UPDATE:有一個名為 QGraphicsSceneMouseEvent
的新類,它使這更容易一些.我剛剛在這里完成了一個使用它的例子:
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
它與下面的答案的不同之處在于它子類化 QGraphicsScene
,而不是 QGraphicsView
,并且它使用 mouseEvent->scenePos()
所以無需手動映射坐標.
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.
您走在正確的軌道上,但您還有一些路要走.
You are on the right track, but you still have a little more to go.
您需要子類化 QGraphicsView
才能使用 QMouseEvent
通過鼠標按下或釋放鼠標來執行某些操作.
You need to subclass QGraphicsView
to be able to do something with mouse presses or with mouse releases using 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
本身沒有無量綱點.您可能希望使用 QGraphicsEllipse
項目或簡單地使用半徑非常小的 scene->addEllipseItem()
.
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.
#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));
}
注意 mapToScene()
的用法,使事件的 pos()
正確映射到鼠標在場景上點擊的位置.
Note the usage of mapToScene()
to make the pos()
of the event map correctly to where the mouse is clicked on the scene.
如果要使用表單,則需要將子類 QGraphicsView
的實例添加到 ui 的 centralWidget 布局中.
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.
QGridLayout * gridLayout = new QGridLayout(ui->centralWidget);
gridLayout->addWidget( new MyQGraphicsView() );
或者如果您的 ui 已經有布局,它將如下所示:
or if your ui has a layout already it will look like this:
ui->centralWidget->layout()->addWidget( new MyGraphicsView() );
如果你不使用 QMainWindow
和一個表單,你可以將它添加到一個 QWidget
如果你為它設置一個布局然后添加你的 QGraphicsView
以類似的方式到該布局.如果您不想在 QGraphicsView
周圍留出邊距,只需調用 show 即可,不要將其放在不同的布局中.
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.
#include <QtGui/QApplication>
#include "myqgraphicsview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyQGraphicsView view;
view.show();
return a.exec();
}
就是這樣.現在您對 QGraphicsView
及其與鼠標的交互很危險.
And that's it. Now you are dangerous with QGraphicsView
's and their interaction with the mouse.
請務必閱讀和研究 Qt 的圖形視圖框架和相關examples在使用QGraphicsView
和 QGraphicsScene
.它們是非常強大的 2D 圖形工具,可能需要一些學習曲線,但它們值得.
Be sure to read and study about Qt's Graphics View Framework and the related examples to 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.
這篇關于如何在 QGraphicsScene 上繪制一個點(鼠標單擊)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!