Results 1 to 7 of 7

Thread: mousePressEvent on a QWidget not working

  1. #1
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default mousePressEvent on a QWidget not working

    I am trying to implement mouse events, and the attached code works OK on a QDialog derived application. Why is it not working on a QWidget derived application?
    Attached Files Attached Files

  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: mousePressEvent on a QWidget not working

    Not sure what you are trying to do, but why do you not want mousePressEvent() to be called?

    You have quite some code in that method, so not getting it called sounds like the opposite of what you would want.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: mousePressEvent on a QWidget not working

    I was trying to understand how events, specific events like mouse and kb events and signals work. So I made the simplest possible application. I want to use this to do a pop up menu on another widget.

    Do you have any other way to do it? From a conceptual point of view, this should work. Usually, when something simple like this does not work, I am not understanding something funndamental.

    Narada

  4. #4
    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: mousePressEvent on a QWidget not working

    Quote Originally Posted by Narada View Post
    From a conceptual point of view, this should work.
    I guess it depends on what your goal is.
    If you goal was to bypass the event handling of QWidget then yes this is how it works.

    But the thread's title suggests that you wanted mousePressEvent to be called, which bypassing the necessary implementation in QWidget will make impossible, conceptually and actually.

    So maybe you could clarify if you wanted to bypass the event widget event handling and are surprised that is actually worked, or of you intended to implement the mouse press event handler and think it should miraculously work even if you bypass the code that calls it.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: mousePressEvent on a QWidget not working

    Actually, I didn't mean to bypass the event handling of the QWidget. I simply wanted to use the event. The way I have used it is typical in many of the examples in the Qt books. The funny thing is instead of QWidget, if I use QDialog with exactly the same code, it works.

  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: mousePressEvent on a QWidget not working

    Quote Originally Posted by Narada View Post
    Actually, I didn't mean to bypass the event handling of the QWidget.
    But you do
    Qt Code:
    1. return QObject::event(e);
    To copy to clipboard, switch view to plain text mode 
    You bypass the implementation of QWidget::event(), which contains the type switch that calls the specific virtual function in QWidget.

    Quote Originally Posted by Narada View Post
    The way I have used it is typical in many of the examples in the Qt books.
    Maybe the mousePressEvent() override, I doubt these examples also bypass the very method that makes the calls to this methods.

    Quote Originally Posted by Narada View Post
    The funny thing is instead of QWidget, if I use QDialog with exactly the same code, it works.
    Interesting, that should not work either.
    Maybe it was not "exactly the same code"?

    Cheers,
    _

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

    Narada (5th April 2016)

  8. #7
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: mousePressEvent on a QWidget not working

    Hello anda_skoa,

    Thanks for sticking with the thread until I resolved the issue.

    You are correct. I took the event() override out, and it works now. My QDialog implementation did not have that override, and that is why it worked.

    One more step further in my learning how event(), mousePressEvent() ( and others), and signals interact with each other.

    If there is a good discussion on these three areas I would be happy to read it.

    Thank you again.

    Narada

Similar Threads

  1. Subclassing QWidget not working
    By guidupas in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2014, 18:48
  2. Replies: 1
    Last Post: 12th April 2014, 10:55
  3. Border-image not working in QWidget?
    By astampor in forum Qt Programming
    Replies: 4
    Last Post: 3rd October 2011, 15:16
  4. Replies: 27
    Last Post: 13th September 2007, 09:01
  5. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 22:24

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.