Solved Qtreeview MouseEvent
-
Hi @mrjj
Its not working for treeview .There are lot of widgets in my application to segregate the mouse press event i have used event filter.But i am unable to understand why the event filter is not working for treeview if possible can you check at your end .
Thanks
-
@Shiv
Hi
Why not just override
mousePressEvent(QMouseEvent *event)
for treeview?
I dont understand why you try with eventfilter even i completely agree
all events should also be seen there.
I will try eventfilter on tree and see if I get a hint. -
hi
The Tree have a viewport that get these events
so
ui->listView->viewport()->installEventFilter(this);and
bool MainWindow::eventFilter( QObject* watched, QEvent* event ) { qDebug() << "treeview event !1"; QTreeView* view = qobject_cast<QTreeView*>(watched); QMouseEvent* k = static_cast<QMouseEvent*>(event); if(k) { if((k->button()) == Qt::LeftButton) { qDebug() << "treeview button down 3 "; } }
worked for me.
return QObject::eventFilter(watched, event);
} -
Hi @mjrr
Thanks for your valuable time and code.
Its working fine with this code.The event is working fine in QT5.4 but I am currently doing this project in Qt 4.8.4 with VS2008 there some spurious events are getting generated without pressing the mouse.I am unable find the cause for this peculiar behavior. Any solution/idea will be helpful for me to move forward.Thanks
-
@Shiv
Hi, ok that is good to hear.
Very hard to guess at when its other Qt and compiler :)You can use my event debug function
http://paste.ofcode.org/xKREBjyuFwX6FEzTr4rRAPand call it with
qDebug() << "EVENT: " << event->type() << " = " << EventDesc(event->type() );
in the event filter to see if u can guess what is happening. -
@mjrr
I will try and let you know.
Thanks -
Hi @mjrr
I have tried with your EventDesc function the mouse press event automatically getting generating and it happens randomly I know its difficult to replicate this issue at your end i am posting some of the debug message kindly have look.The BTN variable is the mouseevent k->button() value//BTN k->button() value EVENT: 127 = mouse cursor enters a hover widget EVENT: 129 = mouse cursor move inside a hover widget BTN 2 EVENT: 12 = paint widget BTN 2347008 EVENT: 129 = mouse cursor move inside a hover widget BTN 2 EVENT: 129 = mouse cursor move inside a hover widget //BTN 1 // mouse event without mouse action //treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget //BTN 1 //treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget //BTN 1 //treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget BTN 1 treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget //BTN 1 //treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget //BTN 1 //treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget BTN 1 treeview button down 3 EVENT: 129 = mouse cursor move inside a hover widget BTN 2 EVENT: 110 = Tooltip BTN 0 EVENT: 129 = mouse cursor move inside a hover widget BTN 1 treeview button down 3 EVENT: 110 = Tooltip BTN 0 EVENT: 129 = mouse cursor move inside a hover widget BTN 0 EVENT: 129 = mouse cursor move inside a hover widget
Thank you
-
hi
what aboutbool MainWindow::eventFilter( QObject* watched, QEvent* event ) { qDebug() << "treeview event !1"; QTreeView* view = qobject_cast<QTreeView*>(watched); QMouseEvent* k = static_cast<QMouseEvent*>(event); if(k && k->type() == QEvent::MouseButtonPress) { if((k->button()) == Qt::LeftButton) { qDebug() << "treeview button down 3 "; } } return QObject::eventFilter(watched, event); }
-
Hi
Great its working thanks a lot..
Thank you -
super :)
Error was that we use
<QMouseEvent*>
and Hover is also such type , i think.
No idea why k->button() seems to be set for those but
with extra check it works then :)