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. Filtering a QComboBox with no completion

Filtering a QComboBox with no completion

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.6k 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.
  • A Offline
    A Offline
    anshah
    wrote on last edited by
    #1

    My objective is to filter a QComboBox while typing. I want the matching entries to display in the QCombobox drop down but I do not want the QComboBox lineedit part to complete what I am typing.

    I think I've been led down the wrong path by the following post:

    https://stackoverflow.com/questions/32142411/filter-with-qcombobox-c

    I am using a QStringListModel, QComboBox, QCompleter, and QSortFilterProxyModel. Here's what I should expect:

    For simplicity let's assume that my QStringList contains:
    { "aaa", "aab", "abb", "bba", "bbb" }

    In the QComboBox if I type "a" the drop down should show the following without completing:
    "aaa"
    "aab"
    "abb"

    If I continue typing "aa" drop down shows:
    "aaa"
    "aab"

    What I'm seeing now is when I type "aa" it auto-completes to "aaa" which I do not want. I don't want completion what so ever.

    Here's my code.

    QStringList strList = { "aaa", "aab", "abb", "bba", "bbb" }
     
    m_pModel = new QStringListModel(strList, this);
    m_pSortFilterProxyModel = new QSortFilterProxyModel(this);
    m_pSortFilterProxyModel->setSourceModel(m_pModel);
    m_pSortFilterProxyModel->setFilterCaseSensitivity(Qt::CaseSensitive);
    m_pComboBox->setModel(m_pSortFilterProxyModel);
    
    m_pCompleter = new QCompleter(m_pModel, this);
    m_pCompleter->setModel(m_pSortFilterProxyModel);
    m_pCompleter->setCompletionMode(QCompleter::PopupCompletion);
    m_pCompleter->setModelSorting(QCompleter::CaseSensitivelySortedModel);
    m_pCompleter->setFilterMode(Qt::MatchStartsWith);
    m_pCompleter->setCaseSensitivity(Qt::CaseSensitive);
    m_pComboBox->setCompleter(m_pCompleter);
    
    m_pComboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_pComboBox->setMaxVisibleItems(20);
    m_pComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    m_pComboBox->setInsertPolicy(QComboBox::NoInsert);
    

    Here's the QComboBox text change slot:

    // Slot to handle QComboBox text change
    void onFilterCombo(const QString &text)
    {
        QStringList items = m_pModel->stringList();
    
        QString item;
        foreach(item, items)
        {
            if (item.indexOf(text) > -1)
            {
                m_pSortFilterProxyModel->setFilterFixedString(text);
                m_pComboBox->showPopup();
                return;
            }
        }
    }
    
    

    What I'm seeing is annoying text completion when I type and occasionally I see a crash when I type.

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

      Hi,

      One thing you can try is to call disconnect on the completer after you set it on your QComboBox.

      Note that the completer already has sorting support. It might be enough for your needs.

      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
      • A Offline
        A Offline
        anshah
        wrote on last edited by
        #3

        I tried disconnecting the QComboBox

        disconnect(m_pComboBox, 0, this, 0);
        

        I also tried setting the completer to zero:

        m_pComboBox->setCompleter(0);
        

        Neither of these seem to work.

        When I type on the QComboBox line edit I don't want to see anything else other than what I type.

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

          I didn't suggest to disconnect the combobox but the completer.

          Another possibility is to create your own subclass of QLineEdit.

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

          A 1 Reply Last reply
          0
          • SGaistS SGaist

            I didn't suggest to disconnect the combobox but the completer.

            Another possibility is to create your own subclass of QLineEdit.

            A Offline
            A Offline
            anshah
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • A Offline
              A Offline
              anshah
              wrote on last edited by
              #6

              This is actually a more critical issue than the completion issue..I am seeing a crash when I do filtering while typing. After looking around this and other forums I see that other Qt users are having the same problem. I have a custom slot for the text change event in my calling class:

              // Slot to handle QComboBox text change
              void MyClass::onFilterCombo(const QString &text)
              {
                  QStringList items = m_pModel->stringList();
              
                  QString item;
                  foreach(item, items)
                  {
                      if (item.indexOf(text) > -1)
                      {
                          m_pSortFilterProxyModel->setFilterFixedString(text);
                          m_pComboBox->showPopup();
                          return;
                      }
                  }
              }
              

              Do you suggest updating the onFilterCombo slot above or creating a custom QSortFilterProxyModel class and reimplementing the slot to handle edit text change to resolve the crash. I'm open to any suggestions as I'm at a bit of an impasse right now.

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

                What stack trace do you get from the crash ?

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

                A 1 Reply Last reply
                0
                • SGaistS SGaist

                  What stack trace do you get from the crash ?

                  A Offline
                  A Offline
                  anshah
                  wrote on last edited by
                  #8

                  @SGaist Unfortunately I didn't collect a crash dump. Once I try again I'll be sure and collect a core dump.

                  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