Results 1 to 7 of 7

Thread: Key Press Event trouble

  1. #1

    Default Key Press Event trouble

    Does any one know how to setup Key press event handeling?

    i can make head nor tail of the online docs on this issue and i just want to setup the escape button to trigger a function when pressed viewing the main window.

    do i have to add a method to the object of the mainwindow class to catch it or something?

    i just can see any examples on this at all (or any that make sense)


    your help on this would be much appreciated

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event trouble

    You can subclass the window and override the keyPressEvent.
    Or you can install filter on window to get its event...
    QWidget::keyPressEvent
    QObject::installEventFilter

  3. #3

    Default Re: Key Press Event trouble

    sorry but i looked at the docs and i cant make sense of it have yo please got a sample code to subclass the QMainWindow and add a keyPressEvent to it for QT:Key_Escape key
    ive tried every thing but i cant get it to work

    Thank you

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event trouble

    can we see wat u tried ? it shudne be a long code

  5. #5

    Default Re: Key Press Event trouble

    im doing this in python PTQT i have this code for the filter which is both an activity filter to reset a inactivity timer and a filter to react to the Escape key but its not working.


    Qt Code:
    1. class activityFilter(QObject):
    2.  
    3. def __init__(self, parent=None):
    4. super(activityFilter, self).__init__(parent)
    5. self.eventaction = turnOffApp()
    6. self.eventexit = MainDisplay()
    7.  
    8. def eventFilter(self, obj, event):
    9. if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape:
    10. self.eventexit.lockNow()
    11. else:
    12. self.eventaction.reset()
    13. return QObject.eventFilter(self, obj, event)
    To copy to clipboard, switch view to plain text mode 

    Then at the bottom of the page i call the main Window an put this filter on it

    Qt Code:
    1. def main():
    2. app = QApplication(sys.argv)
    3. app.setOrganizationName("random.")
    4. app.setOrganizationDomain("random")
    5. app.setApplicationName("random")
    6. app.setWindowIcon(QIcon(":/icon.png"))
    7.  
    8.  
    9. filename = os.path.join(os.path.dirname(os.path.dirname(__file__)), "dataCache.db")
    10. create = not QFile.exists(filename)
    11.  
    12. db = QSqlDatabase.addDatabase("QSQLITE")
    13. db.setDatabaseName(filename)
    14. if not db.open():
    15. QMessageBox.warning(None, "Store Application",
    16. QString("Database Error: %1").arg(db.lastError().text()))
    17. sys.exit(1)
    18. if create:
    19. createDatabaseData()
    20.  
    21. app.processEvents()
    22. form = MainWindow()
    23. ef = activityFilter()
    24. form.installEventFilter(ef)
    25. form.show()
    26. app.exec_()
    27.  
    28.  
    29. main()
    To copy to clipboard, switch view to plain text mode 


    the filter calls to other class functions which are definitely working the problem lies in the filter
    Last edited by morraine; 16th August 2008 at 10:00.

  6. #6
    Join Date
    Sep 2007
    Posts
    36
    Thanks
    5
    Thanked 3 Times in 1 Post

    Default Re: Key Press Event trouble

    this this...

    8. def eventFilter(self, obj, event):

    to

    def keyPressEvent(self, event):


    http://www.zetcode.com/tutorials/pyq...ntsandsignals/

    section "Reimplementing event handler"

  7. #7

    Default Re: Key Press Event trouble

    Thanks for getting back to me that look slike a good tutorial you have linked there. i will look at that today.

    Cheers!

Similar Threads

  1. Zooming on PushButton Press event
    By KosyakOFF in forum Qwt
    Replies: 5
    Last Post: 11th April 2008, 10:12
  2. Replies: 2
    Last Post: 2nd April 2008, 15:19
  3. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 19:39
  4. Replies: 1
    Last Post: 24th October 2007, 19:34
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 12:57

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.