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. [SOLVED] search/filter inside Combobox QCombobox
QtWS25 Last Chance

[SOLVED] search/filter inside Combobox QCombobox

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 21.3k Views
  • 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
    arsinte_andrei
    wrote on 7 May 2014, 18:00 last edited by
    #1

    Hi,
    I'm using a Qcombobox and I'm trying to implement a search/filter on it.
    as model I'm using a SqlQuerryModel.

    E.g. List items:
    Albert Einstand
    Andrew Robert
    Paul Manson
    Michael Jordan

    ok now in in the combobox I'll start typing
    "a" - I get the hole list
    "an"- =>
    Albert Einstand
    Andrew Robert
    Paul Manson
    "and"- =>
    Albert Einstand
    Andrew Robert
    "andr"- => Andrew Robert
    please note that my desire is that the refresh of the list to be done automatically without any update button pressed. on_combobox_currentTextChanged event.
    also the list have to remain open as the user is typing and else to work like a combobox with this new features

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 May 2014, 20:08 last edited by
      #2

      Hi,

      Maybe "this":http://qt-project.org/doc/qt-5/qcompleter.html will do

      Hope it helps

      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
        arsinte_andrei
        wrote on 7 May 2014, 20:31 last edited by
        #3

        yep i've done this

        @QCompleter *mycompletear = new QCompleter(this);
        mycompletear->setCaseSensitivity(Qt::CaseInsensitive);
        mycompletear->setModel(proxyModel);
        mycompletear->setCompletionColumn(1);
        mycompletear->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
        ui->comp_comb->setCompleter(mycompletear);@

        but unfortunately is showing an empty combobox if i type nothing in it

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 May 2014, 21:49 last edited by
          #4

          What should it show ?

          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
            arsinte_andrei
            wrote on 8 May 2014, 07:34 last edited by
            #5

            so.. when the form load and the user click on dropdown (combobox button) it have to show the full list f about 50 items (but only 6 or 8 with a scroolbar on the right ) and when the user start typing something then the list have to change accordingly.
            but now when the user click is showing an empty list and if is starting to type something for example just an "a" it will already complete the first item - when it have to show only the items and the user to be able to select it from the list

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 May 2014, 20:38 last edited by
              #6

              Do you use the same model for both the combo box and the completer ?

              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
                arsinte_andrei
                wrote on 9 May 2014, 08:02 last edited by
                #7

                yes I use the same proxy models... is here the trick? to use different models? I'll try this and I will let you know the result I also have a model and a proxymodel. i'll see the difference and let you know. thanks for this

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  arsinte_andrei
                  wrote on 9 May 2014, 11:22 last edited by
                  #8

                  Voila the "solution" hope will help other in the future. a very nice future for a combo to to have it.
                  this is the cpp file
                  @
                  model = new QSqlQueryModel;
                  model->setQuery("SELECT * FROM company_tbl");

                  proxyModel = new QSortFilterProxyModel(this);
                  proxyModel->setSourceModel(model);
                  proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
                  proxyModel->setFilterKeyColumn(1);

                  proxyModel1 = new QSortFilterProxyModel(this);
                  proxyModel1->setSourceModel(model);
                  proxyModel1->setFilterCaseSensitivity(Qt::CaseInsensitive);
                  proxyModel1->setFilterKeyColumn(1);

                  ui->comp_comb->setModel(proxyModel1);
                  ui->comp_comb->setModelColumn(1);

                  QCompleter *mycompletear = new QCompleter(this);
                  mycompletear->setCaseSensitivity(Qt::CaseInsensitive);
                  mycompletear->setModel(proxyModel);
                  mycompletear->setCompletionColumn(1);
                  mycompletear->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
                  ui->comp_comb->setCompleter(mycompletear);

                  @

                  and also this is the

                  @void MainWindow::on_comp_comb_currentTextChanged(const QString &arg1) {
                  if (!arg1.isEmpty()) {
                  proxyModel->setFilterFixedString(arg1);
                  //the following line will do an empty combobox - this is not a fix.. but a temporary thing just to work we will have just the compleater filtered
                  //proxyModel1->setFilterFixedString(arg1);
                  }
                  qDebug() << arg1;
                  }@

                  and this is the h file under private:
                  @
                  private:
                  QSqlQueryModel *model;
                  QSortFilterProxyModel *proxyModel;
                  QSortFilterProxyModel *proxyModel1;@

                  hope this will help others that need to filter a combobox after any letter from the word
                  Have a good day...
                  PS - if you have a better one please share it with me...
                  Many thanks

                  1 Reply Last reply
                  3
                  • N Offline
                    N Offline
                    nicktrandafil
                    wrote on 17 Oct 2015, 16:37 last edited by
                    #9

                    I have a QTableView with a QSqlRelationalTableModel.
                    I obtained model editing with search/filter support this way:

                    class PrettyComboBox : public QSqlRelationalDelegate
                    {
                    public:
                      explicit PrettyComboBox(QObject *parent = nullptr);
                    
                      QWidget* createEditor(  QWidget *parent, const QStyleOptionViewItem &option
                                            , const QModelIndex &index) const override;
                    };
                    
                    PrettyComboBox::PrettyComboBox(QObject *parent)
                      : QSqlRelationalDelegate{parent} {}
                    
                    
                    QWidget* PrettyComboBox::createEditor(  QWidget *parent
                                                          , const QStyleOptionViewItem &option
                                                          , const QModelIndex &index) const
                    {
                      auto w = QSqlRelationalDelegate::createEditor(parent, option, index);
                    
                      if (auto c = dynamic_cast<QComboBox*>(w)) {
                    
                        c->setEditable(true);
                        c->setInsertPolicy(QComboBox::NoInsert);
                    
                        auto proxy = new QSortFilterProxyModel(w);
                        proxy->setSourceModel(c->model());
                        proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
                        proxy->setFilterKeyColumn(c->modelColumn());
                    
                        auto completer = new QCompleter(w);
                        completer->setCaseSensitivity(Qt::CaseInsensitive);
                        completer->setModel(proxy);
                        completer->setCompletionColumn(c->modelColumn());
                        completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
                        c->setCompleter(completer);
                    
                        connect(  c, &QComboBox::editTextChanged
                                , [proxy](const QString &txt) {
                                    proxy->setFilterFixedString(txt); });
                      }
                    
                      return w;
                    }
                    
                    ...and somewhere
                    table_view->setItemDelegate(new PrettyComboBox(table_view));
                    
                    1 Reply Last reply
                    1

                    • Login

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