Results 1 to 3 of 3

Thread: Cant move my widget properly

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cant move my widget properly

    I have a simple code (i think) to move a simple Widget placed at the central-widget of a window. I have subclass the mouseevents to do the work but it does not work properly.
    ( the widget flikks and on every movement of 1 pixel the mouse is 1 pixel from it have to be placed... )

    Mousemove:
    Qt Code:
    1. int mouse_x = event->pos().x();
    2. int mouse_y = event->pos().y();
    3. if (mouse_active==Qt::LeftButton) {
    4. mouse_prev= mouse_active;
    5. mouse_active=Qt::NoButton; // to avoid re-call
    6. event->accept();
    7. // mouse_rx, mouse_ry are private vars . First value is given by mousepress, the nexts from here
    8. this->move(this->geometry().left()+mouse_x-mouse_rx,this->geometry().top() +mouse_y-mouse_ry);
    9. mouse_rx=mouse_x;mouse_ry=mouse_y; // remember
    10. mouse_active=mouse_prev; // restore the button type}
    To copy to clipboard, switch view to plain text mode 

    Mousepress :
    Qt Code:
    1. mouse_active = event->button();
    2. mouse_rx = event->pos().x();
    3. mouse_ry = event->pos().y();
    To copy to clipboard, switch view to plain text mode 


    Any idea ? (I'm tired to see how so simple thing does not work ....)

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Cant move my widget properly

    The code is somewhat hard to follow, due to the varying indentation (nowadays when Python is familiar to many, they are prone to read too much into any indentation) and the naming of variables. mouse_x and mouse_rx do not eplain much on their purpose and get confused in the tightly typed code.

    But still, the way I see it, the problem is the approach where you move the widget based on the delta of last two mouse moves. But the widget may lag behind.
    I would suggest an approach where you in the click calculate the distance on the clik from the widget. Something like this:
    * in mousePressEvent:
    QPoint positionDiffOfMouseAndWidget = event->pos() - this->geometry().topLeft();
    * in mouseMoveEvent:
    QPoint newpoint = event->pos() - positionDiffOfMouseAndWidget;
    this->move(newpoint);

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cant move my widget properly

    Thanks, I'm going to try it.

Similar Threads

  1. Custom widget won't size properly in a table cell
    By zeroknowledge in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2011, 16:32
  2. How to move child widget with parent widget?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 15:23
  3. Widget not painted properly
    By qwakaw in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2008, 11:30
  4. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  5. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.