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



  • 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);
    }



  • 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....


  • Lifetime Qt Champion

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.