Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should ask yourself a question: do you have some draggable elements I want to drag to specific targets (for example you have widgets that represent pieces of different shapes that should be dragged on other widgets representing holes and each "hole" accepts only pieces of the same shape) or do you just want to move one widget over the other widget?</p> <p>If you need drag &amp; drop functionality, it seems to me that you can't really change the behavior during mouse moving - it's more or less hardcoded into QDrag object.</p> <p>If you just want to move stuff around, you should be able to do it only be reimplementing mouse Move/Press/Release events (you might also need to enable mouse tracking by QWidget::setMouseTracking method).</p> <p><strong>EDIT:</strong></p> <p>This is sample code for draggable label for "just moving stuff around":</p> <pre><code>#ifndef DRAGGABLELABEL_H #define DRAGGABLELABEL_H #include &lt;QLabel&gt; #include &lt;QPoint&gt; #include &lt;QMouseEvent&gt; class DraggableLabel : public QLabel { Q_OBJECT public: explicit DraggableLabel(const QString &amp;text, QWidget *parent = 0) : QLabel(text, parent) {} protected: void mousePressEvent(QMouseEvent *event) { dragStart = event-&gt;pos(); } void mouseMoveEvent(QMouseEvent *event) { setGeometry(QRect(geometry().topLeft() + event-&gt;pos() - dragStart, rect().size())); } private: QPoint dragStart; }; #endif // DRAGGABLELABEL_H </code></pre> <p>Now you just need to add it to your main widget:</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QWidget&gt; #include "draggablelabel.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget mainWindow; DraggableLabel *label = new DraggableLabel("Test", &amp;mainWindow); mainWindow.setMinimumSize(200, 200); mainWindow.show(); return a.exec(); } </code></pre> <p>You can experiment with moveMouseEvent to achieve dragging only in one axis.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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