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. Searching a QTable widget items using wild cards!
QtWS25 Last Chance

Searching a QTable widget items using wild cards!

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtabwidgetitemsearchwildcards
3 Posts 3 Posters 2.2k 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.
  • L Offline
    L Offline
    Lasith
    wrote on 18 Oct 2017, 02:11 last edited by VRonin
    #1

    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;
                break;
            }
        }
        if(found){
            ui->tableWidget->clear();
            ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));
        }
        else{
            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?

    D 1 Reply Last reply 18 Oct 2017, 03:05
    0
    • L Lasith
      18 Oct 2017, 02:11

      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;
                  break;
              }
          }
          if(found){
              ui->tableWidget->clear();
              ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));
          }
          else{
              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?

      D Offline
      D Offline
      Devopia53
      wrote on 18 Oct 2017, 03:05 last edited by
      #2

      @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),
                  Qt::DisplayRole,
                  QVariant::fromValue(QString("*%1*").arg(Line)),
                  1, // -1: for all matching items
                  Qt::MatchWildcard);
      if (!items.isEmpty()) {
          ui->tableWidget->clear();
          ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));
      }
      [...]
      
      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 18 Oct 2017, 07:42 last edited by VRonin
        #3
        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1

        1/3

        18 Oct 2017, 02:11

        • Login

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