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: how to prevent unselect all?
QtWS25 Last Chance

QListWidget: how to prevent unselect all?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistwidgetselection
7 Posts 4 Posters 6.2k 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.
  • F Offline
    F Offline
    Franz Amador
    wrote on 1 Feb 2016, 18:24 last edited by
    #1

    A QListWidget in SingleSelection mode allows unselecting the last selected item in several ways, such as Ctrl-click (the docs suggest that it should not allow this, but see the comments on QTBUG-49217). How can I prevent this? My item list must always have one item selected.

    I first tried overriding mousePressEvent and ignoring it if necessary. This works, but there are other ways to unselect (e.g. it can happen on mouseMoveEvent), and it seems clunky to find and override them all.

    I next tried setting a custom QItemSelectionModel that refuses to unselect the currently selected item. That leads to what may be a Qt bug: QListWidget never issues its itemSelectionChanged signal, even if my QItemSelectionModel has no custom behavior at all. (Apparently my call to QListWidget::setSelectionModel somehow causes the connection between QItemSelectionModel::selectionChanged and QListWidget::itemSelectionChanged to break. See my related question.)

    Ideally, QListWidget would have optional require-at-least-one-selection behavior built in. I can't be the only one who needs this.

    K R 2 Replies Last reply 2 Feb 2016, 03:11
    0
    • F Franz Amador
      1 Feb 2016, 18:24

      A QListWidget in SingleSelection mode allows unselecting the last selected item in several ways, such as Ctrl-click (the docs suggest that it should not allow this, but see the comments on QTBUG-49217). How can I prevent this? My item list must always have one item selected.

      I first tried overriding mousePressEvent and ignoring it if necessary. This works, but there are other ways to unselect (e.g. it can happen on mouseMoveEvent), and it seems clunky to find and override them all.

      I next tried setting a custom QItemSelectionModel that refuses to unselect the currently selected item. That leads to what may be a Qt bug: QListWidget never issues its itemSelectionChanged signal, even if my QItemSelectionModel has no custom behavior at all. (Apparently my call to QListWidget::setSelectionModel somehow causes the connection between QItemSelectionModel::selectionChanged and QListWidget::itemSelectionChanged to break. See my related question.)

      Ideally, QListWidget would have optional require-at-least-one-selection behavior built in. I can't be the only one who needs this.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 2 Feb 2016, 03:11 last edited by kshegunov 2 Feb 2016, 03:11
      #2

      @Franz-Amador
      Hello,
      See my answer for your related question. I suggest overriding

      void QItemSelectionModel::select(const QModelIndex & index, QItemSelectionModel::SelectionFlags command)
      

      for your selection model to discard the unneeded deselections. Additionally, you should want to override QAbstractItemView::setSelectionModel for your list widget to restore the appropriate selection model connections before calling the default implementation.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • F Franz Amador
        1 Feb 2016, 18:24

        A QListWidget in SingleSelection mode allows unselecting the last selected item in several ways, such as Ctrl-click (the docs suggest that it should not allow this, but see the comments on QTBUG-49217). How can I prevent this? My item list must always have one item selected.

        I first tried overriding mousePressEvent and ignoring it if necessary. This works, but there are other ways to unselect (e.g. it can happen on mouseMoveEvent), and it seems clunky to find and override them all.

        I next tried setting a custom QItemSelectionModel that refuses to unselect the currently selected item. That leads to what may be a Qt bug: QListWidget never issues its itemSelectionChanged signal, even if my QItemSelectionModel has no custom behavior at all. (Apparently my call to QListWidget::setSelectionModel somehow causes the connection between QItemSelectionModel::selectionChanged and QListWidget::itemSelectionChanged to break. See my related question.)

        Ideally, QListWidget would have optional require-at-least-one-selection behavior built in. I can't be the only one who needs this.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 2 Feb 2016, 07:19 last edited by
        #3

        @Franz-Amador
        another possible solution can be overriding QAbstractItemView::selectionCommand() and return QItemSelectionModel::NoUpdate in case the selection should not change.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        F 1 Reply Last reply 8 Feb 2016, 23:44
        1
        • X Offline
          X Offline
          Xicor
          wrote on 4 Feb 2016, 22:59 last edited by
          #4

          just wondering, did you 'ignore' the event or did you 'accept' the event? in qt, the 'ignore' option means that YOUR CLASS is ignoring the event, not that you want qt to ignore the event. if you want qt to ignore the event, you need to call 'accept'.

          F 1 Reply Last reply 5 Feb 2016, 00:08
          0
          • X Xicor
            4 Feb 2016, 22:59

            just wondering, did you 'ignore' the event or did you 'accept' the event? in qt, the 'ignore' option means that YOUR CLASS is ignoring the event, not that you want qt to ignore the event. if you want qt to ignore the event, you need to call 'accept'.

            F Offline
            F Offline
            Franz Amador
            wrote on 5 Feb 2016, 00:08 last edited by
            #5

            @Xicor Properly speaking, I didn't do either. I just overrode mousePressEvent and did not pass the event along to QAbstractItemView::mousePressEvent if I wanted to block the deselect. It did not occur to me to do anything to the event object itself. I'm guessing that was wrong. (I'm new to Qt.)

            K 1 Reply Last reply 5 Feb 2016, 00:10
            0
            • F Franz Amador
              5 Feb 2016, 00:08

              @Xicor Properly speaking, I didn't do either. I just overrode mousePressEvent and did not pass the event along to QAbstractItemView::mousePressEvent if I wanted to block the deselect. It did not occur to me to do anything to the event object itself. I'm guessing that was wrong. (I'm new to Qt.)

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 5 Feb 2016, 00:10 last edited by kshegunov 2 May 2016, 00:10
              #6

              Yes, the point is valid. If you want to stop the propagation of the event, you are supposed to call QEvent::accept and do nothing. However any child widgets you may have are receiving the event before their parent, so it may or may not work.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • R raven-worx
                2 Feb 2016, 07:19

                @Franz-Amador
                another possible solution can be overriding QAbstractItemView::selectionCommand() and return QItemSelectionModel::NoUpdate in case the selection should not change.

                F Offline
                F Offline
                Franz Amador
                wrote on 8 Feb 2016, 23:44 last edited by
                #7

                @raven-worx Thanks. I ended up using this solution, which seems a bit more direct and focused than overriding "select".

                1 Reply Last reply
                0

                6/7

                5 Feb 2016, 00:10

                • Login

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