Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag & QMouseEvent

Drag & QMouseEvent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michelson
    wrote on last edited by
    #1

    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:

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

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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • M Offline
        M Offline
        michelson
        wrote on last edited by
        #3

        Jeez, thanks! :D

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved