Hover and Highlight QTable and QTree Items
I was curious, is Qt Stylesheets the only way to get the hover: property?
I wanted to somehow highlight like a whole row in a TableWidget when you hover your mouse over a row. I also wanted to highlight the QTreeWidgetItem when someone hovers their mouse over it, so the background color changes on that item. I figured it out using QPalette for Highlight by clicking, but not my mouseover...
Any ideas? Thanks.
Re: Hover and Highlight QTable and QTree Items
U can use itemAt() function of QTableWidget to get the table widget item under the mouse... then set its background brush....
same u can do with qtreewidgetitems i guess...
and yah... capture the mouse movements on mouseMoveEvent() ....
you have to set setMouseTracking(true) to receive the mouse move events....
hope this will help you.. :)
Re: Hover and Highlight QTable and QTree Items
It kind of helps, I like your itemAt idea, I could do like
itemAt(QCursor::pos()); but then I would need to put this in a loop and multithread right? Where else could I put this? How could I code this so it works?
I think perhaps the best way is to use mouseMoveEvent() and subclass TableWidget and TreeWidget.
Re: Hover and Highlight QTable and QTree Items
So subclassing is only way I take it? I couldn't figure out the itemAt thing.
Re: Hover and Highlight QTable and QTree Items
Enable mouse tracking on the viewport and catch mouse move events either by subclassing and reimplementing the event handler or alternatively with an event filter. The latter approach does not require subclassing.
Re: Hover and Highlight QTable and QTree Items
Where can I learn about "event filters". The subclassing seems time consuming :O.
Re: Hover and Highlight QTable and QTree Items
Re: Hover and Highlight QTable and QTree Items
Ok I got the code, I'm at the part where I do
event->type() == QEvent::MouseMove){
so in here, do I just somehow get an instance of my original class of QTreeWidget and just do:
QTreeWidgetItem* bla = Widget->itemAt(QCursor::pos());
bla->setBackground(0, QBrush(QColor(255,0,0)));
Is that how I am suppose to do it? Or wait do I cast the QObject as a QTreeWidget perhaps?
Re: Hover and Highlight QTable and QTree Items
The position passed within the event might be a bit more reliable:
Code:
// QTreeWidgetItem* prev;
if (event
->type
() == QEvent::MouseMove) {
QMouseEvent* mouseEvent
= static_cast<QMouseEvent
*>
(event
);
if (current && current != prev)
{
if (prev)
prev->setBackground(0, palette().base()); // reset previous
current->setBackground(0, Qt::red); // highlight current
prev = current;
}
}
Re: Hover and Highlight QTable and QTree Items
I think that's exactly what I was looking for thank you. I think I was close.
Re: Hover and Highlight QTable and QTree Items
I made a Filter class,
I used Q_OBJECT.
I used the obj and event as you said.
I even fixed your code, but it seems it works but very slowly. Like I have to whirl my mouse all over USerList and outside of UserList in order to get it to work properly. I mean it works, it even deletes old highlights, but it doesn't highlight the new spot when i move my mouse.
Re: Hover and Highlight QTable and QTree Items
Have you enabled mouse tracking on the viewport and also installed the event filter on the viewport?
Code:
treeWidget->viewport()->setMouseTracking(true);
treeWidget->viewport()->installEventFilter(this);
Edit: is the tree massive, containing huge amount of items in a complex hierarchy? :)
Re: Hover and Highlight QTable and QTree Items
Just stumbled upon this thread, so I decided to include my own 5 cents...
There is another possibility - you can use a custom delegate and reimplement editorEvent() and act upon one (or more) of QEvent::HoverEnter, QEvent::HoverLeave or QEvent::HoverMove. If I remember correctly you have to enable mouse tracking for these events to be delivered, although I'm not sure of that.
Re: Hover and Highlight QTable and QTree Items
wysota, I don't think I wanna delve into making something too complex.
jpn, I was doing:
TreeWidg->setMouseTracking(true);
TreeWidg->installEventFilter(mhf); // mhf = new MouseHoverFilter;
When I changed it to:
TreeWidg->viewport()->setMouseTracking(true);
TreeWidg->viewport()->installEventFilter(mhf); // mhf = new MouseHoverFilter;
Now If I hover over treewidget it crashes the program.
1 Attachment(s)
Re: Hover and Highlight QTable and QTree Items
Quote:
Originally Posted by
VireX
Now If I hover over treewidget it crashes the program.
Improper cast? Insufficient checks? ...see the attached example.
Re: Hover and Highlight QTable and QTree Items
No I did exactly what you posted, except that I didn't subclass my treeWidget.
I made a class called MouseHoverFilter which has the event filter. Then I installed the event filter and setmousetracking on the TreeWidget. It only crashes if i add viewport().
Re: Hover and Highlight QTable and QTree Items
Heres the exact code I used, just try it:
In my main file:
MouseHoverFilter* mhf;
MyList->setMouseTracking(true);
mhf = new MouseHoverFilter;
MyList->installEventFilter(mhf);
Filters.h :
class MouseHoverFilter : public QObject {
Q_OBJECT
public:
MouseHoverFilter(QObject* parent);
QTreeWidgetItem* prev;
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
MouseHoverFilter::MouseHoverFilter(QObject* parent = 0) : QObject(parent) {
prev = 0;
}
bool MouseHoverFilter::eventFilter(QObject *obj, QEvent *event){
if(event->type() == QEvent::MouseMove){
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
QTreeWidget* qtw = static_cast<QTreeWidget*>(obj);
QTreeWidgetItem* current = qtw->itemAt(mouseEvent->pos());
if(current && current != prev && current->childCount() == 0){
if(prev)prev->setBackground(0, qtw->palette().base());
if(prev)prev->setBackground(1, qtw->palette().base());
if(prev)prev->setBackground(2, qtw->palette().base());
current->setBackground(0, QColor(193, 235, 255));
current->setBackground(1, QColor(193, 235, 255));
current->setBackground(2, QColor(193, 235, 255));
prev = current;
}
return true;
} else {
QObject::eventFilter(obj, event);
}
return false;
}
#include "Filters.moc"
1 Attachment(s)
Re: Hover and Highlight QTable and QTree Items
Quote:
Originally Posted by
VireX
wysota, I don't think I wanna delve into making something too complex.
Doesn't seem very complex...
Re: Hover and Highlight QTable and QTree Items
This is the improper cast:
The receiver object is the viewport widget, not the tree widget.
Re: Hover and Highlight QTable and QTree Items
I've tried setting a QTreeWidget inside the Mouse class and delivering the original QTreeWidget to the Mouse Class constructor, but that made no difference, so that isn't the problem, it works this way too.