[SOLVED] - QTableView - change cursor when hovering specific column



  • Hi,

    I would like to set a special cursor when the column 0 is hovered in my QTableView.
    Here is an example of what I would like :
    https://www.dropbox.com/s/88g5l5tu1f7bjgz/hoverCursor.png

    I already use a custom QStyledItemDelegate on my QTableView, I thought it would be possible to set it there but I haven't found anything working yet. I also searched for a way to modify my custom QAbstractTableModel and maybe returning a cursor from the data() function.

    The other way I tried is subclassing QTableView and reimplementing mouseMoveEvent, I can get the cursor this way but it breaks the drag&drop which is coded in my QAbstractTableModel(dropMimeData). I think coding this in the QTableView is bad practice anyway but i'm looking for a way to do it..

    Maybe I could add a custom Widget and set it cursor to a special one in the column I want (in every rows), but this sounds also bad and memory intensive.

    Thanks if anyone can help me, also if you work from Qt, I would like to know if paid support exist for Qt so I could get more help?
    Thank you!


  • Lifetime Qt Champion

    Hi,

    Did you call the base implementation of mouseMouveEvent in your reimplemenation ?



  • Ohh you found my newbie mistake, thanks! Working now, too bad I have to keep a class just for reimplementing a small function but it will do :)

    Code :

    @TableViewInterval::TableViewInterval(QWidget *parent) :
    QTableView(parent)
    {
    this->setMouseTracking(true);

    setStyle(new IntervalViewStyle(style()));
    

    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void TableViewInterval::mouseMoveEvent(QMouseEvent* event) {

    QPoint pos = event->pos();
    QModelIndex index = indexAt(pos);
    
    
    if (index.isValid())
    {
        /// Drag cursor when hoverwing first column
        if (index.column() == 0) {
            setCursor(Qt::PointingHandCursor);
        }
        else {
            setCursor(Qt::ArrowCursor);
        }
    }
    QTableView::mouseMoveEvent(event);
    

    }@


  • Lifetime Qt Champion

    You can also use an eventFilter



  • Thanks SGaist, would it be more efficient with an evenFilter?
    Now this function is triggered a lot of times, it seems to run well enough but not sure if it's the right way..

    How could I use event filter since the only even I have here is the mouse move event?


  • Lifetime Qt Champion

    More ? Don't think so, it should be pretty much the same.

    That's the idea of the evenFilter, you test if it's the event you want on the widget you want and act accordingly :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.