Solved QTreeWidget Click/Pressed event comes AFTER currentItemChanged event?
-
Hi,
When a row is clicked in a QTreeWidget, why are the Changed() events (itemSelectionChanged and currentItemChanged) issued EARLIER than the Clicked/Pressed events?Doesn't it make more sense to have at least the Pressed event come before the Changed events?
-
Interesting observation. what you observe is coded in
QAbstractItemView::mouseReleaseEvent
as you can see
d->selectionModel->select
is called beforeemit clicked(index);
.I can't think of a particular reason why this is the case but that's indeed how the system works.
such an implementation detail should not be so critical to break your code though -
@VRonin
Well, I was hoping to intercept the Clicked or Pressed event to modify the behavior when the item selection (here the complete row, as I use the tree as a list) is changed by directly clicking it, or changed by key up/down press using a simple flag, but due to the behavior described, I need to do it a bit differently.Thanks for your explanation!
-
you can always subclass
QTreeWidget
, add aQ_SIGNAL void clickedBefore();
and reimplementvirtual void mouseReleaseEvent(QMouseEvent *event){clickedBefore(); QTreeWidget::mouseReleaseEvent(event);}
-
@VRonin
Thanks! I also found out that simply installing an event filter on the the QTreeWidget's viewport does the trick: it gets intercepted before the currentItemChanged() event is issued:ui->scriptTree->viewport()->installEventFilter(this); //need to install to viewport for mouse events ...
and
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (qobject_cast<QWidget*>(obj) == ui->scriptTree->viewport()) { if (event->type() == QEvent::MouseButtonPress) qDebug() << "##### MOUSE PRESSED #####"; } }
And the output is after clicking on an item:
##### MOUSE PRESSED ##### void __cdecl MainWindow::on_scriptTree_currentItemChanged(class QTreeWidgetItem *,class QTreeWidgetItem *)
So in the filter, I can add flag "m_wasClicked" or so to modify the behavior of the currentItemChanged code.
Thanks again!