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. QListWidget selection, preventing selection and revert to previous?
Forum Updated to NodeBB v4.3 + New Features

QListWidget selection, preventing selection and revert to previous?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.3k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 20 Jan 2022, 10:34 last edited by SPlatten
    #1

    When the currentItemChanged signal occurs, in the slot I look for changes in other related controls and as a result I get either true to allow the change or false to prevent the change.

    The question is how do I instruct QListWidget to revert back to the previous selection?

    In the slot I have the parameters:

    QListWidgetItem* pobjCurrent, QListWidgetItem* pobjPrevious
    

    Based on the result I've tried:

    if ( blnOkToChange != true ) {
        setCurrentItem(pobjPrevious);
        return;
    }
    

    This doesn't work.

    Edit, I've also tried:

    if ( blnOkToChange != true ) {
        pobjCurrent->setSelected(false);
        pobjPrevious->setSelected(true);
        return;
    }
    

    This doesn't work at all.

    Kind Regards,
    Sy

    J 1 Reply Last reply 20 Jan 2022, 12:44
    0
    • J JonB
      20 Jan 2022, 13:34

      @SPlatten
      I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set a QListWidget's selection you must do something on the QListWidget, not on the items? Have you even tested your setSelected() call in any circumstance?

      S Offline
      S Offline
      SPlatten
      wrote on 20 Jan 2022, 13:42 last edited by SPlatten
      #9

      @JonB , so far all efforts at trying to prevent the selection have failed.

      if ( blnOkToChange != true ) {
      //Select previous selection and exit
          QListWidgetItem* pobjItem(pobjPrevious);
          QTimer::singleShot(100, this, [pobjItem]() {
              pobjItem->setSelected(true);
          });
          return;
      }
      

      Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.

      Kind Regards,
      Sy

      S 1 Reply Last reply 21 Jan 2022, 07:57
      0
      • S SPlatten
        20 Jan 2022, 10:34

        When the currentItemChanged signal occurs, in the slot I look for changes in other related controls and as a result I get either true to allow the change or false to prevent the change.

        The question is how do I instruct QListWidget to revert back to the previous selection?

        In the slot I have the parameters:

        QListWidgetItem* pobjCurrent, QListWidgetItem* pobjPrevious
        

        Based on the result I've tried:

        if ( blnOkToChange != true ) {
            setCurrentItem(pobjPrevious);
            return;
        }
        

        This doesn't work.

        Edit, I've also tried:

        if ( blnOkToChange != true ) {
            pobjCurrent->setSelected(false);
            pobjPrevious->setSelected(true);
            return;
        }
        

        This doesn't work at all.

        J Offline
        J Offline
        JonB
        wrote on 20 Jan 2022, 12:44 last edited by JonB
        #2

        @SPlatten
        currentItemChanged is only a signal to say it has happened, after the change has been made. It does not offer "reject".

        I would have thought your code would work, so long as you are correctly checking for recursive call, because I think you will get the signal again for the change back. You may have to set both current item & selected item.

        If it does not work, presumably either subclass QListWidget to handle, or install event filter on the it and handle check there, or try your revert-select on a timer immediately afterward to allow the current signal-slot to exit as-is. Not very elegant, but might suffice.

        S 1 Reply Last reply 20 Jan 2022, 13:21
        1
        • J JonB
          20 Jan 2022, 12:44

          @SPlatten
          currentItemChanged is only a signal to say it has happened, after the change has been made. It does not offer "reject".

          I would have thought your code would work, so long as you are correctly checking for recursive call, because I think you will get the signal again for the change back. You may have to set both current item & selected item.

          If it does not work, presumably either subclass QListWidget to handle, or install event filter on the it and handle check there, or try your revert-select on a timer immediately afterward to allow the current signal-slot to exit as-is. Not very elegant, but might suffice.

          S Offline
          S Offline
          SPlatten
          wrote on 20 Jan 2022, 13:21 last edited by
          #3

          @JonB, thank you, I added this to the top of the slot of handle recursion:

              if ( pobjCurrent == pobjPrevious ) {
              //Ignore!
                  return;
              }
          

          It still doesn't work. I get a prompt, if I answer no I can see it drops into the logic:

              if ( blnOkToChange != true ) {
              //Select previous selection and exit
                  setCurrentItem(pobjPrevious);
                  return;
              }
          

          But the display still shows the new selection.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 20 Jan 2022, 13:25 last edited by
            #4

            setCurrentItem() does not set a selection but the current index. current index has nothing to do with the current selection.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply 20 Jan 2022, 13:26
            2
            • C Christian Ehrlicher
              20 Jan 2022, 13:25

              setCurrentItem() does not set a selection but the current index. current index has nothing to do with the current selection.

              S Offline
              S Offline
              SPlatten
              wrote on 20 Jan 2022, 13:26 last edited by
              #5

              @Christian-Ehrlicher , thank you, then how to ?

              Kind Regards,
              Sy

              J 1 Reply Last reply 20 Jan 2022, 13:30
              0
              • S SPlatten
                20 Jan 2022, 13:26

                @Christian-Ehrlicher , thank you, then how to ?

                J Offline
                J Offline
                JonB
                wrote on 20 Jan 2022, 13:30 last edited by JonB
                #6

                @SPlatten
                Like I said earlier, you probably want to set both current item and selection for your purpose? You have to decide.

                S 1 Reply Last reply 20 Jan 2022, 13:31
                0
                • J JonB
                  20 Jan 2022, 13:30

                  @SPlatten
                  Like I said earlier, you probably want to set both current item and selection for your purpose? You have to decide.

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 20 Jan 2022, 13:31 last edited by
                  #7

                  @JonB , I've just tried:

                      if ( blnOkToChange != true ) {
                      //Select previous selection and exit
                          QTimer::singleShot(50, [pobjCurrent, pobjPrevious]() {
                              pobjCurrent->setSelected(false);
                              pobjPrevious->setSelected(true);
                          });
                          return;
                      }
                  

                  Still not right, the list selection doesn't revert from the new selection.

                  Kind Regards,
                  Sy

                  J 1 Reply Last reply 20 Jan 2022, 13:34
                  0
                  • S SPlatten
                    20 Jan 2022, 13:31

                    @JonB , I've just tried:

                        if ( blnOkToChange != true ) {
                        //Select previous selection and exit
                            QTimer::singleShot(50, [pobjCurrent, pobjPrevious]() {
                                pobjCurrent->setSelected(false);
                                pobjPrevious->setSelected(true);
                            });
                            return;
                        }
                    

                    Still not right, the list selection doesn't revert from the new selection.

                    J Offline
                    J Offline
                    JonB
                    wrote on 20 Jan 2022, 13:34 last edited by
                    #8

                    @SPlatten
                    I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set a QListWidget's selection you must do something on the QListWidget, not on the items? Have you even tested your setSelected() call in any circumstance?

                    S 1 Reply Last reply 20 Jan 2022, 13:42
                    0
                    • J JonB
                      20 Jan 2022, 13:34

                      @SPlatten
                      I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set a QListWidget's selection you must do something on the QListWidget, not on the items? Have you even tested your setSelected() call in any circumstance?

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 20 Jan 2022, 13:42 last edited by SPlatten
                      #9

                      @JonB , so far all efforts at trying to prevent the selection have failed.

                      if ( blnOkToChange != true ) {
                      //Select previous selection and exit
                          QListWidgetItem* pobjItem(pobjPrevious);
                          QTimer::singleShot(100, this, [pobjItem]() {
                              pobjItem->setSelected(true);
                          });
                          return;
                      }
                      

                      Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.

                      Kind Regards,
                      Sy

                      S 1 Reply Last reply 21 Jan 2022, 07:57
                      0
                      • S SPlatten
                        20 Jan 2022, 13:42

                        @JonB , so far all efforts at trying to prevent the selection have failed.

                        if ( blnOkToChange != true ) {
                        //Select previous selection and exit
                            QListWidgetItem* pobjItem(pobjPrevious);
                            QTimer::singleShot(100, this, [pobjItem]() {
                                pobjItem->setSelected(true);
                            });
                            return;
                        }
                        

                        Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on 21 Jan 2022, 07:57 last edited by
                        #10

                        @SPlatten said in QListWidget selection, preventing selection and revert to previous?:

                        Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.

                        You don't have to fully disable the list widget (which is when it is grayed out), but just blockSignals for the list widget.

                        If I'm not mistaken I always go through the model to tweak the selection. This is the only way I have found so far to work properly.

                        1 Reply Last reply
                        1

                        1/10

                        20 Jan 2022, 10:34

                        • Login

                        • Login or register to search.
                        1 out of 10
                        • First post
                          1/10
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved