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. What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?
Forum Updated to NodeBB v4.3 + New Features

What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.4k 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.
  • Y Offline
    Y Offline
    YJMForgive
    wrote on last edited by
    #1

    I have a Qt desktop application with a QLineEdit that filters items in a QListWidget. I need to handle two scenarios:

    • When user types/changes text in QLineEdit:
      • Need to update QListWidget to show filtered items
    • When user clears the QLineEdit:
      • Need to restore/show all items

    I'm considering these approaches:

    • Clear the entire QListWidget and repopulate it from scratch each time
    • Use setHidden()/setVisible() on items

    Questions:

    Which approach would be more performant, especially with large datasets?
    Is there a better alternative I haven't considered?

    Environment:

    • Qt 6.x
    • Platform: Windows

    Any insights on best practices for this common UI pattern would be appreciated.

    JonBJ 1 Reply Last reply
    0
    • Y YJMForgive

      @JonB I'm not looking for QLineEdit autocompletion. Instead, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

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

      @YJMForgive said in What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?:

      , I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

      It's just that this is exactly what QCompleter does, on a list in a popup rather than in a QLisWidget. And you can react on user clicking an item in the filtered list.

      Anyway, accepting that QCompleter is not what you want for whatever reason, it uses the approach of a model and a filter in the same way as described in @Pl45m4's suggestion. So you should follow that.

      1 Reply Last reply
      0
      • Y YJMForgive

        I have a Qt desktop application with a QLineEdit that filters items in a QListWidget. I need to handle two scenarios:

        • When user types/changes text in QLineEdit:
          • Need to update QListWidget to show filtered items
        • When user clears the QLineEdit:
          • Need to restore/show all items

        I'm considering these approaches:

        • Clear the entire QListWidget and repopulate it from scratch each time
        • Use setHidden()/setVisible() on items

        Questions:

        Which approach would be more performant, especially with large datasets?
        Is there a better alternative I haven't considered?

        Environment:

        • Qt 6.x
        • Platform: Windows

        Any insights on best practices for this common UI pattern would be appreciated.

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

        @YJMForgive Are you aware of QCompleter, which already behaves this way?

        Y 1 Reply Last reply
        0
        • JonBJ JonB

          @YJMForgive Are you aware of QCompleter, which already behaves this way?

          Y Offline
          Y Offline
          YJMForgive
          wrote on last edited by
          #3

          @JonB I'm not looking for QLineEdit autocompletion. Instead, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

          Pl45m4P JonBJ 2 Replies Last reply
          0
          • Y YJMForgive

            @JonB I'm not looking for QLineEdit autocompletion. Instead, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @YJMForgive

            For this you should look into Qt's filter models...

            E.g.

            • https://doc.qt.io/qt-6/qtwidgets-itemviews-customsortfiltermodel-example.html

            To use such model/view behavior you probably have to put QListWidget aside and use your own implementation of QListView with your custom model.

            for a proper filter model, start here:

            • https://doc.qt.io/qt-6/qsortfilterproxymodel.html#filtering

            The filter arguments can be taken from your line edit and the filtering can be updated on every textChanged signal emitted.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            4
            • Y YJMForgive

              @JonB I'm not looking for QLineEdit autocompletion. Instead, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

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

              @YJMForgive said in What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?:

              , I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.

              It's just that this is exactly what QCompleter does, on a list in a popup rather than in a QLisWidget. And you can react on user clicking an item in the filtered list.

              Anyway, accepting that QCompleter is not what you want for whatever reason, it uses the approach of a model and a filter in the same way as described in @Pl45m4's suggestion. So you should follow that.

              1 Reply Last reply
              0
              • Y YJMForgive 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