Qtreeview MouseEvent
- 
Hi @mrjj 
 I have tried to implement mouse press event with event filters using below codeui.usertreeView->installEventFilter( this ); //event filter for qtreeview //event handler bool MainWindow::eventFilter( QObject * watched, QEvent * event ) { QTreeView *view = qobject_cast<QTreeView *>(watched); QMouseEvent *k = (QMouseEvent *)event; if(watched==view) { if((k->button())== Qt::LeftButton) { qDebug()<<"treeview event"; } } return QObject::eventFilter(watched, event); }but no event is getting generated when I click on the treeview. If the same code is used to capture for a QLabel the event is getting generated.What may be the reason? Thanks 
- 
Hi 
 No, that sounds odd. Should also work for
 treeview.
 Is there a reason to use a eventfilter versus
 just implement the MousePress function ?
- 
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 
- 
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 
- 
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.
- 
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 widgetThank 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); }
- 
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 :)
