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. QListView deselect item by clicking on selected Item
Forum Updated to NodeBB v4.3 + New Features

QListView deselect item by clicking on selected Item

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.2k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey,

    I have a question about the QListView. In a QListView I can set the SelectionMode via listView->setSelectionMode(QAbstractItemView::SelectionMode).I want to achieve the same as with QAbstractItemView::ExtendedSelection but without pressing the CTRL key. The best would be to simply click on the activated item and this will deselect it. Is this possible in a simple way?

    1 Reply Last reply
    0
    • ? A Former User

      Hi,

      no MultiSelection is not what I want. I want a single item selection and the possibility to select multiple items with shift. Selecting and deselecting a single selection should only be done by mouse click (not CTRL + mouse).

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      @Gabber
      Hi
      Ok. i missed the shift part.

      Well you can try a mousepress override.
      Normally one would subclass QItemSelectionModel but I'm not sure if we can know ctrl was pressed (in the view) and
      change the selection. logic

      You can try

      class ToggleListView : public QListView
      {
          Q_OBJECT
      public:
          explicit ToggleListView(QWidget *parent = Q_NULLPTR) : QListView(parent) {}
      protected:
          void mousePressEvent(QMouseEvent *event) override
          {
              Qt::KeyboardModifiers keys;
              keys.setFlag(Qt::ControlModifier);
              event->setModifiers(keys);
              QListView::mousePressEvent(event);
          }
      };
      
      

      Seems to be ok but you might want to test if shift is pressed and then not set CTRL.

      ? 1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        But does
        setSelectionMode(QAbstractItemView::SelectionMode::MultiSelection);
        not function as you want as it can select/deselect by clicking ?
        (no ctrl)

        alt text

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #3

          Hi,

          no MultiSelection is not what I want. I want a single item selection and the possibility to select multiple items with shift. Selecting and deselecting a single selection should only be done by mouse click (not CTRL + mouse).

          mrjjM 1 Reply Last reply
          0
          • ? A Former User

            Hi,

            no MultiSelection is not what I want. I want a single item selection and the possibility to select multiple items with shift. Selecting and deselecting a single selection should only be done by mouse click (not CTRL + mouse).

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #4

            @Gabber
            Hi
            Ok. i missed the shift part.

            Well you can try a mousepress override.
            Normally one would subclass QItemSelectionModel but I'm not sure if we can know ctrl was pressed (in the view) and
            change the selection. logic

            You can try

            class ToggleListView : public QListView
            {
                Q_OBJECT
            public:
                explicit ToggleListView(QWidget *parent = Q_NULLPTR) : QListView(parent) {}
            protected:
                void mousePressEvent(QMouseEvent *event) override
                {
                    Qt::KeyboardModifiers keys;
                    keys.setFlag(Qt::ControlModifier);
                    event->setModifiers(keys);
                    QListView::mousePressEvent(event);
                }
            };
            
            

            Seems to be ok but you might want to test if shift is pressed and then not set CTRL.

            ? 1 Reply Last reply
            3
            • mrjjM mrjj

              @Gabber
              Hi
              Ok. i missed the shift part.

              Well you can try a mousepress override.
              Normally one would subclass QItemSelectionModel but I'm not sure if we can know ctrl was pressed (in the view) and
              change the selection. logic

              You can try

              class ToggleListView : public QListView
              {
                  Q_OBJECT
              public:
                  explicit ToggleListView(QWidget *parent = Q_NULLPTR) : QListView(parent) {}
              protected:
                  void mousePressEvent(QMouseEvent *event) override
                  {
                      Qt::KeyboardModifiers keys;
                      keys.setFlag(Qt::ControlModifier);
                      event->setModifiers(keys);
                      QListView::mousePressEvent(event);
                  }
              };
              
              

              Seems to be ok but you might want to test if shift is pressed and then not set CTRL.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #5

              @mrjj
              Thanks for your code example, it works perfectly!

              I have thought about the whole thing again and will stay for now with a simple select (without shift). Therefore I will mark the thread as solved.

              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