Unsolved Draggable widget stops moving when children shown / hidden
-
I made a top-level draggable widget by reimplementing mouseMoveEvent()
Depending on its location on the screen, I want the widget to be able to display different kinds of things, so I inherited QStackedWidget in order to dynamically switch content as the widget is dragged.
The issue is that as soon as I call QStackedWidget::setCurrentWidget(), mouse move events cease to reach my widget (they seemed to be eaten by QWidgetWindow).
The issue is not specific to QStackedWidget. I get the same problem if I just inherit QWidget and manually show / hide children.
Here is a minimal example to illustrate the issue :
#include <QApplication> #include <QStackedWidget> #include <QLabel> #include <QMouseEvent> #include <QFont> class DraggableWidget : public QStackedWidget { public: DraggableWidget() : QStackedWidget { nullptr } { setWindowFlag(Qt::FramelessWindowHint); setMinimumSize(300, 300); // here we have to imagine that label1 and label2 have different type auto label1 = new QLabel("Y < 500"); auto label2 = new QLabel("Y > 500"); QFont font("Arial", 48, QFont::Bold); label1->setFont(font); label2->setFont(font); addWidget(label1); addWidget(label2); } virtual void mouseMoveEvent(QMouseEvent *event) override { auto r = geometry(); r.moveCenter(mapToGlobal(event->pos())); setGeometry(r); setCurrentIndex((r.center().y() < 500) ? 0 : 1); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); (new DraggableWidget)->show(); return a.exec(); }
When the widget reaches the y = 500px line, the drag stops, and the widget has to be clicked on again to resume the move, which is very annoying.
How could i get the widget to cross the line smoothly in a single mouse move ?