Results 1 to 2 of 2

Thread: Handling touch events for parent and child widgets

  1. #1
    Join Date
    Nov 2020
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Question Handling touch events for parent and child widgets

    Hello!

    There are two widgets - parent (main window) and child (pop-up menu):
    Qt Code:
    1. from PyQt5.QtCore import *
    2. from PyQt5.QtGui import *
    3. from PyQt5.QtWidgets import *
    4.  
    5. class Window(QWidget):
    6. def __init__(self):
    7. QWidget.__init__(self)
    8. self.menu = Menu(parent=self)
    9. self.menu.hide()
    10. ...
    11.  
    12. class Menu(QWidget):
    13. def __init__(self, parent):
    14. QWidget.__init__(self, parent)
    15. ...
    To copy to clipboard, switch view to plain text mode 


    By default, menu is hidden. The menu should be shown when touch on the main window area. This task is easily solved with QEvent.TouchBegin:
    Qt Code:
    1. def eventFilter(self, obj, event):
    2. if event.type() == QEvent.TouchBegin: self.menu.show()
    To copy to clipboard, switch view to plain text mode 

    The menu should become hidden when touch on any area of the main window that doesn't have a menu above it (a touch outside the menu).

    Is there an easy way to detect this event (without using touch coordinates)?

  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: Handling touch events for parent and child widgets

    I am not sure why you are using an event filter when all you really need to do is override the QWidget::event() method in your main window class to look for TouchBegin events. When you detect a touch, if the menu is not visible, you show it, if it is visible, you hide it. The menu itself will handle its own touch events and will hide itself when an action is triggered.

    Event filters are generally used when you want one QObject to spy on what is happening in another QObject, not to monitor events within a QObject itself.

    Also, why are you deriving Menu from QWidget and not QMenu?
    <=== 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: 3
    Last Post: 18th May 2012, 10:42
  2. Handling parent und child events at once.
    By dentist in forum Newbie
    Replies: 3
    Last Post: 2nd April 2010, 11:55
  3. Replies: 9
    Last Post: 22nd June 2008, 23:26
  4. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 19:44
  5. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 09:11

Tags for this Thread

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.