Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Dynamic QCompleter for QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Dynamic QCompleter for QLineEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 644 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Numerlor
    wrote on last edited by
    #1

    I'm trying to get a QCompleter working for a QLineEdit. The completer's model is dynamically filled with values from a network request that's made with the line edit's text, but with the delayed update, the completer's popup is hidden and doesn't show up unless the line edit's text matches something from what was in the model previously.

    Calling the completer's complete method manually works, but causes flicker because the completer hides its popup on key events.
    Is there any way of getting this to work nicely, or will I have to reimplement some of the completer's methods to get it working?

    from PySide6 import QtCore, QtWidgets
    from __feature__ import snake_case, true_property  # noqa: F401
    
    class Window(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.main_widget = QtWidgets.QWidget(self)
            self.set_central_widget(self.main_widget)
            self.layout_ = QtWidgets.QVBoxLayout(self.main_widget)
    
            self.line_edit = QtWidgets.QLineEdit(self)
            self.layout_.add_widget(self.line_edit)
    
            self.completer_model = QtCore.QStringListModel(self)
            self.completer = QtWidgets.QCompleter(self.completer_model, self)
            self.line_edit.textEdited.connect(self.update_model)
            self.line_edit.set_completer(self.completer)
    
        def update_model(self, query: str):
            """Simulate the network requests that calls self.update_callback when finished."""
            QtCore.QTimer.single_shot(0, lambda: self.update_callback(query))
            #self.completer_model.set_string_list([query + "completedtext"])
    
        def update_callback(self, query):
            self.completer_model.set_string_list([query + "completedtext"])
    
    app = QtWidgets.QApplication()
    window = Window()
    window.show()
    app.exec()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you consider creating a custom model to handle the data ? Or do you have a particular reason to replace all the entries every time ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Numerlor
        wrote on last edited by
        #3

        The entries are suggestions from an api for the query, so they can't be created locally

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hence my suggestion of a custom model.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Numerlor
            wrote on last edited by
            #5

            I'm not particularly familiar with models around Qt, how would a custom model differ from the current model that's updated? The values have to always be fetched after the text is changed, which will update the model after a response is received

            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