Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QListView. Unselect item.

    General and Desktop
    5
    13
    15713
    Loading More Posts
    • 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
      Chiz_ last edited by

      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 Reply Quote 0
      • A
        andre last edited by

        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 Reply Quote 0
        • C
          Chiz_ last edited by

          [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 Reply Quote 0
          • A
            andre last edited by

            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 Reply Quote 0
            • C
              Chiz_ last edited by

              [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 Reply Quote 0
              • G
                goetz last edited by

                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 Reply Quote 0
                • C
                  Chiz_ last edited by

                  [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 Reply Quote 0
                  • I
                    ivan.todorovich last edited by

                    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 Reply Quote 0
                    • C
                      Chiz_ last edited by

                      [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 Reply Quote 0
                      • D
                        dangelog last edited by

                        [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 Reply Quote 0
                        • I
                          ivan.todorovich last edited by

                          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 Reply Quote 0
                          • G
                            goetz last edited by

                            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 Reply Quote 0
                            • C
                              Chiz_ last edited by

                              [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 Reply Quote 0
                              • First post
                                Last post