Search code examples
qtqstandarditemmodel

Apply regex to all items of a QStandardItemModel


  1. I use a QStandardItemModel with a QTtableview to hold regexes
  2. I also use a *QStandardItemModel with a QTableview to which I want to apply all regexes from QTableview described in 1)

What is the best way to do that please ?


Solution

  • If you only want to browse your model and apply a regular expression, you can use QAbstractItemModel::rowCount() and QAbstractItemModel::columnCount() and two loops to get each item in your model with QAbstractItemModel::item():

    for ( int col = 0; col < model.columnCount(); ++col ) 
    {
      for( int row = 0; row < model.rowCount(); ++row ) 
      {
        item = model.item( row, col );
        doSomething( item->text() );        
      }
    }
    

    If you want to filter your model to display only the items that match your regexp, you should use a QSortFilterModel.

    Edit for small syntaxic typo and indent.