Results 1 to 14 of 14

Thread: mousePressEvent & mouseReleaseEvent question

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default mousePressEvent & mouseReleaseEvent question

    Hi to all!

    I have a tiny problem regarding mousePressEvent & mouseReleaseEvent: Once I click a mouse button, mousePressEvent is called and once I release it mouseReleasedEvent is called. That works perfectly, but let's say I hold mouse button for half a second. After half a second, I want that mousePressEvent is ignored (I still HOLD mouse button!) and mouseReleaseEvent is called. After that normal event loop is continued. How do I achieve that, please help me!

    Sincerely,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mousePressEvent & mouseReleaseEvent question

    There is no way to do this. You can forge mouseReleaseEvent() but when you release the mouse, another mouse event will be posted. mouseMoveEvent() will also continue being called as you drag the mouse. Maybe you should say what you need this functionality for?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Well, here is the task that needs to be done: On my graphics scene I have several ProxyWidgets. If I click on one of the widgets, the "selected" widget gets zoomed in. If I click again same (zoomed in) proxy widget, the proxy gets zoomed out. So far, so good . But here is the catch that needs to be impleneted and I simply do not know how to do it: If I press mouse on any of this proxy widgets, it zooms in and if I STILL HOLD the mouse button, for instance, 500ms, I must get zoomed out and event loop continues. So, if I click on window it gets zoomed in and another click zooms it out - that works. But If i hold mouse button, it zooms in, but how then zooms out after 500ms and event loop normally continues?
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mousePressEvent & mouseReleaseEvent question

    Don't treat mouse events as the basis for your functionality but just means to trigger it. If you have a zoomIn() and zoomOut() methods, you can call them from your event handlers. Then you can use a timer that will call zoomOut() 500ms after mousePressEvent has been handled (provided the timer is not canceled by releasing the mouse).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Quote Originally Posted by wysota View Post
    Don't treat mouse events as the basis for your functionality but just means to trigger it. If you have a zoomIn() and zoomOut() methods, you can call them from your event handlers. Then you can use a timer that will call zoomOut() 500ms after mousePressEvent has been handled (provided the timer is not canceled by releasing the mouse).
    But mouse event ARE basis for my functionality, its the same if I call method or call animation after 500ms, it does not work!
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mousePressEvent & mouseReleaseEvent question

    Define "does not work".

    Mouse events are meant for one purpose only - to represent the state of the mouse. You should not be trying to put additional fake events into the queue to control your application. If you want your application to do something after 500ms of holding down the mouse, then put that into your application via a timer that is triggered by a mouse press.

  7. The following user says thank you to squidge for this useful post:

    MarkoSan (7th December 2009)

  8. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Well, I mean, it is not important if I call method from event handler or handle code in evenhandler, it's same s....!
    Qt 5.3 Opensource & Creator 3.1.2

  9. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Quote Originally Posted by fatjuicymole View Post
    Define "does not work".

    Mouse events are meant for one purpose only - to represent the state of the mouse. You should not be trying to put additional fake events into the queue to control your application. If you want your application to do something after 500ms of holding down the mouse, then put that into your application via a timer that is triggered by a mouse press.
    AAAA, thanks! So you mean, inside mousePress event handler I create timer and start it and then I connect timeout() signal to zoomOut() method/slot? So, I also setup a state of window (i.e. WindowZoomState=ZOOM_IN) and in mouseRelease event handler I check the state. If the state is NOT ZOOM_IN then i zoomOut()? Thanks, will try!
    Qt 5.3 Opensource & Creator 3.1.2

  10. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Well, I did following:
    Qt Code:
    1. void WidgetProxy::mousePressEvent (QGraphicsSceneMouseEvent * event)
    2. {
    3. scene()->setActiveWindow(this);
    4.  
    5. // sets up timer
    6. QTimer mouseButtonPressTimer(this);
    7. mouseButtonPressTimer.setSingleShot(true);
    8. mouseButtonPressTimer.setInterval(PRESS_DURATION/*500*/);
    9. mouseButtonPressTimer.connect(&mouseButtonPressTimer,
    10. SIGNAL(timeout()),
    11. this,
    12. SLOT(transferMerchandiseToOrderCart()));
    13. mouseButtonPressTimer.start();
    14.  
    15. if(m_ZoomState==ZOOM_OUT)
    16. {
    17. mousePressEventTime()->start(); // sets current time to timer
    18.  
    19. emit zoomTransition(ZOOM_IN);
    20. }
    21. else
    22. {
    23. emit zoomTransition(ZOOM_OUT);
    24. }
    25. }
    26.  
    27. void WidgetProxy::transferToOrderCart()
    28. {
    29. // drops merchandise into order
    30. emit zoomTransition(ZOOM_OUT); // unselects merchandise
    31. }
    To copy to clipboard, switch view to plain text mode 
    , but transferToOrderCart() never gets called. Why??
    Qt 5.3 Opensource & Creator 3.1.2

  11. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent & mouseReleaseEvent question

    Try:

    Qt Code:
    1. void WidgetProxy::mousePressEvent (QGraphicsSceneMouseEvent * event)
    2. {
    3. scene()->setActiveWindow(this);
    4.  
    5. // sets up timer
    6. QTimer* mouseButtonPressTimer = new QTimer(this);
    7. mouseButtonPressTimer->setSingleShot(true);
    8. mouseButtonPressTimer->setInterval(PRESS_DURATION/*500*/);
    9. mouseButtonPressTimer->connect(mouseButtonPressTimer,
    10. SIGNAL(timeout()),
    11. this,
    12. SLOT(transferToOrderCart()));
    13. mouseButtonPressTimer->start();
    14.  
    15. if(m_ZoomState==ZOOM_OUT)
    16. {
    17. mousePressEventTime()->start(); // sets current time to timer
    18.  
    19. emit zoomTransition(ZOOM_IN);
    20. }
    21. else
    22. {
    23. emit zoomTransition(ZOOM_OUT);
    24. }
    25. }
    26.  
    27. void WidgetProxy::transferToOrderCart()
    28. {
    29. // drops merchandise into order
    30. emit zoomTransition(ZOOM_OUT); // unselects merchandise
    31. }
    To copy to clipboard, switch view to plain text mode 

    Your destination slot was wrong. Created the timer as a pointer.. because I'm not sure what happens, if it goes out of scope otherwise..

    HIH

    Johannes

  12. The following user says thank you to JohannesMunk for this useful post:

    MarkoSan (8th December 2009)

  13. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Hmm, now it works. but I have a leakage here, I think it is not good to to create timer every time mouse press is handled without deleting it! And now the window iz zoomed out regardless of holding the mouse button for PRESS_DURATION (which is ok) or just clicking on window. I need following functionality:

    1. if the mouse button is clicked on window, the window get zoomed in until the mouse button is clicked again (the window iz zoomed in because of showing some info inside it)
    2. if the mouse button is HELD down for a time of PRESS_DURATION [ms], the windows is zoomed out automaticly (the window was selected and info inside window has been transferred to some other object, after transfer the window is "deselected and is zoomed out)
    Last edited by MarkoSan; 8th December 2009 at 03:50.
    Qt 5.3 Opensource & Creator 3.1.2

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mousePressEvent & mouseReleaseEvent question

    Geez man... an example follows:

    Qt Code:
    1. class MyWidget : public QWidget {
    2. Q_OBJECT
    3. public:
    4. MyWidget(...) : QWidget(...) {
    5. m_zoomOutTimer.setInterval(500);
    6. m_zoomOutTimer.setSingleShot(true);
    7. connect(&m_zoomOutTimer, SIGNAL(timeout()), this, SLOT(zoomOut()));
    8. }
    9. public slots:
    10. void zoomOut() { zoomOut(currentItem); }
    11. void zoomIn() { ... }
    12. protected:
    13. void mousePressEvent(...) { zoomIn(); m_zoomOutTimer.start(); }
    14. void mouseReleaseEvent(...) {
    15. if(!m_zoomOutTimer.isActive()) return;
    16. m_zoomOutTimer.stop();
    17. zoomOut();
    18. }
    19. private:
    20. QTimer m_zoomOutTimer;
    21. ... currentItem;
    22. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: mousePressEvent & mouseReleaseEvent question

    Quote Originally Posted by wysota View Post
    Geez man... an example follows:

    Qt Code:
    1. class MyWidget : public QWidget {
    2. Q_OBJECT
    3. public:
    4. MyWidget(...) : QWidget(...) {
    5. m_zoomOutTimer.setInterval(500);
    6. m_zoomOutTimer.setSingleShot(true);
    7. connect(&m_zoomOutTimer, SIGNAL(timeout()), this, SLOT(zoomOut()));
    8. }
    9. public slots:
    10. void zoomOut() { zoomOut(currentItem); }
    11. void zoomIn() { ... }
    12. protected:
    13. void mousePressEvent(...) { zoomIn(); m_zoomOutTimer.start(); }
    14. void mouseReleaseEvent(...) {
    15. if(!m_zoomOutTimer.isActive()) return;
    16. m_zoomOutTimer.stop();
    17. zoomOut();
    18. }
    19. private:
    20. QTimer m_zoomOutTimer;
    21. ... currentItem;
    22. };
    To copy to clipboard, switch view to plain text mode 
    Well, geez man, that example (thanks for it), BUT IT DOES NOT WORK as it should. Now the windows zooms out either is press mouse button once (WRONG!) either i hold the mouse button (OK).
    Qt 5.3 Opensource & Creator 3.1.2

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mousePressEvent & mouseReleaseEvent question

    This is just an example of a concept of using a timer, adjust it to your needs. I'm sure you can do that after writing 585 posts in this forum. If you just blindly copy code pasted on the forum and expect it to do exactly what you need it to do then probably you should stop doing that and start thinking on your own.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 6th December 2009, 04:33
  2. Replies: 2
    Last Post: 1st November 2007, 08:25

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.