Drag & QMouseEvent

  • Hello,
    I have a simple widget which I would like to move with the mouse (one axis only - x in this case). I have implemented three MouseEvents to be able to move widget around:

    void MyWidget::mousePressEvent(QMouseEvent *event){
        qDebug() << "click";
        m_is_dragging = true;
    void MyWidget::mouseMoveEvent(QMouseEvent *event){
        if(m_is_dragging)    {
    /* !!!!!!!!!!!!note this one!!!!!!!!!!!!*/
            qDebug() << event->pos();         
            this->move(event->pos().x(), 400);
    void MyWidget::mouseReleaseEvent(QMouseEvent *event){
        m_is_dragging = false;
        qDebug() << "relese";

    The output of the program is:

    QPoint(39,27) // 1
    QPoint(21,27) // 2
    QPoint(40,27) // 1
    QPoint(22,27) // 2
    QPoint(41,27) // 1
    QPoint(23,27) // 2

    Note that x positions are jumping like every other call was concering totally different pos. Why?

  • Qt Champions 2018

    form http://doc.qt.io/qt-5/qmouseevent.html#pos

    If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.

  • Jeez, thanks! :D

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.