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. Unselect item.
QtWS25 Last Chance

QListView. Unselect item.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 16.9k 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.
  • C Offline
    C Offline
    Chiz_
    wrote on last edited by
    #1

    I have QListView widget. It have some items.
    When I click on an item I show details about this item. Everything is good so far.
    If I click another item, details of clicked element is shown. Everything is perfect so far.
    For such behavior I use clicked(QModelIndex) signal in QListView.
    Question: how to UNSELECT item? I want to click on empty area of QListView and hide details. Problem is that signal clicked(QModelIndex) is not invoked/called.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Obviously the clicked(QModelIndex) signal is not invoked, if you did not click on an item.

      You could use the signals provided by QItemSelectionModel. A pointer to an instance of that class is available for every QAbstractItemView-derived class, including QListView.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chiz_
        wrote on last edited by
        #3

        [quote author="Andre" date="1295272042"]Obviously the clicked(QModelIndex) signal is not invoked, if you did not click on an item.

        You could use the signals provided by QItemSelectionModel. A pointer to an instance of that class is available for every QAbstractItemView-derived class, including QListView.

        [/quote]
        Thank you for your time!
        I can't get it work. I try
        @// create connect from listview selectionModel
        connect( listView->selectionModel(), SIGNAL( selectionChanged( const QItemSelection &, const QItemSelection & )),
        this, SLOT( selectionChanged( const QItemSelection &, const QItemSelection & ) ));

        // slot you created for your class ( e.g MainWindow here )
        void MainWindow::selectionChanged( const QItemSelection &sel, const QItemSelection &des )
        {
        Q_UNUSED( des ); // keep compiler from complaining
        if( sel.isEmpty() ) {
        listView->selectionModel()->reset(); // reset the selectionModel to clear entries and highlighted items
        // ... other post clean up if necessary .. //
        }
        }@
        But it doesn't help. I'm trying to find smth around it, but for now it's without result.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Well, sel.isEmpty() on line 9 is true, then you don't actually have selected items, do you?

          So what is it you want to reset, exactly?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chiz_
            wrote on last edited by
            #5

            [quote author="Andre" date="1295347918"]Well, sel.isEmpty() on line 9 is true, then you don't actually have selected items, do you?

            So what is it you want to reset, exactly? [/quote]

            Method "selectionChanged" doesn't arise at all. So there is no matter what is inside method :-)

            I solve my problem, but I don't like the way I did it. I set selectionMode from SingleSelection to ExtendedSelection. I don't like that user can select more then one item, but so far I can't achive unselecting.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Which selection mode do you have set on your list view?

              bq. QAbstractItemView::SingleSelection:
              When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.

              With single selection, you could subclass QListView, reimplement mousePressEvent, check if the mouse press was on a valid index (continue with the base class implementation) or not (clear the selection model and stop).

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chiz_
                wrote on last edited by
                #7

                [quote author="Volker" date="1295352652"]Which selection mode do you have set on your list view?

                bq. QAbstractItemView::SingleSelection:
                When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.

                With single selection, you could subclass QListView, reimplement mousePressEvent, check if the mouse press was on a valid index (continue with the base class implementation) or not (clear the selection model and stop).[/quote]

                Wow! Thanks a lot!
                I just need QAbstractItemView::ContiguousSelection selection mode.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  ivan.todorovich
                  wrote on last edited by
                  #8

                  There's currently an active "Jira Ticket":http://bugreports.qt.nokia.com/browse/QTBUG-8836 on this issue.
                  Since there's no way to interactively deselect item. You should take a look at it and vote if you are interested.

                  o_o Sorry for my rusted english.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chiz_
                    wrote on last edited by
                    #9

                    [quote author="ivan.todorovich" date="1295444078"]There's currently an active "Jira Ticket":http://bugreports.qt.nokia.com/browse/QTBUG-8836 on this issue.
                    Since there's no way to interactively deselect item. You should take a look at it and vote if you are interested.[/quote]

                    It explains everything. Thanks a lot. I'll be tracking this bug.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #10

                      [quote author="Chiz_" date="1295271468"]I have QListView widget. It have some items.
                      When I click on an item I show details about this item. Everything is good so far.
                      If I click another item, details of clicked element is shown. Everything is perfect so far.
                      For such behavior I use clicked(QModelIndex) signal in QListView.
                      Question: how to UNSELECT item? I want to click on empty area of QListView and hide details. Problem is that signal clicked(QModelIndex) is not invoked/called.[/quote]

                      How about subclassing and using something like this in your mousePressEvent?
                      @
                      QModelIndex index = indexAt(event->pos());
                      if (index.isValid()) {
                      selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
                      } else {
                      clearSelection();
                      }
                      @

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        ivan.todorovich
                        wrote on last edited by
                        #11

                        reimplement mouseReleaseEvent instead of mousePress, since the 'click' happens when you release the mouse.

                        o_o Sorry for my rusted english.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          As far as I know this is platform dependent. On a Mac the selction changes as soon as you press the mouse button. On windows it might be not until button release.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Chiz_
                            wrote on last edited by
                            #13

                            [quote author="Volker" date="1295554550"]As far as I know this is platform dependent. On a Mac the selction changes as soon as you press the mouse button. On windows it might be not until button release.[/quote]

                            Thanks for such information. I'm developing on WinXP for Linux :-)

                            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