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 9145561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:32:42+00:00 2026-06-17T10:32:42+00:00

The QLineEdit is for entering post code. User may also input city name, while

  • 0

The QLineEdit is for entering post code. User may also input city name, while QCompleter will display a list of names for user to select. The problem is, on selecting the name in completer, how could the post code be put in the QLineEdit?
I tried to connect QCompleter::activated(QModelIndex) to slot that change the QLineEdit text to post code. But later the text was again set to city name by QLineEdit.

  • 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-17T10:32:43+00:00Added an answer on June 17, 2026 at 10:32 am

    Sorry, my previous answer was not correct, so I’ve edited it.

    As the documentation says:

    QString QCompleter::pathFromIndex ( const QModelIndex & index ) const
    [virtual]

    Returns the path for the given index. The completer object
    uses this to obtain the completion text from the underlying model. The
    default implementation returns the edit role of the item for list
    models. It returns the absolute file path if the model is a QDirModel.

    I’ve got what you need by subclassing QCompleter and reimplementing pathFromIndex:

    class CodeCompleter : public QCompleter
    {
        Q_OBJECT
    public:
        explicit CodeCompleter(QObject *parent = 0);
        
        static const int CompleteRole;
    
        QString pathFromIndex(const QModelIndex &index) const;
    };
    
    const int CodeCompleter::CompleteRole = Qt::UserRole + 1;
    
    CodeCompleter::CodeCompleter(QObject *parent) :
        QCompleter(parent)
    {
    
    }
    
    QString
    CodeCompleter::pathFromIndex(const QModelIndex &index) const
    {
        QMap<int, QVariant> data = model()->itemData(index);
        QString code = data.value(CompleteRole).toString();
        return code;
    }
    

    And you can use it like this:

    QStringList cities;
        cities << "Moscow" << "London" << "Las Vegas" << "New York";
    
        QStandardItemModel *model = new QStandardItemModel;
        for (int i = 0; i < cities.count(); ++i)
        {
            QString city = cities.at(i);
            QString code = city.at(0) + QString::number(city.length());///< just an example
    
            QStandardItem *item = new QStandardItem;
            item->setText(city);
            item->setData(code, CodeCompleter::CompleteRole);
    
            model->appendRow(item);
        }
    
        QLineEdit *lineEdit = new QLineEdit(this);
    
        CodeCompleter *completer = new CodeCompleter(this);
        completer->setModel(model);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        lineEdit->setCompleter(completer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QLineEdit where the user should input only numbers. So is there
I want to use QLineEdit for input. My problem is that I don't know
I've got a QList of QLineEdit*'s QList<QLineEdit*> example; Example will hold 100 items of
With a QLineEdit is it possible to display the thousand separator of a number
I am making a name field using QlineEdit. I want the entry in this
I have this code: myEdit = QLineEdit() myQFormLayout.addRow(myLabelText, myEdit) Now I have to remove
I have a QLineEdit with a QCompleter. I want a slot myslot() called when
Is there any standard way to create drop-down menu from QLineEdit without QCompleter? For
I'm trying to display a warning icon next to a QLineEdit if it contains
So currently I have code, in C++, that creates a few QLabels, a QLineEdit,

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.