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

  • Home
  • SEARCH
  • 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 8375857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:17:04+00:00 2026-06-09T15:17:04+00:00

using PopupCompletion mode when you select an item (using arrow keys) and press return

  • 0

using PopupCompletion mode when you select an item (using arrow keys) and press return – lineEdit should become empty (i clear lineEdit when return is pressed), but lineEdit does not become empty. (If you press ‘Enter’ again it will empty the lineEdit). So i think pressing return does clear lineEdit, but pressing return also tells QCompleter to insert selected item into lineEdit, so it seems like nothing happens.

But, if you click the item insted of selecting it with arrows – everything works fine.

I tried to find the solution on the internet, but i found only one person that had the same problem: http://lists.trolltech.com/qt-interest/2006-10/thread00985-0.html . Sadly there are no answers. Please read his question because it will help understand my problem.

How can I clean LineEdit after QCompleter inserted selected item? (catching activated signal does not help)

  • 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-06-09T15:17:05+00:00Added an answer on June 9, 2026 at 3:17 pm

    The issue here is that the completer actually contains a pop-up, which is actually a separate QAbstractItemView widget (refer to the QCompleter::popup() documentation). As such, when you press ‘Enter’ on the QCompleter, the key event actually goes to the pop-up and not the line edit.

    There are two different ways to resolve your issue:

    Option 1

    Connect the completer’s activated signal to the line edit’s clear slot, but do it as a QueuedConnection:

    QObject::connect(completer, SIGNAL(activated(const QString&)),
                     lineEdit, SLOT(clear()),
                     Qt::QueuedConnection);
    

    The reason why using a direct connection doesn’t work is because your are essentially dependent on the order in which slots get called from a signal. Using a QueuedConnection gets around this. From a code maintenance standpoint, I don’t really prefer this solution because it isn’t clear what your intention is just by looking at the code.

    Option 2

    Write an event filter around the pop-up to filter out the ‘Enter’ key to clear the line edit explicitly. Your event filter would end up looking something like this:

    class EventFilter : public QObject
    {
       Q_OBJECT
    public:
       EventFilter(QLineEdit* lineEdit, QObject* parent = NULL)
          :QObject(parent)
          ,mLineEdit(lineEdit)
       { }
       virtual ~EventFilter()
       { }
    
       bool eventFilter(QObject* watched, QEvent* event)
       {
          QAbstractItemView* view = qobject_cast<QAbstractItemView*>(watched);
          if (event->type() == QEvent::KeyPress)
          {
             QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
             if (keyEvent->key() == Qt::Key_Return || 
                 keyEvent->key() == Qt::Key_Enter)
             {
                mLineEdit->clear();
                view->hide();
                return true;
             }
          }
          return false;
       }
    
    private:
       QLineEdit* mLineEdit;
    };
    

    You would then install the event filter on the completer’s pop-up:

    EventFilter* filter = new EventFilter(lineEdit);
    completer->popup()->installEventFilter(filter);
    

    This option is more work, but it’s clearer as to what you are doing. Moreover, you can perform additional customization this way, if you prefer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2008 R2 we are looking for a way to select the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using JQuery, how do I select all elements with class x within an element
Using the following query: SELECT sum(severity) as Score, DATE_FORMAT(LAST_UPDATE,%Y-%m) as score_date FROM missingpatches GROUP
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the

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.