Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget Click/Pressed event comes AFTER currentItemChanged event?
Qt 6.12 LTS is out! See what's new in the release blog

QTreeWidget Click/Pressed event comes AFTER currentItemChanged event?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Interesting observation. what you observe is coded in QAbstractItemView::mouseReleaseEvent

      as you can see d->selectionModel->select is called before emit 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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      DiracsbracketD 1 Reply Last reply
      2
      • VRoninV VRonin

        Interesting observation. what you observe is coded in QAbstractItemView::mouseReleaseEvent

        as you can see d->selectionModel->select is called before emit 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

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          you can always subclass QTreeWidget, add a Q_SIGNAL void clickedBefore(); and reimplement virtual void mouseReleaseEvent(QMouseEvent *event){clickedBefore(); QTreeWidget::mouseReleaseEvent(event);}

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          DiracsbracketD 1 Reply Last reply
          2
          • VRoninV VRonin

            you can always subclass QTreeWidget, add a Q_SIGNAL void clickedBefore(); and reimplement virtual void mouseReleaseEvent(QMouseEvent *event){clickedBefore(); QTreeWidget::mouseReleaseEvent(event);}

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

            @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!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt
            • Unsolved