Qtreeview MouseEvent
-
Dear All
I have implemented a QTreeview with QAbstractItemModel now I want to emit a signal whenever mouse is clicked with LeftButton on the Items of the QTreeview.I have tried with 'clicked' and 'selectionChanged' signal but the problem is it responds for RightButton too since I have to use a context menu for RightClick . Is there any way to emit a signal from QTreeview other than QEvent eventfilter or QMouseEvent.
Thanks -
hi
Im not 100% sure what is not working except right click should not
give "selectionChanged" for your use case ?If you already have a QTreeview child with a
mousePressEvent(QMouseEvent *event)
implemented you could just make your own signal?treeview..
public signals:
void LeftClicked( any param u want );then in mousePressEvent
if (event->button() == Qt::LeftButton) {
emit LeftClicked( the actual paramters);
}Then from outside u just use your new signal.
Note: for a signal, you do not implement anything in the cpp. its
just a definition in the class. No body. -
@Shiv
well yes, you could also emit clicked.do you use
http://doc.qt.io/qt-5/qcontextmenuevent.html#detailsto make the context menu ?
-
Ok.
But catching MousePress and check
if left button then emit
new signal for left selection
and if not left button,
then simply call base class MousePress
Is not acceptable for you? -
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
-
@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. -
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); }