Important: Please read the Qt Code of Conduct -

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
        DraggableWidget() : QStackedWidget { nullptr }
            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);
        virtual void mouseMoveEvent(QMouseEvent *event) override
            auto r = geometry();
            setCurrentIndex(( < 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 ?

Log in to reply