Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make QLineEdit query QCompleter again

Make QLineEdit query QCompleter again

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 236 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephan Lorenzen
    wrote on last edited by
    #1

    I want to enable autocompletion in a QLineEdit where the autocompletion suggesions are the results of a query to a website using the content of the QLineEdit.

    I set a QCompleter for the QLineEdit and set the Model for the QCompleter to an object of my own class StockCompleterderived from QAbstracTableModel. I connected QLineEdit::textChanged(const QString&) to a slot StockCompleter::textChanged(const QString&) in my class derived from QAbstracTableModel. This function then queries a website using a QNetworkAccessManager and, after the results arrive, updates its model (calling beginResetModel() and endResetModel()) so that the autocomplete suggestions should be shown. The data(const QModelIndex&, int) function always return the current text of the QLineEdit for the EditRole.

    However, I see the following behavior: Whenever I press a key in the QLineEdit, immediately StockCompleter::data(const QModelIndex&, int) is called. Since that still contains the "old" autocompletion suggestions (before the key was pressed), nothing matches and no autocompletions are shown. Only after that, StockCompleter::textChanged(const QString&) is called, which calls the website. When the website is loaded, the downloadFinished(QNetworkReply*) function is called and the autocompletion suggestions in my class are updated (calling beginResetModel() and endResetModel()). However, the QLineEdit does not "ask" the QCompleter again for suggestions unless I press a key again, when the same procedure starts over again (first asking for completions which at that timepoint do not match the content of the QLineEdit, then updating the autocompletion suggestions).

    I tried several things to force the QLineEdit to "ask" the QCompleter for suggestions again after updating the suggestions in StockCompleter::downloadFinished(QNetworkReply*), including

    	completer()->setModel(this);
    	lineEdit()->setCompleter(nullptr);
    	lineEdit()->setCompleter(completer());
    	lineEdit()->setText(query());
    
    

    Nothing helps. In principle, the QCompleter works; if my StockCompleter::data(const QModelIndex&, int) function always returns "ho" for QEditRole then the QLineEdit shows suggestions if I enter "ho". But, of course, I want to update these suggestions after the autocompletion suggestions are downloaded from the website in my StockCompleter::downloadFinished(QNetworkReply*) function.

    What am i doing wrong?

    JonBJ 1 Reply Last reply
    0
    • S Stephan Lorenzen

      I want to enable autocompletion in a QLineEdit where the autocompletion suggesions are the results of a query to a website using the content of the QLineEdit.

      I set a QCompleter for the QLineEdit and set the Model for the QCompleter to an object of my own class StockCompleterderived from QAbstracTableModel. I connected QLineEdit::textChanged(const QString&) to a slot StockCompleter::textChanged(const QString&) in my class derived from QAbstracTableModel. This function then queries a website using a QNetworkAccessManager and, after the results arrive, updates its model (calling beginResetModel() and endResetModel()) so that the autocomplete suggestions should be shown. The data(const QModelIndex&, int) function always return the current text of the QLineEdit for the EditRole.

      However, I see the following behavior: Whenever I press a key in the QLineEdit, immediately StockCompleter::data(const QModelIndex&, int) is called. Since that still contains the "old" autocompletion suggestions (before the key was pressed), nothing matches and no autocompletions are shown. Only after that, StockCompleter::textChanged(const QString&) is called, which calls the website. When the website is loaded, the downloadFinished(QNetworkReply*) function is called and the autocompletion suggestions in my class are updated (calling beginResetModel() and endResetModel()). However, the QLineEdit does not "ask" the QCompleter again for suggestions unless I press a key again, when the same procedure starts over again (first asking for completions which at that timepoint do not match the content of the QLineEdit, then updating the autocompletion suggestions).

      I tried several things to force the QLineEdit to "ask" the QCompleter for suggestions again after updating the suggestions in StockCompleter::downloadFinished(QNetworkReply*), including

      	completer()->setModel(this);
      	lineEdit()->setCompleter(nullptr);
      	lineEdit()->setCompleter(completer());
      	lineEdit()->setText(query());
      
      

      Nothing helps. In principle, the QCompleter works; if my StockCompleter::data(const QModelIndex&, int) function always returns "ho" for QEditRole then the QLineEdit shows suggestions if I enter "ho". But, of course, I want to update these suggestions after the autocompletion suggestions are downloaded from the website in my StockCompleter::downloadFinished(QNetworkReply*) function.

      What am i doing wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Stephan-Lorenzen
      I don't claim to understand entirely, and I have not even used a QCompleter, but in 2009(!) https://stackoverflow.com/questions/1927289/how-to-update-qcompleters-model-dynamically said

      It seems that the model should be set before QLineEdit emits textChanged signal. One way is to reimplement keyPressEvent, but it involves many conditions to get QLineEdit's text.

      and the OP claimed to answer his own question with

      Oh, I have found the answer:

      Use signal textEdited instead of textChanged.

      Debuging QT's source code told me the answer.

      Does this help any?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved