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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

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

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on 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
      0
      • SPlattenS SPlatten

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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.

        SPlattenS 1 Reply Last reply
        1
        • JonBJ JonB

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

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            SPlattenS 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

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

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

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

              Kind Regards,
              Sy

              JonBJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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.

                SPlattenS 1 Reply Last reply
                0
                • JonBJ JonB

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

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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?

                    SPlattenS 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on 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
                      0
                      • SPlattenS SPlatten

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

                        • Login

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