Results 1 to 7 of 7

Thread: Change mouse cursor while the mouse is grabbed

  1. #1
    Join Date
    May 2019
    Posts
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Change mouse cursor while the mouse is grabbed

    Hi, I want to change the mouse cursor while the mouse is being grabbed by my widget. This is to react to different objects the mouse is over. QWidget::setCursor does not do the job. Under Windows a special care needs to be taken in that case (which I have done in the past not using Qt). How it can be done in Qt?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Change mouse cursor while the mouse is grabbed

    From the docs for QWidget::cursor():

    Some underlying window implementations will reset the cursor if it leaves a widget even if the mouse is grabbed. If you want to have a cursor set for all widgets, even when outside the window, consider QApplication:: setOverrideCursor().
    There is more information in QApplication::setOverrideCursor(). If you find that when using the QWidget setCursor() call that Windows is restoring the default cursor, then you may need to handle enter, leave, and mouseMove events in your widget to change the cursor back to what you want if Windows has changed it for you.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2019
    Posts
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: Change mouse cursor while the mouse is grabbed

    Thank you, d_stranz. I already have read the relative docs including the ones you refer. Thanks again, but did you read my question carefully? If so, please post a working solution to the problem.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Change mouse cursor while the mouse is grabbed

    If so, please post a working solution to the problem.
    If you have read other posts in this forum, you will understand that we aren't contractors who write your code for you. And typically, you don't get very good answers when you simply post something like "QWidget:: setCursor() does not do the job". Our response will amost always be "Why not? What have you tried?"

    You also asked:

    How it can be done in Qt?
    and I pointed you to the Qt documentation that I thought discussed the problem you might be having, and I suggested ways you might work around it.

    So the next thing for you to do is to post your non-working code and hopefully someone here will see why it isn't working and can help you fix it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    May 2019
    Posts
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: Change mouse cursor while the mouse is grabbed

    For those who have bumped the same problem and want a concrete solution (works for Qt 5.12.0):

    Qt Code:
    1. void SpyButton::mousePressEvent(QMouseEvent *ev)
    2. {
    3. MyBase::mousePressEvent(ev);
    4. grabMouse(m_cursor);
    5. }
    6.  
    7. void SpyButton::mouseMoveEvent(QMouseEvent *ev)
    8. {
    9. MyBase::mouseMoveEvent(ev);
    10. if (QGuiApplication::queryKeyboardModifiers()&Qt::ShiftModifier)
    11. {
    12. setCursor(m_cursorDifferent); // <- does nothing while mouse is gabbed
    13. QGuiApplication::changeOverrideCursor(m_cursorProhibited); // <- this works!
    14. }
    15. else
    16. {
    17. setCursor(m_cursor); // <- does nothing while mouse is grabbed
    18. QGuiApplication::changeOverrideCursor(m_cursor); // <- this works!
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    It seems like (I do not have the time to download and debug Qt now) that QWidget::grabMouse() uses QApplication::setOverrideCursor() and QApplication::restoreOverrideCursor() internally (which makes sense). Otherwise QGuiApplication::changeOverrideCursor() would not work in the suggested solution. The documentation explicitly states that QApplication::setOverrideCursor() shall be called before changeOverrideCursor().

    If we do not want to rely on QWidget::grabMouse() to internally call QApplication::setOverrideCursor() then I would suggest to ensure that one calls setOverrideCursor()/restoreOverrideCursor() pair explicitly in the code in the same way grabMouse() calls those on grab/release the mouse. Note that those must go in pairs because of the internal stack of cursors in the Qt application.

  6. #6
    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: Change mouse cursor while the mouse is grabbed

    Instead of
    Qt Code:
    1. QGuiApplication::queryKeyboardModifiers()&Qt::ShiftModifier
    To copy to clipboard, switch view to plain text mode 
    you could also write
    Qt Code:
    1. ev->modifiers() & Qt::ShiftModifier
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    May 2019
    Posts
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: Change mouse cursor while the mouse is grabbed

    Thanks anda_skoa
    I would imagine QGuiApplication::queryKeyboardModifiers()would check the state at the moment of call.
    ev->modifiers() would reflect the state in the moment of event generation.

Similar Threads

  1. Replies: 1
    Last Post: 19th April 2012, 10:49
  2. Replies: 0
    Last Post: 28th June 2011, 08:41
  3. Qgraphicsitem grabbed with mouse.
    By repka3 in forum Qt Programming
    Replies: 0
    Last Post: 31st August 2009, 15:34
  4. Replies: 1
    Last Post: 16th July 2009, 17:58
  5. Replies: 1
    Last Post: 10th July 2009, 10:54

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.