Results 1 to 5 of 5

Thread: Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWindow

  1. #1
    Join Date
    Dec 2011
    Posts
    47
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWindow

    I'm unable to successfully capture a mouse click in my QMainWindow for subclassed child widgets, such those that live inside a subclassed QTabWidget, like buttons, QTextEdits, etc.

    I install a mousePressEvent() filter on the QMainWindow class.

    Qt Code:
    1. void MyTool::mousePressEvent(QMouseEvent *event)
    2. {
    3. qDebug() << "mousePressEvent";
    4. QMainWindow::mousePressEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I also pass the QMainWindow parent pointer to the subclassed child QTabWidget.

    When I click around in the UI, I get a successful mouse press event if I click on whitespace (such as the tab widget frame). Good.

    But I do not get a mouse press event if I click on any QWidget inside the QTabWidget, such as buttons or a text edit. These widgets would (I suppose) be grand-children of the QMainWindow.

    Shouldn't I be able to install a mouse event filter on the parent and receive events for a click on any subclassed descendant? Or, do I need event filters at all child levels?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWind

    QMainWindow::mousePressEvent(event);
    Why do you think that this line of code will result in the event being passed to your QMainWindow instance? QMainWindow::mousePressEvent() is not a static function; it must be called through a pointer to a QMainWindow. The only way this could be correct syntax would be if MyTool was derived from QMainWindow.

    I install a mousePressEvent() filter on the QMainWindow class.
    This code does not install an event filter on anything. It overrides and re-implements the virtual mousePressEvent() of whatever class MyTool is derived from. A Qt "event filter" is a specific type of function that can be implemented in one class instance to intercept events generated for an instance of another class.
    <=== 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
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,216
    Thanked 849 Times in 798 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWind

    Quote Originally Posted by alketi View Post
    Or, do I need event filters at all child levels?
    Input events are processed bottom up - until they are accepted. So yes, when clicking on widgets like buttons the parent does not get them anymore.
    Installing event filters is one solution, another one is to overload QApplication::notify if you want to process the event before.

    In Qt/Quick there is a QQuickItem::childMouseEventFilter, that is made for detecting gestures.
    I havn't checked how gestures are done for widgets, but maybe you find something in this area that can be used for your purposes.

    HTH,
    Uwe

  4. #4
    Join Date
    Dec 2011
    Posts
    47
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWind

    Quote Originally Posted by d_stranz View Post
    Why do you think that this line of code will result in the event being passed to your QMainWindow instance? QMainWindow::mousePressEvent() is not a static function; it must be called through a pointer to a QMainWindow. The only way this could be correct syntax would be if MyTool was derived from QMainWindow.
    Well, I think that this line of code will be passed to my QMainWindow instance because MyTool is derived from QMainWindow, as you stated in your second sentence.

    Thank you Uwe. Your explanation makes perfect sense.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Can't catch mouse-click in MainWindow's subclassed child widgets from my MainWind

    Well, I think that this line of code will be passed to my QMainWindow instance because MyTool is derived from QMainWindow
    So then why did you ask your question this way?

    "I'm unable to successfully capture a mouse click in my QMainWindow for subclassed child widgets, such those that live inside a subclassed QTabWidget, like buttons, QTextEdits, etc."

    None of these widgets are derived from QMainWindow. You then go on to talk about "MyTool", which based on the above, I assumed was some child widget embedded in a tab widget somewhere.

    What Uwe says is correct - if a child widget accepts a mouse event, then it gets eaten and doesn't pass further up the chain of parents. If you need the actual mouse event, then writing an event filter for your QMainWindow class and installing it on the child widget is one solution. It is less brute-force than using QApplication::notify().

    If you don't need the actual muse events, it might be cleaner to try to use signals generated by the child widgets or subclass the child widget, override the mouse event, and emit a signal when it occurs.
    <=== 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.

Similar Threads

  1. Replies: 2
    Last Post: 20th April 2016, 16:52
  2. Catch mouse click to hide item
    By joko in forum Qt Quick
    Replies: 2
    Last Post: 10th November 2014, 12:16
  3. Replies: 4
    Last Post: 17th April 2010, 03:42
  4. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 09:39
  5. Closing all of the mainWindow's child dialogs
    By JPNaude in forum Qt Programming
    Replies: 4
    Last Post: 2nd October 2008, 14:18

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.