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 in the QComboBox
Qt 6.11 is out! See what's new in the release blog

Filtering in the QComboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 4.2k Views 2 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
    aliks-os
    wrote on last edited by
    #1

    I try to make a filtering in the popup list of QComboBox. Commonly is ready. But I have a crash during some consistency.
    With the following error:
    ASSERT failure in QVector<T>::remove: "index out of range", file ....\include/QtCore/../../src/corelib/tools/qvector.h, line 449

    How to replicate.

    1. Run application
    2. Type in the combobox 'zzz'
    3. Press Enter
    4. Type any key

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QStringListModel>
    #include <QSortFilterProxyModel>
    #include <QCompleter>
    #include <QComboBox>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QCompleter *completer;
        QSortFilterProxyModel *proxyModel;
        QStringListModel *stringListModel;
    
    private slots:
        void setFilterWildcard(const QString& value);
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QLineEdit>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->comboBox->setEditable(true);
    
    
        QStringList Items;
        Items << "hi" << "bye";
        Items << "master" << "vessel";
        Items << "gun" << "rifle";
        Items << "handgun" << "atom";
        Items << "hello" << "manual";
        Items << "hillo" << "minual";
    
        stringListModel = new QStringListModel();
        stringListModel->setStringList(Items);
    
        proxyModel = new QSortFilterProxyModel;
        proxyModel->setSourceModel(stringListModel);
        proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
        ui->comboBox->setModel(proxyModel);
    
    
        auto completer = new QCompleter(this);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        completer->setModel(proxyModel);
        completer->setCompletionColumn(0);
        completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
        ui->comboBox->setCompleter(completer);
    
        ui->comboBox->setCurrentIndex(-1);
    
        ui->comboBox->lineEdit()->setPlaceholderText("Some text");
        connect(ui->comboBox, SIGNAL(editTextChanged(const QString&)),
                this, SLOT(setFilterWildcard(const QString&)));
    
    }
    
    void MainWindow::setFilterWildcard(const QString &value)
    {
        QStringList items = stringListModel->stringList();
            QString item;
            foreach(item, items) {
                if (item.indexOf(value) > -1) {
                    proxyModel->setFilterFixedString(value);
                    return;
                }
            }
    }
    
    
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    A 1 Reply Last reply
    0
    • A aliks-os

      I try to make a filtering in the popup list of QComboBox. Commonly is ready. But I have a crash during some consistency.
      With the following error:
      ASSERT failure in QVector<T>::remove: "index out of range", file ....\include/QtCore/../../src/corelib/tools/qvector.h, line 449

      How to replicate.

      1. Run application
      2. Type in the combobox 'zzz'
      3. Press Enter
      4. Type any key

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QStringListModel>
      #include <QSortFilterProxyModel>
      #include <QCompleter>
      #include <QComboBox>
      
      
      namespace Ui {
      class MainWindow;
      }
      
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          QCompleter *completer;
          QSortFilterProxyModel *proxyModel;
          QStringListModel *stringListModel;
      
      private slots:
          void setFilterWildcard(const QString& value);
      
      };
      
      #endif // MAINWINDOW_H
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QLineEdit>
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          ui->comboBox->setEditable(true);
      
      
          QStringList Items;
          Items << "hi" << "bye";
          Items << "master" << "vessel";
          Items << "gun" << "rifle";
          Items << "handgun" << "atom";
          Items << "hello" << "manual";
          Items << "hillo" << "minual";
      
          stringListModel = new QStringListModel();
          stringListModel->setStringList(Items);
      
          proxyModel = new QSortFilterProxyModel;
          proxyModel->setSourceModel(stringListModel);
          proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
          ui->comboBox->setModel(proxyModel);
      
      
          auto completer = new QCompleter(this);
          completer->setCaseSensitivity(Qt::CaseInsensitive);
          completer->setModel(proxyModel);
          completer->setCompletionColumn(0);
          completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
          ui->comboBox->setCompleter(completer);
      
          ui->comboBox->setCurrentIndex(-1);
      
          ui->comboBox->lineEdit()->setPlaceholderText("Some text");
          connect(ui->comboBox, SIGNAL(editTextChanged(const QString&)),
                  this, SLOT(setFilterWildcard(const QString&)));
      
      }
      
      void MainWindow::setFilterWildcard(const QString &value)
      {
          QStringList items = stringListModel->stringList();
              QString item;
              foreach(item, items) {
                  if (item.indexOf(value) > -1) {
                      proxyModel->setFilterFixedString(value);
                      return;
                  }
              }
      }
      
      
      
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      A Offline
      A Offline
      aliks-os
      wrote on last edited by
      #2

      @aliks-os
      sorry a lit bit correction

      1. Run application
      2. Type in the combobox 'zzz'
      3. Press Enter
        3a) select 'zzz'
      4. Type any key
      K 1 Reply Last reply
      1
      • A aliks-os

        @aliks-os
        sorry a lit bit correction

        1. Run application
        2. Type in the combobox 'zzz'
        3. Press Enter
          3a) select 'zzz'
        4. Type any key
        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @aliks-os

        Which OS are you on?

        Out of curiosity I have created a new GUI app.
        I had a freshly setup of Ubuntu 32 bit and an install of Qt 5.5.1 32 bit.
        The MainWindow.cpp and include was changed to hold your code. For the MainWindow.ui I simply added a ComboBox with designer and left as is.

        By performing the steps as iniially given and also the corrected sequence, I do not get a crash.

        However, I can provoke a crash with
        1 Run application
        2 Type in the combobox 'zzz'
        3 Press Enter
        4 Mark 'zzz' to overwrite
        5 Type any key

        Vote the answer(s) that helped you to solve your issue(s)

        mrjjM 1 Reply Last reply
        0
        • A Offline
          A Offline
          aliks-os
          wrote on last edited by
          #4

          @koahnig said in Filtering in the QComboBox:

          However, I can provoke a crash with
          1 Run application
          2 Type in the combobox 'zzz'
          3 Press Enter
          4 Mark 'zzz' to overwrite
          5 Type any key

          yes, you do all right. I am exactly get crash as you.
          I am using Win10

          1 Reply Last reply
          0
          • K koahnig

            @aliks-os

            Which OS are you on?

            Out of curiosity I have created a new GUI app.
            I had a freshly setup of Ubuntu 32 bit and an install of Qt 5.5.1 32 bit.
            The MainWindow.cpp and include was changed to hold your code. For the MainWindow.ui I simply added a ComboBox with designer and left as is.

            By performing the steps as iniially given and also the corrected sequence, I do not get a crash.

            However, I can provoke a crash with
            1 Run application
            2 Type in the combobox 'zzz'
            3 Press Enter
            4 Mark 'zzz' to overwrite
            5 Type any key

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @koahnig
            Hi
            I did as you with default project.
            Its does crash here on win 10 +Qt5.9 vs 2015
            alt text

            It seems to come from looping around in the list calling setFilterFixedString

            However if i do.

            void MainWindow::setFilterWildcard(const QString& value) {
              QStringList items = stringListModel->stringList();
             if (items.indexOf(value) > -1 )
                proxyModel->setFilterFixedString(value);
            }
            

            I cannot get it to crash anymore. Maybe it dont do exactly the same.

            K 1 Reply Last reply
            1
            • mrjjM mrjj

              @koahnig
              Hi
              I did as you with default project.
              Its does crash here on win 10 +Qt5.9 vs 2015
              alt text

              It seems to come from looping around in the list calling setFilterFixedString

              However if i do.

              void MainWindow::setFilterWildcard(const QString& value) {
                QStringList items = stringListModel->stringList();
               if (items.indexOf(value) > -1 )
                  proxyModel->setFilterFixedString(value);
              }
              

              I cannot get it to crash anymore. Maybe it dont do exactly the same.

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @mrjj @aliks-os

              Yes,it obviously comes from that loop.

              When applying @mrjj changes it does not crash anymore. However, the functionality is not yet as expected.

              I have tested on Ubuntu 32 bit with Qt 5.5.1 and win10 64 bit Qt 5.4.2 for MinGW.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aliks-os
                wrote on last edited by
                #7

                I replace on the following

                void MainWindow::setFilterWildcard(const QString &value)
                {
                    if (value == "")
                        proxyModel->invalidate();
                    else
                        proxyModel->setFilterWildcard(value);
                }
                
                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