Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not telling us at all what is it that you're trying to do. The Qt's graphics scene / view system is powerful enough to support dragging of items without you having to write any code to handle low-level mouse events. If that's what you're after, that is.</p> <p>Your mouse move event is not called because your mouse press doesn't happen in your widget, but in the child <code>QGraphicsScene</code>. Mouse presses that happen in children will not trigger mouse tracking within your widget.</p> <p>Had you added a layout to manage the child widgets in your Painter class, you'd notice that your mouse move events are handled if you click in the margin outside of the <code>QGraphicsView</code>. The code below shows how to do set a layout on a widget containing a scene view.</p> <p>The code below is a complete and hopefully correct example of creating randomly sized, moveable and focuseable circles. It demonstrates as well:</p> <ul> <li>How to properly map coordinates from the parent widget to scene itself.</li> <li>That clearing the scene does not require manually keeping track of items.</li> <li>How to derive from a predefined item to add simple functionality.</li> <li>How to operate on an item that has focus.</li> <li>That the scene is notified when its children are removed, thus simply deleting a child item is perfectly safe thing to do.</li> <li>That <code>delete</code> safely ignores null pointers, such as potentially returned by <code>QGraphicsScene::focusItem()</code>.</li> <li>How to use object names to automatically attach slots named <code>on_name_signal</code>.</li> <li>How to have Q_OBJECT classes within .cpp files by including the .moc file at the end.</li> <li>How to keep members by value, relegating memory management to the compiler.</li> </ul> <p>The example is self contained and consists of just one .pro and .cpp file each. It works under both Qt 4 and Qt 5, and leverages C++11.</p> <p><img src="https://i.stack.imgur.com/TdkNV.png" alt="screnshot from running the example code"></p> <pre><code>// https://github.com/KubaO/stackoverflown/tree/master/questions/scene-movable-circles-11188261 // main.cpp #include &lt;QtGui&gt; #if QT_VERSION &gt;= QT_VERSION_CHECK(5,0,0) #include &lt;QtWidgets&gt; #endif #include &lt;cmath&gt; class Circle : public QGraphicsEllipseItem { QBrush m_inBrush{Qt::red}, m_outBrush{Qt::lightGray}; public: Circle(const QPointF &amp; c) { const qreal r = 10.0 + (50.0*qrand())/RAND_MAX; setRect({c.x()-r, c.y()-r, 2.0*r, 2.0*r}); setFlag(QGraphicsItem::ItemIsMovable); setFlag(QGraphicsItem::ItemIsFocusable); setPen({Qt::red}); setBrush(m_outBrush); } void focusInEvent(QFocusEvent *) override { setBrush(m_inBrush); } void focusOutEvent(QFocusEvent *) override { setBrush(m_outBrush); } }; class Painter : public QWidget { Q_OBJECT QGridLayout m_layout{this}; QGraphicsView m_view; QPushButton m_clear{"Clear"}; QPushButton m_remove{"Remove"}; QGraphicsScene m_scene; public: explicit Painter(QWidget *parent = nullptr); protected: Q_SLOT void on_remove_clicked(); void mousePressEvent(QMouseEvent *event) override; }; Painter::Painter(QWidget *parent) : QWidget(parent) { m_layout.addWidget(&amp;m_view, 0, 0, 1, 2); m_layout.addWidget(&amp;m_clear, 1, 0); m_layout.addWidget(&amp;m_remove, 1, 1); m_remove.setObjectName("remove"); QMetaObject::connectSlotsByName(this); connect(&amp;m_clear, SIGNAL(clicked()), &amp;m_scene, SLOT(clear())); m_view.setRenderHint(QPainter::Antialiasing); m_view.setScene(&amp;m_scene); m_view.setSceneRect(0,0,500,500); } void Painter::mousePressEvent(QMouseEvent *event) { auto center = m_view.mapToScene(m_view.mapFromParent(event-&gt;pos())); m_scene.addItem(new Circle(center)); } void Painter::on_remove_clicked() { delete m_scene.focusItem(); } int main(int argc, char ** argv) { QApplication app{argc, argv}; Painter p; p.show(); return app.exec(); } #include "main.moc" </code></pre> <pre><code># scene-movable-circles-11188261.pro greaterThan(QT_MAJOR_VERSION, 4) { QT = widgets CONFIG += c++11 } else { QT = gui unix:QMAKE_CXXFLAGS += -std=c++11 macx { QMAKE_CXXFLAGS += -stdlib=libc++ QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7 QMAKE_CXXFLAGS_WARN_ON += -Wno-inconsistent-missing-override } } DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x050800 TARGET = scene-movable-circles-11188261 TEMPLATE = app SOURCES += main.cpp </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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