Results 1 to 5 of 5

Thread: Receiving events while mouse is pressed

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Receiving events while mouse is pressed

    Hi,

    Qt Code:
    1. QMenu menu;
    2.  
    3. menu.addAction("Item 1");
    4. menu.addAction("Item 2");
    5. menu.addAction("Item 3");
    6.  
    7. qApp->setStyleSheet("QMenu::item:selected { background: rgba(0, 0, 128, 20%); }");
    To copy to clipboard, switch view to plain text mode 
    This example creates a simple QMenu where the backgrounds of the single items get colored when the mouse is hovering over them. This also works if the mouse is pressed on "Item 1" and then hovered over the other ones (not releasing the mouse button).


    Is there a way to achieve this behaviour on widgets in a layout?
    Qt Code:
    1. QVBoxLayout layout;
    2.  
    3. layout.addWidget(new QLabel("Item 1"));
    4. layout.addWidget(new QLabel("Item 2"));
    5. layout.addWidget(new QLabel("Item 3"));
    6.  
    7. qApp->setStyleSheet("QLabel:hover { background: rgba(0, 0, 128, 20%); }");
    To copy to clipboard, switch view to plain text mode 
    With this example, the labels only get colored when the mouse is hovering them without a mouse button being pressed. If I press and hold the left mouse button on "Item 1" on hover over the other items, "Item 1" remains being colored. The others will not get colored.

    I guess this is a problem of event propagation. When the mouse is pressed and moved, only "Item 1" receives events (QEvent::MouseMove and QEvent::HoverMove). The others are not receiving any type of event.

    So is there a way to solve this problem, i.e. to let the other items also receive the events? Or if it is not a problem of event propagation, how do I achieve the same behaviour like on the QMenu?


    Thanks in advance for your help.

  2. #2
    Join Date
    Apr 2012
    Location
    India.
    Posts
    88
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Receiving events while mouse is pressed

    Read QMouseEvent class documentation, It says ....

    "Qt automatically grabs the mouse when a mouse button is pressed inside a widget; the widget will continue to receive mouse events until the last mouse button is released."

    So think about it and you should try setting "mouseTracking" false when mouse button is pressed, see if it works for you.

Similar Threads

  1. How to detect hover events when mouse button is pressed
    By yagabey in forum Qt Programming
    Replies: 12
    Last Post: 26th April 2016, 09:23
  2. Replies: 0
    Last Post: 3rd May 2013, 16:53
  3. Replies: 5
    Last Post: 10th August 2010, 04:39
  4. how to do this ,mouse pressed,then can move the window
    By tsuibin in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2009, 19:04
  5. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 11:21

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.