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. How do I update a QCompleter with a new strings list?
Forum Updated to NodeBB v4.3 + New Features

How do I update a QCompleter with a new strings list?

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 903 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.
  • U Offline
    U Offline
    user239857
    wrote on last edited by user239857
    #1

    I have the following code which makes a completer with two auto-completes.

    test_list.append("APPLE")
    test_list.append("ORANGE")
    completer = QCompleter(test_list, self)
    self.ui.lineText.setCompleter(completer)
    # CHECKPOINT
    test_list.append("KIWI")
    

    When I append strings after CHECKPOINT to test_list, the completer is not updated. test_list has KIWI but lineText auto-complete only completes APPLE and ORANGE. It seems I have to update the completer but I don't know how..

    What do I do?

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @user239857

      Disclaimer: I'm unfamiliar with Qt for Python... but hopefully this info set you in the right direction.

      The problem is that the QCompleter takes a copy of your list, and creates a new model (a QStringListModel in this case) to use, which is no longer associated with your test_list.

      There's probably better, options, but something like this should work:

      test_list.append("APPLE")
      test_list.append("ORANGE")
      model = QStringListModel(tes_list);
      completer = QCompleter(model, self)
      self.ui.lineText.setCompleter(completer)
      # CHECKPOINT
      test_list.append("KIWI")
      model.setStringList(test_list);
      # Or:
      model.setStringList(model.stringList().append("KIWI"));
      

      You could be more efficient, by appending to model instead of replacing the list each time, but that's a little more complicated. Ideally you'd beginInsertRows(), add the new row, set the (string) data, then endInsertRows(). Which would be very efficient, but significantly more code.

      Cheers.

      U 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @user239857

        Disclaimer: I'm unfamiliar with Qt for Python... but hopefully this info set you in the right direction.

        The problem is that the QCompleter takes a copy of your list, and creates a new model (a QStringListModel in this case) to use, which is no longer associated with your test_list.

        There's probably better, options, but something like this should work:

        test_list.append("APPLE")
        test_list.append("ORANGE")
        model = QStringListModel(tes_list);
        completer = QCompleter(model, self)
        self.ui.lineText.setCompleter(completer)
        # CHECKPOINT
        test_list.append("KIWI")
        model.setStringList(test_list);
        # Or:
        model.setStringList(model.stringList().append("KIWI"));
        

        You could be more efficient, by appending to model instead of replacing the list each time, but that's a little more complicated. Ideally you'd beginInsertRows(), add the new row, set the (string) data, then endInsertRows(). Which would be very efficient, but significantly more code.

        Cheers.

        U Offline
        U Offline
        user239857
        wrote on last edited by
        #3

        @Paul-Colby This was extremely helpful and solved it. Thank you!

        1 Reply Last reply
        0
        • SGaistS SGaist has marked this topic as solved on

        • Login

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