Results 1 to 3 of 3

Thread: EventFilter - MoveMouse event changes position every pixel

  1. #1
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default EventFilter - MoveMouse event changes position every pixel

    I'm making some draggable widgets. Labels work fine any way I do it, but there is obviously a problem with PlainTextEdits. I've installed an EventFilter on PlainTextEdit's viewport and want to drag the whole widget with mouse movement.

    Qt Code:
    1. bool FilerPlainText2Click::eventFilter(QObject* object, QEvent* event)
    2. {
    3. if(event->type() == QEvent::MouseButtonDblClick)
    4. //some code here (set pSelectedWidget)
    5. else if(event->type() == QEvent::MouseButtonPress && ((QMouseEvent*)event)->button() == Qt::LeftButton)
    6. //some code here (set bDragging true)
    7. else if(event->type() == QEvent::MouseMove)
    8. {
    9. QWidget* pSelected = ((QWidget*)object)->parentWidget(); // pointer to the whole widget
    10. MainWindow* pWin = (MainWindow*)pSelected->window(); // pointer to the main window, which this widget is placed in
    11.  
    12. if(pWin->bDragging)
    13. {
    14. QMouseEvent* mEvent = (QMouseEvent*)event;
    15. int x = mEvent->x() - pWin->iDraggingDistX;
    16. int y = mEvent->y() - pWin->iDraggingDistY;
    17. QRect rect(x, y, pWin->pSelectedWidget->width(), pWin->pSelectedWidget->height());
    18. pWin->pSelectedWidget->setGeometry(rect);
    19.  
    20. qDebug() << "mEvent pos: " << mEvent->x() << " | " << mEvent->y();
    21. }
    22. return true;
    23. }
    24. else
    25. return QObject::eventFilter(object,event);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Explanation:
    Qt Code:
    1. iDraggingDistX / iDraggingDistY - distance between grabbed point and left top corner of the widget
    2. pSelectedWidget - the widget we're dragging
    To copy to clipboard, switch view to plain text mode 

    qDebug's effect:
    Qt Code:
    1. mEvent pos: 217 | 462
    2. mEvent pos: 127 | 62
    3. mEvent pos: 216 | 462
    4. mEvent pos: 126 | 62
    5. mEvent pos: 216 | 463
    6. mEvent pos: 125 | 62
    7. mEvent pos: 215 | 463
    8. mEvent pos: 124 | 62
    9. mEvent pos: 215 | 464
    10. mEvent pos: 123 | 62
    To copy to clipboard, switch view to plain text mode 

    For some reason every time i move my widget by 1 pixel, mEvent's coordinates change (they seem to be realtive to widget once and relative to main window second time). My widget is obviously blinking in both positions. Why is this?
    I realise it may sound very confusing and i might not be clear enough. Adding a video for clarity.

    Last edited by Grzyboo; 1st August 2015 at 22:27.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: EventFilter - MoveMouse event changes position every pixel

    You could try to map the coordinates to global coordinates and do the distance calculation in that domain.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Grzyboo (2nd August 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: EventFilter - MoveMouse event changes position every pixel

    Thanks a lot! It helped.

Similar Threads

  1. Replies: 0
    Last Post: 1st February 2014, 16:35
  2. eventFilter, Focus event and shortcut dispatching
    By tuli in forum Qt Programming
    Replies: 0
    Last Post: 25th May 2013, 20:13
  3. Enter event mouse position
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 7th May 2008, 18:53
  4. Pixel Position and Printing text
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2006, 12:35
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.