Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:34+00:00 2026-05-27T22:25:34+00:00

I am using such constructs to test whether a needed key is pressed: def

  • 0

I am using such constructs to test whether a needed key is pressed:

def eventFilter(self, tableView, event):
    if event.type() == QtCore.QEvent.KeyPress:
        key = event.key()
        if event.modifiers() in (QtCore.Qt.NoModifier, QtCore.Qt.KeypadModifier):
            if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
                self.menu.editItem.trigger()
                return True

I know that ‘Premature optimization is the root of all evil’, but i think eventFilter is called quite often to think about its optimization.

My concerns:

  1. if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return) upon each run makes double lookup: 1. Find Qt attribute in QtCore module; 2. Find Key_Enter attribute in Qt module.
  2. if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return) this constructs the tuple upon each run. The search in the tuple is sequential – better to use frozenset?

How do you deal with such cases? Don’t care?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:25:34+00:00Added an answer on May 27, 2026 at 10:25 pm

    Your code:

    def eventFilter(self, tableView, event): 
        if event.type() == QtCore.QEvent.KeyPress: 
            key = event.key() 
            if event.modifiers() in (QtCore.Qt.NoModifier, QtCore.Qt.KeypadModifier): 
                if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): 
                    self.menu.editItem.trigger() 
                    return True
    

    As I mentioned in a comment to @interjay, there may be a multitude of calls to this function for any kind of UI event, and if you have many of these filters, they could make for a sluggish UI. If you wanted to optimize this at least as far as the first if test, then move the local definition of QtCore.QEvent.KeyPress into a default argument value:

    def eventFilter(self, tableView, event,
        FILTER_EVENT_TYPE=QtCore.QEvent.KeyPress
        ): 
        if event.type() == FILTER_EVENT_TYPE: 
            if event.modifiers() in (QtCore.Qt.NoModifier, QtCore.Qt.KeypadModifier): 
                key = event.key() 
                if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): 
                    self.menu.editItem.trigger() 
                    return True
    

    (I also moved the function call to event.key() to after the test on event.modifiers().)

    Default arguments like this are evaluated once at function compile time when the module is imported, not once per call, so your lookups for QtCore.QEvent.KeyPress will be accelerated. You could of course take this to the extreme of:

    def eventFilter(self, tableView, event,
        FILTER_EVENT_TYPE=QtCore.QEvent.KeyPress
        FILTER_MODIFIERS=(QtCore.Qt.NoModifier, QtCore.Qt.KeypadModifier),
        FILTER_KEYS=(QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return)
        ): 
        if (event.type() == FILTER_EVENT_TYPE and
            event.modifiers() in FILTER_MODIFIERS and 
            event.key() in FILTER_KEYS): 
                self.menu.editItem.trigger() 
                return True
    

    Now you have optimized not only the module-object-attribute lookups, but also the tuple constructions, and as @AndrewDalke mentions, my testing of in shows it is faster for tuples than sets up to a size of about 3 or 4 elements. The single condition will still short-circuit when any part of the condition fails, so you won’t get calls to event.modifiers or event.key if the type is not a keypress.

    EDIT: I like @ekhumoro’s coupled testing of key and modifier, here’s how it would look merged into my code:

    def eventFilter(self, tableView, event,
        FILTER_EVENT_TYPE=QtCore.QEvent.KeyPress
        FILTER_KEY_MODIFIERS=((QtCore.Qt.Key_Return, QtCore.Qt.NoModifier),
                              (QtCore.Qt.Key_Enter, QtCore.Qt.KeypadModifier),
                              )
        ): 
        if (event.type() == FILTER_EVENT_TYPE and
            (event.key(), event.modifiers()) in FILTER_KEY_MODIFIERS): 
                self.menu.editItem.trigger() 
                return True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a CF8 form, I'm using a tag such as the following: <cfinput type
I'm building a blog engine to test some concepts such us TDD and using
Using C# 3.0, we can initialize objects without their constructors for syntactical reasons. Such
When using resources such as brushes, templates and styles in WPF, they can be
When creating a website using Asp.Net and using controls such as the ListView is
How do you safely encode a URL using JavaScript such that it can be
how string.format() can help to avoid using + in such statement: string statement =
I have a Qt application in Visual Studio 2005 which is linked using \subsystem:windows such that
Using ASP.NET MVC there are situations (such as form submission) that may require a
When using third-party libraries such as jquery, yui-reset, swfobject and so on, do you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.