c++qtqt4

QTableWidget: How to find value in specific column


I need to check whether a specific value is in a specific column when using QTableWidget. In my case I need to check the first column whether an ID is already present. If it is, I need the number of the containing row to update this row. Otherwise I would like to add a new row. Is there a solution provided by Qt to check the column?


Solution

  • I assume that you are looking for your value in the first column (that's why the second argument to item(int, int) is 0) and the table's name is myQTableWidget.

    int rows = myQTableWidget->rowCount();
    bool found = false;
    for (int i = 0; i < rows; ++i)
    {
        if (myQTableWidget->item(i, 0)->text() == "Something")
        {
            // we have found our value so we can update row 'i'
            found = true;
            break;
        }
    }
    if (!found)
    {
        // we didn't find our value so we can insert a row
    }