Results 1 to 2 of 2

Thread: QCompleter with dynamic data model

  1. #1
    Join Date
    Sep 2017
    Qt products

    Default QCompleter with dynamic data model

    Using Qt 4.8.5

    I want a dynamic data model for a QCompleter installed in a QLineEdit. I connect the QLineEdit::textEdited signal to my own function that updates the model, which is my sub-class of QAbstractListModel. It begins as an empty model, but fills itself out on the third keystroke. No completions occur. If I full populate the model when constructed, all works. See code below. I do not know how to notify the QCompleter that the model has changed. I used 'beginResetModel()/endResetModel()' as a try.

    Qt Code:
    1. // This slot is connected to the QLineEdit::textEdited signal.
    2. void MyaNameModel::Update(const QString &text)
    3. {
    4. if (text.size() < 3)
    5. { if (!names.empty())
    6. { m_names.clear(); // Clear data model's collection of names.
    8. // Notify QCompleter that model has changed.
    9. beginResetModel();
    10. endResetModel();
    11. }
    12. }
    13. else if (m_names.empty())
    14. { // This extracts information from an external repository via in-house API.
    15. // It fills the container of names.
    16. m_ap.MatchChannel(Glob((text + "*").toAscii().data()), m_names);
    18. // Notify QCompleter that model has changed.
    19. beginResetModel();
    20. endResetModel();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 808 Times in 797 Posts
    Qt products
    Windows Android

    Default Re: QCompleter with dynamic data model

    Typically, you call beginModelReset(), change the data, then call endModelReset(). In your case you are changing the data then calling the two reset methods. It probably doesn't make a difference in this particular case, but you should change your code to do it correctly anyway.

    I assume you have implemented the data() method for your model. The text displayed by the QCompleter comes from the value returned by the Qt::EditRole role. Be sure your data() method is returning values for this role.

    Finally, ensure you are calling QCompleter::setCompletionPrefix() with the QLineEdit text.

    BUT the problem with what you are doing is that what you are trying to do manually, looking up partial matches to names from a global database and then supplying them to QCompleter as completion candidates is exactly what QCompleter itself is designed to do.

    What you should be doing is giving QCompleter the entire list of names (preferably sorted) (i.e. m_ap.MatchChannel( Glob( "*" ), m_names )) once. All your slot needs to do at that point is to tell QCompleter to set the prefix string (QCompleter::setCompletionPrefix()) to whatever the current text is in the line edit. QCompleter will filter the list of names and send back the correct subset, all without your "help".
    <=== 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: 2
    Last Post: 14th July 2016, 17:26
  2. QCompleter performance for large data models
    By jpmgr in forum Qt Programming
    Replies: 4
    Last Post: 31st October 2015, 17:09
  3. Replies: 0
    Last Post: 13th May 2011, 08:11
  4. Replies: 0
    Last Post: 17th September 2010, 12:19
  5. Replies: 1
    Last Post: 12th October 2008, 08:21


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.