Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableView current index and cursor



  • I didn't find much info about a view current index and cursor in the Qt model/view framework doc.

    I wrote 2 derived classes from QTableView and QAbstractTableModel to work together.

    The view is essentially simply to display the model data. Therefore my model implement the following method:

    Qt::ItemFlags BaseKrakenModel::flags(const QModelIndex &index) const
    {
        Qt::ItemFlags f = QAbstractTableModel::flags(index);
        if (index.isValid())
            f &= ~(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
        return f;
    }
    

    On the view side, I want 2 functionalities:

    • display a context menu by right clicking on one of the table rows.
    • double click on a row

    By looking in the Qt framework code, I did discover that in order to have my slot connected to QAbstractItemView::doubleClicked() called, I need the clicked item to be enabled (AFAIK, subtlety not clearly stated in the API doc BTW!).

    So I did changed my model code to put back Qt::ItemIsEnabled in the return value. However by doing so, I did end up with the annoying view cursor that I do not want!

    Simply by searching in the documentation, I couldn't find out the correct way to have my model items enabled so that I can double click them and NOT having the view cursor show up. There is a comment in the function QAbstractItemView::setSelectionModel() that say that passing NULL to this function isn't a good idea (enforced with a QASSERT() macro...).

    So I ended up doing the following in my view class:

    QModelIndex SimpleGrid::moveCursor(QAbstractItemView::CursorAction /*cursorAction*/, Qt::KeyboardModifiers /*modifiers*/)
    {
        return QModelIndex();
    }
    
    void SimpleGrid::currentChanged(const QModelIndex &current, const QModelIndex &/*previous*/)
    {
        if (current.isValid())
            setCurrentIndex(QModelIndex());
    }
    

    and it seems to work like a charm. My question to the forum. Is this the best approach to eliminate the cursor?


Log in to reply