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. How to filter with QSortFilterProxyModel that only matches input string from left side
Forum Updated to NodeBB v4.3 + New Features

How to filter with QSortFilterProxyModel that only matches input string from left side

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.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.
  • G Offline
    G Offline
    guru007
    wrote on last edited by
    #1

    I am using QStandardItem model which is having two columns with train numbers and train names.
    Train No. Train name
    10 xy
    1011 abc
    1022 abcd
    401011 xyz
    66044 xyza
    66045 gg

    I am filtering this based on user input.But issue is proxy model is filtering matching words which contains input words at any position.
    Means If I enter input 10 then ideally it should display only those values that start with 10 like 10,1011,1022 but problem is it is showing all values which contains the input expression.
    like 10,1011,1022,401011
    How Can I achieve that?
    Here is source :
    connect(ui->lineEdit,SIGNAL(textChanged(QString)),thisSLOT(lineedit_filtering(QSring)));
    connect(this,SIGNAL(lineedit_filter(QString)),proxyModel,SLOT(setFilterFixedString(QString)));
    proxyModel = new QSortFilterProxyModel;
    model= new QStandardItemModel(0,0);
    header_font.setPointSize(20);
    header_font.setFamily("Garuda");
    master_trains_model->setTable("tbl_TrainNumber");
    slave_trains_model->setTable("tbl_slave_route");
    slave_trains_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    slave_trains_model->select();
    slave_trains_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    master_trains_model->select();

    labels.append("Train_no");
    labels.append("Train_name");
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->horizontalHeader()->setFont(header_font);
    QList <QStandardItem *> rowData;
    QList <QStandardItem *> ColumnData;
    
    
    //////////////////// Inserting Master Train numbers ///////////////////////////
    for(loop_count=0;loop_count<master_trains_model->rowCount();loop_count++)
    {
        ColumnData << new QStandardItem(master_trains_model->data(master_trains_model->index(loop_count,TABLE_TRAIN_NUMBER::TRAIN_NO)).toString());
    }
    //---------------------------------------------------------------------------//
    
    //////////////////// Inserting Slave Train numbers ////////////////////////////
    
    for(loop_count=0;loop_count<slave_trains_model->rowCount();loop_count++)
    {
        ColumnData << new QStandardItem(slave_trains_model->data(slave_trains_model->index(loop_count,TABLE_TRAIN_NUMBER::TRAIN_NO)).toString());
    }
    model->insertColumn(0,ColumnData);
    ColumnData.clear();
    

    ** proxyModel->setSourceModel(model);**
    ui->tableView->setModel(proxyModel);
    void route_selection::lineedit_filtering(QString value)
    {
    qDebug() << "VALUE------------- " << value;
    proxyModel->setFilterKeyColumn(0);
    proxyModel->setFilterFixedString(value);
    emit this->lineedit_filter(value);
    }

    G 1 Reply Last reply
    0
    • G guru007

      I am using QStandardItem model which is having two columns with train numbers and train names.
      Train No. Train name
      10 xy
      1011 abc
      1022 abcd
      401011 xyz
      66044 xyza
      66045 gg

      I am filtering this based on user input.But issue is proxy model is filtering matching words which contains input words at any position.
      Means If I enter input 10 then ideally it should display only those values that start with 10 like 10,1011,1022 but problem is it is showing all values which contains the input expression.
      like 10,1011,1022,401011
      How Can I achieve that?
      Here is source :
      connect(ui->lineEdit,SIGNAL(textChanged(QString)),thisSLOT(lineedit_filtering(QSring)));
      connect(this,SIGNAL(lineedit_filter(QString)),proxyModel,SLOT(setFilterFixedString(QString)));
      proxyModel = new QSortFilterProxyModel;
      model= new QStandardItemModel(0,0);
      header_font.setPointSize(20);
      header_font.setFamily("Garuda");
      master_trains_model->setTable("tbl_TrainNumber");
      slave_trains_model->setTable("tbl_slave_route");
      slave_trains_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
      slave_trains_model->select();
      slave_trains_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
      master_trains_model->select();

      labels.append("Train_no");
      labels.append("Train_name");
      ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
      ui->tableView->horizontalHeader()->setFont(header_font);
      QList <QStandardItem *> rowData;
      QList <QStandardItem *> ColumnData;
      
      
      //////////////////// Inserting Master Train numbers ///////////////////////////
      for(loop_count=0;loop_count<master_trains_model->rowCount();loop_count++)
      {
          ColumnData << new QStandardItem(master_trains_model->data(master_trains_model->index(loop_count,TABLE_TRAIN_NUMBER::TRAIN_NO)).toString());
      }
      //---------------------------------------------------------------------------//
      
      //////////////////// Inserting Slave Train numbers ////////////////////////////
      
      for(loop_count=0;loop_count<slave_trains_model->rowCount();loop_count++)
      {
          ColumnData << new QStandardItem(slave_trains_model->data(slave_trains_model->index(loop_count,TABLE_TRAIN_NUMBER::TRAIN_NO)).toString());
      }
      model->insertColumn(0,ColumnData);
      ColumnData.clear();
      

      ** proxyModel->setSourceModel(model);**
      ui->tableView->setModel(proxyModel);
      void route_selection::lineedit_filtering(QString value)
      {
      qDebug() << "VALUE------------- " << value;
      proxyModel->setFilterKeyColumn(0);
      proxyModel->setFilterFixedString(value);
      emit this->lineedit_filter(value);
      }

      G Offline
      G Offline
      guru007
      wrote on last edited by
      #2

      I think I can use QregExp to find matches which starts from input numbers. How Can I construct that QregExp ...
      like INPUT = 1
      QRegExp output ----> 1*******
      means output starting with 1 but follows any numbers afterward.Then I put it in
      proxyModel->setFilterRegExp(QRegExp);
      Please help me setting the RegExp....

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

        Hi,

        The start of line regexp char is a $. You can then happen what the user inserted in the line edit.

        You should take a look at the RegExp example, it's a little helper tool to build regular expressions for QRegExp

        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

        • Login

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