How to iterate through items in a qtableView to determine if a number exists

  • Hi,

    How can I iterate through items in a qtableView to determine if a number in a row matches an external number. In other words I would like to go through the tableView and see if one of the rows contain a certain number.

    How can I do what I'm describing above?


    I'm using QStandardItemModel.

    Thanks a lot.

  • Here is what I came up with that works for what I want to do.

    @void MainWindow::findNumber()
    int row = model->rowCount();
    int col = model->columnCount();
    for (int i = 0; i < row ; ++i)
    for (int j = 0; j < col; j++)
    QVariant content = model->data(model->index(i, j), Qt::DisplayRole);
    if(content == QVariant("5"))
    qDebug()<<"Found a number 5";
    qDebug()<<"There are no number 5s in this table";



  • Lifetime Qt Champion


    You can iterate through all indexes from the row and check each entry for the value you are searching. Maybe not the most efficient but it does the job.

  • What is the most efficient way, the way I did it or the way you are suggesting?

  • Lifetime Qt Champion

    Sorry, i didn't saw your update after sending my answer which is the same as yours but you where only talking about a row not searching the entire model.

    You could try using QtConcurrent to speed things up but that would complicate your code a bit

  • Thanks a lot for your help!

Log in to reply

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