QMouse event

  • Everyone knows that we are putting the program on a robotic table clamping the top strip. So I want to know how to put the window in only when I clamped the top bar and also how to make it where I clamped and left the mouse?

    QPoint newF::previousPosition() const
        return m_previousPosition;
    void newF::setPreviousPosition(QPoint previousPosition)
        if (m_previousPosition == previousPosition)
        m_previousPosition = previousPosition;
        emit previousPositionChanged(previousPosition);
    void newF::mouseMoveEvent(QMouseEvent *event)
                auto dx = event->x() - m_previousPosition.x();
                auto dy = event->y() - m_previousPosition.y();
                setGeometry(x() + dx, y() + dy, width(), height());
        return QWidget::mouseMoveEvent(event);

  • @TETTRA Hi, friend, do you want to use the mouse to move the window dialog of not have title bar ?

    sorry . I didn't understand what you said.

  • @joeQ Yes

  • @TETTRA Why didn't you use the mousePress,mouseRelease,mouseMove event?

    Reimplement the following virtual functions in subclass of QMainWindow or QDialog, or QWidget

    virtual void mouseMoveEvent(QMouseEvent *event)
    virtual void mousePressEvent(QMouseEvent *event)
    virtual void mouseReleaseEvent(QMouseEvent *event)

    First way(Suggest)

    When user use the left button to press window, it will begin to move, in the moving, you can know the position of mouse from mouseMoveEvent, and to set the new position of window use move() function. when user release left button, moving window is end.

    Second way

    Only use the mouseMoveEvent, but, you must to check the left button is pressed when mouse moving in function. But, you must set the setMouseTracking(true).


    If you want the moving action look naturally. You should record the press point in Application Window position.

    Try them again.

    Demo Snippet Code

    Widget::Widget(QWidget *parent) :
        ui(new Ui::Widget)
        delete ui;
    void Widget::mousePressEvent(QMouseEvent *event)
        _winPt   = this->pos();
        _mousePt = event->globalPos();
        _dtPt    = _mousePt - _winPt;
    void Widget::mouseMoveEvent(QMouseEvent *event)
        this->move(event->globalPos() - _dtPt);

  • @joeQ Excuse me. Can you write me more in detail. Just i'm new

  • @TETTRA I don't know what you do not understand. The snippet code is very clear. Can you drew the window and diaog coordinate system ? Please draw it for yourself, you will clear well. and, you also can debug it to see the value.

    Click to download the complete demo

    The link is valid for one week

  • @joeQ Thx. If I use this-> setWindowFlags (Qt :: SplashScreen) ;? Then how is the vintue tittle bar? But that did not stretch the windows?

Log in to reply