Important: Please read the Qt Code of Conduct -

Searching a QTable widget items using wild cards!

  • IN my qt c++ application I have several items in a qtablewidget. A lineedit along with a button is used by me inorder to search the qtablewidget when a particular word is given to the line edit and the search button is clicked! FOllowing is my code!

    bool found=false;
        QString Line= ui->search->text();
        for(int i=0;i<100;i++){
            if(ui->tableWidget->item(i,0)->text()== Line){
                found = true;
            ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));
            QMessageBox::warning(this, tr("Application Name"), tr("The word you are searching does not exist!") );

    This code works if an exact word in the table widget is given but if I use

    ui->tableWidget->item(i,0)->text()=="%"+ Line+"%"

    it won't work for the wild card scenario so that I can search even part of a word is given! How can I correct this issue?

  • @Lasith

    You can use QAbstractItemModel to solve the problem easily.

    like this:

    #include <QAbstractItemModel>
    #include <QModelIndexList>
    auto    model = ui->tableWidget->model();
    auto    items = model->match(
                model->index(0, 0),
                1, // -1: for all matching items
    if (!items.isEmpty()) {
        ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));

    1. separate QTableWidget into a QTableView and a QStandardItemModel
    2. add a QSortFilterProxyModel as the model of QTableView and set the QStandardItemModel as the QSortFilterProxyModel's sourceModel
    3. call QSortFilterProxyModel::setFilterWildcard or QSortFilterProxyModel::setFilterRegExp to perform the searching

    See Basic Sort/Filter Model Example