Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>UPDATE: There is a new class called <code>QGraphicsSceneMouseEvent</code> that makes this a little easier. I just finished an example using it here:</p> <p><a href="https://stackoverflow.com/a/26903599/999943">https://stackoverflow.com/a/26903599/999943</a></p> <p>It differs with the answer below in that it subclasses <code>QGraphicsScene</code>, not <code>QGraphicsView</code>, and it uses <code>mouseEvent-&gt;scenePos()</code> so there isn't a need to manually map coordinates.</p> <hr> <p>You are on the right track, but you still have a little more to go.</p> <p>You need to subclass <code>QGraphicsView</code> to be able to do something with mouse presses or with mouse releases using <code>QMouseEvent</code>.</p> <pre><code> #include &lt;QGraphicsView&gt; #include &lt;QGraphicsScene&gt; #include &lt;QGraphicsEllipseItem&gt; #include &lt;QMouseEvent&gt; 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; }; </code></pre> <p><code>QGraphicsView</code> doesn't natively have dimension-less points. You will probably want to use <code>QGraphicsEllipse</code> item or simply, <code>scene-&gt;addEllipseItem()</code> with a very small radius.</p> <pre><code> #include "myqgraphicsview.h" #include &lt;QPointF&gt; MyQGraphicsView::MyQGraphicsView(QWidget *parent) : QGraphicsView(parent) { scene = new QGraphicsScene(); this-&gt;setSceneRect(50, 50, 350, 350); this-&gt;setScene(scene); } void MyQGraphicsView::mousePressEvent(QMouseEvent * e) { double rad = 1; QPointF pt = mapToScene(e-&gt;pos()); scene-&gt;addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, QPen(), QBrush(Qt::SolidPattern)); } </code></pre> <p>Note the usage of <code>mapToScene()</code> to make the <code>pos()</code> of the event map correctly to where the mouse is clicked on the scene.</p> <p>You need to add an instance of your subclassed <code>QGraphicsView</code> to the centralWidget's layout of your ui if you are going to use a form. </p> <pre><code> QGridLayout * gridLayout = new QGridLayout(ui-&gt;centralWidget); gridLayout-&gt;addWidget( new MyQGraphicsView() ); </code></pre> <p>or if your ui has a layout already it will look like this:</p> <pre><code> ui-&gt;centralWidget-&gt;layout()-&gt;addWidget( new MyGraphicsView() ); </code></pre> <p>If you don't use a <code>QMainWindow</code> and a form, you can add it to a <code>QWidget</code> if you set a layout for it and then add your <code>QGraphicsView</code> to that layout in a similar manner. If you don't want a margin around your <code>QGraphicsView</code>, just call show on it and don't put it inside a different layout.</p> <pre><code> #include &lt;QtGui/QApplication&gt; #include "myqgraphicsview.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MyQGraphicsView view; view.show(); return a.exec(); } </code></pre> <p>And that's it. Now you are dangerous with <code>QGraphicsView</code>'s and their interaction with the mouse.</p> <p>Be sure to read and study about Qt's <a href="https://doc.qt.io/qt-5/graphicsview.html" rel="nofollow noreferrer">Graphics View Framework</a> and the related <a href="https://doc.qt.io/qt-5/examples-graphicsview.html" rel="nofollow noreferrer">examples</a> to be effective when using <code>QGraphicsView</code> and <code>QGraphicsScene</code>. They are very powerful tools for 2D graphics and can have a bit of a learning curve but they are worth it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload