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.pngI 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! -
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);
}@
-
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?
-
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 :)
-
Hi, I used the above code in my customTableView and Its changing the mouse cursor but from second time(Not for the first time).
And when its changed to Hand cursor, Its not going back to Arrow cursor for another column.
Looks like the move event is not calculating the pos() every time on mouse move.
I have already installed the event filter and setMouseTracking as true.
Is there anything I am missing? -
Hi and welcome to devnet,
What version of Qt ?
On what platform ? -
Windows, QT 5.10.1
-
Can you test a more recent version of Qt ?
-
@SGaist
I am using 5.12.2. Still facing the same issue. -
Test on macOS with 5.12.2 and it's working as expected. As soon as you pass on the first column the cursor changes and stays the same until a different column is passed on.