Why does Qt ColumnView widget crash in this situation?



  • I'm testing an application in Qt that uses a QColumnView widget. The widget has two columns and when an item in the first column (parent) is clicked, it's items (children) are shown in the second column.

    The problem I'm having is this:

    If any parent has more children than there are parents, the application crashes if any of the "excess" children are selected.

    In other words, if there are five parents and each parent has five (or less) children, everything is fine. However, if any parent has six or more children, the application crashes if any child with index greater than the the first five is selected.

    I'm quite sure this is not an error in the code but maybe a bug in Qt itself.

    I hope the above is clear.

    Thanks.


  • Moderators

    Hi, welcome to devnet.

    I just tested it (Qt5.5 MSVC2013) and this case works fine. I'm betting it's a bug in your model.



  • @Chris-Kawa Hi, Thank you for the prompt reply. The bug was in my model as you said, but I think I've found the problem.

    It turns out I needed the following:

    if(!hasIndex(row, column, parent))
        return QModelIndex();
    

    In my "QAbstractItemModel::index()" function. I didn't think the view widget would call "index()" with out of bound row/column combinations since it can use "QAbstractItemModel::rowCount()" and QAbstractItemModel::columnCount()" to get the structure of the model.

    Thanks again for your help.