Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView current index and cursor
Qt 6.11 is out! See what's new in the release blog

QTableView current index and cursor

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 769 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lano1106
    wrote on last edited by lano1106
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved