Important: Please read the Qt Code of Conduct -

About checking an index of a QTableView

  • Hi!

    I´m looking for a way to check if a QTableView's index has information (is not null) or not..

    For example, if I have an empty table and I try to get value from index(0,0), Qt will crash:

    QAbstractItemModel * model = ui->tbl_data->model();
    QModelIndex _cell;
    _cell = model->index(0,0);        //Crash

    I guess is that model has no information, so the _cell is null

    I have tried with Try Catch but still is crashing..

    Any property or way to check if it is null?..

    Now, I know that this has no sense, if there no information in table, why should I check for its indexes ?..
    I want to iterate through the table to count how many rows it has... I can do that, but I need to detect the last used row, check if the next row is null to stop counting...


  • Lifetime Qt Champion

    Did you actually set a model to the view? I would guess no.

  • Thanks for your reply,

    If i put a row value from sqlite and then import it into QTableView, then my code works... No crashes.. The above code reads the index 0,0 successfully, but if there is no row to read, then it crashes..
    I guess i did what you said..

  • Lifetime Qt Champion


    As @Christian-Ehrlicher answer suggests, your QTableView doesn't have any model until you set one on it.

  • My friends... this is what I have.. maybe I´m missing what you both said..:

    QAbstractItemModel * model = ui->tbl_data->model();     
    QModelIndex celda = model->index(0, 0);                 //here crashes if cell has no data (not empty string)   
    QString celdaString =;        

    if I´m missing setting model, how can I do it ?
    Thanks again..

  • Lifetime Qt Champion

    @U7Development said in About checking an index of a QTableView:


    Create your model and call setModel on your tbl_data widget.

Log in to reply