Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?

How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.2k Views 2 Watching
  • 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.
  • Guy GizmoG Offline
    Guy GizmoG Offline
    Guy Gizmo
    wrote on last edited by
    #1

    I need to get a signal from a QTreeWidget that its selection has been changed by the user. This includes the user clicking on an item in the widget, or using the keyboard to change the selection.

    QTreeWidget does provide the signal itemSelectionChanged but it fires anytime the selection changes for any reason, including it being changed programmatically, so it doesn't fulfill my requirements.

    How can I accomplish this?

    JonBJ 1 Reply Last reply
    0
    • Guy GizmoG Guy Gizmo

      I need to get a signal from a QTreeWidget that its selection has been changed by the user. This includes the user clicking on an item in the widget, or using the keyboard to change the selection.

      QTreeWidget does provide the signal itemSelectionChanged but it fires anytime the selection changes for any reason, including it being changed programmatically, so it doesn't fulfill my requirements.

      How can I accomplish this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Guy-Gizmo
      Qt does not offer to distinguish for itemSelectionChanged().

      • Create some state variable during clicks/key presses in a QTreeWidget subclass so that you know it has come from input. Maybe QTreeWidget::itemPressed() helps. Not desirable.

      • Could you use eventFilter() to recognise comes from input? Probably same undesirability.

      Guy GizmoG 1 Reply Last reply
      0
      • JonBJ JonB

        @Guy-Gizmo
        Qt does not offer to distinguish for itemSelectionChanged().

        • Create some state variable during clicks/key presses in a QTreeWidget subclass so that you know it has come from input. Maybe QTreeWidget::itemPressed() helps. Not desirable.

        • Could you use eventFilter() to recognise comes from input? Probably same undesirability.

        Guy GizmoG Offline
        Guy GizmoG Offline
        Guy Gizmo
        wrote on last edited by
        #3

        @JonB I thought about using mouse and key events to set a flag, but I worry that this will catch situations that I don't want it to. Here's one potential example: the user presses Control+Z / Command+Z while the text field is focused. This will trigger code that will programmatically change its selection if that's the last undoable action, but it shouldn't cause this new signal to fire.

        I can try to be careful and catch all situations that are triggered by user action, but the truth is that only QListWidget really knows when it's changing the selection via user action as opposed to something else! But I'm trying to find a solution that does not involve basically reimplementing QTreeWidget from scratch.

        JonBJ 1 Reply Last reply
        0
        • Guy GizmoG Guy Gizmo

          @JonB I thought about using mouse and key events to set a flag, but I worry that this will catch situations that I don't want it to. Here's one potential example: the user presses Control+Z / Command+Z while the text field is focused. This will trigger code that will programmatically change its selection if that's the last undoable action, but it shouldn't cause this new signal to fire.

          I can try to be careful and catch all situations that are triggered by user action, but the truth is that only QListWidget really knows when it's changing the selection via user action as opposed to something else! But I'm trying to find a solution that does not involve basically reimplementing QTreeWidget from scratch.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Guy-Gizmo
          I do not disagree with you, but I do not think Qt code offers you a way to distinguish a "user initiated" selection change versus a programmatic one. I think it will be tricky to do, trying to recognise/cover all input cases.

          If I had to do this --- and if no one else here offers an alternative --- I would have a look through the Qt source code to see if I could spot where the signal is emitted for programmatic versus user invocation and whether I could lever that. If you say QListWidget gives you what you want but not QTreeWidget, is there a useful difference you can spot in the code?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Can you explain what differences comes from a user initiated selection and a programmatically made ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Guy GizmoG 1 Reply Last reply
            0
            • D Offline
              D Offline
              DerReisende
              wrote on last edited by
              #6

              I may be completely wrong but wouldn't QSignalBlocker - used in all programmatic changes - be the way to prevent the emission of unwanted signals?

              Guy GizmoG 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Can you explain what differences comes from a user initiated selection and a programmatically made ?

                Guy GizmoG Offline
                Guy GizmoG Offline
                Guy Gizmo
                wrote on last edited by
                #7

                @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                Hi,

                Can you explain what differences comes from a user initiated selection and a programmatically made ?

                A user initiated change would come from the user clicking items in the QTreeWidget, or using keystrokes to select items. The change in selection would be handled internally by QTreeWidget.

                A programmatic change would be my own code calling a function like QTreeWidget::clearSelection or QTreeWidgetItem::setSelected.

                It is analogous to two signals in QLineEdit, where QLineEdit::textChanged fires any time its text changes for any reason, and QLineEdit::textEdited fires any time the text is changed due to user action specifically. It's too bad that more widget classes do not include signals like this that explicitly fire only in response to user-driven changes.

                SGaistS 1 Reply Last reply
                0
                • D DerReisende

                  I may be completely wrong but wouldn't QSignalBlocker - used in all programmatic changes - be the way to prevent the emission of unwanted signals?

                  Guy GizmoG Offline
                  Guy GizmoG Offline
                  Guy Gizmo
                  wrote on last edited by
                  #8

                  @DerReisende said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                  I may be completely wrong but wouldn't QSignalBlocker - used in all programmatic changes - be the way to prevent the emission of unwanted signals?

                  This is my current strategy, which works for the time being. But I think it's brittle: I'm not aware of absolutely every function I can call on QTreeWidget that might end up changing its selection somehow, and so this makes it easy for me to write bugs without realizing it.

                  1 Reply Last reply
                  0
                  • Guy GizmoG Guy Gizmo

                    @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                    Hi,

                    Can you explain what differences comes from a user initiated selection and a programmatically made ?

                    A user initiated change would come from the user clicking items in the QTreeWidget, or using keystrokes to select items. The change in selection would be handled internally by QTreeWidget.

                    A programmatic change would be my own code calling a function like QTreeWidget::clearSelection or QTreeWidgetItem::setSelected.

                    It is analogous to two signals in QLineEdit, where QLineEdit::textChanged fires any time its text changes for any reason, and QLineEdit::textEdited fires any time the text is changed due to user action specifically. It's too bad that more widget classes do not include signals like this that explicitly fire only in response to user-driven changes.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Guy-Gizmo said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                    @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                    Hi,

                    Can you explain what differences comes from a user initiated selection and a programmatically made ?

                    A user initiated change would come from the user clicking items in the QTreeWidget, or using keystrokes to select items. The change in selection would be handled internally by QTreeWidget.

                    A programmatic change would be my own code calling a function like QTreeWidget::clearSelection or QTreeWidgetItem::setSelected.

                    It is analogous to two signals in QLineEdit, where QLineEdit::textChanged fires any time its text changes for any reason, and QLineEdit::textEdited fires any time the text is changed due to user action specifically. It's too bad that more widget classes do not include signals like this that explicitly fire only in response to user-driven changes.

                    I meant with regard to what should happen in your application when the selection comes from the user and when it's done programmatically.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Guy GizmoG 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @Guy-Gizmo said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                      @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                      Hi,

                      Can you explain what differences comes from a user initiated selection and a programmatically made ?

                      A user initiated change would come from the user clicking items in the QTreeWidget, or using keystrokes to select items. The change in selection would be handled internally by QTreeWidget.

                      A programmatic change would be my own code calling a function like QTreeWidget::clearSelection or QTreeWidgetItem::setSelected.

                      It is analogous to two signals in QLineEdit, where QLineEdit::textChanged fires any time its text changes for any reason, and QLineEdit::textEdited fires any time the text is changed due to user action specifically. It's too bad that more widget classes do not include signals like this that explicitly fire only in response to user-driven changes.

                      I meant with regard to what should happen in your application when the selection comes from the user and when it's done programmatically.

                      Guy GizmoG Offline
                      Guy GizmoG Offline
                      Guy Gizmo
                      wrote on last edited by
                      #10

                      @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                      I meant with regard to what should happen in your application when the selection comes from the user and when it's done programmatically.

                      I'm using the tree view to allow the user to select an item from a big categorized list, and I have a custom undo system that needs to make entries in the undo history corresponding to when the user selects something in this list, but avoid making entries of non-user changes.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Then it seems sensible to block the selection model's signals when you undo things.

                        Out of curiosity, are you using Qt's undo framework ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        Guy GizmoG 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Then it seems sensible to block the selection model's signals when you undo things.

                          Out of curiosity, are you using Qt's undo framework ?

                          Guy GizmoG Offline
                          Guy GizmoG Offline
                          Guy Gizmo
                          wrote on last edited by
                          #12

                          @SGaist said in How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?:

                          Out of curiosity, are you using Qt's undo framework ?

                          Not this time. I ended up needing a lot of custom behavior so I made my own.

                          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 Extensions
                          • Unsolved