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. View->Model and selection...

View->Model and selection...

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.5k 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.
  • R Offline
    R Offline
    ronM71
    wrote on last edited by
    #1

    It seems there is zero built-in selection support with my choice of QListView -> QAbstractListModel.

    Do I have to write everything from scratch? the catching of a selection event in the UI, the marking of the model item as selected, etc? It seems there is no out-of-the-box support for this.

    the weird thing is that there is a QItemSelectionModel that does support this, but you cannot use it with QListView as it's not derived from QAbstract....

    Should my model class use multiple inheritance to inherit both from QItemSelectionModel and QAbstractListModel? Otherwise I don't see how I can avoid having to re-writing this functionality myself.

    My final goal is for the delegate that draws my items to know if the item is selected, both in the paint and the sizeHint function.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      "QAbstractItemView::selectionMode":http://doc.qt.nokia.com/4.7/qabstractitemview.html#selectionMode-prop does the stuff

      The selection is part of the view not of the model!

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ronM71
        wrote on last edited by
        #3

        I already have :
        @
        // Set up multi-select for the contacts list
        ui->ContactsList->setSelectionMode(QAbstractItemView::MultiSelection);
        @

        But how do I know which row is selected in my Item delegate's hintSize or Paint?

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

          Well... if you look carefully at your item views, you see that they have both a data model (a QAbstractItemModel) and a QItemSelectionModel. You don't seriously think that Qt would not support these basic features, if they are shown off in the samples for the item views?

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ronM71
            wrote on last edited by
            #5

            so, my QListView's ancestor has: "virtual void setSelectionModel ( QItemSelectionModel * selectionModel )"

            But what does that mean? should I just set is with a "new QItemSelectionModel"?

            terminology here is strange. A Model is DATA. Why do I have to set two models for my view if I just want to access selection information? what's the logic behind this? have another model where the VIEW will write its selection information? Wouldn't it be simpler to allow the user to derive an item model that already has the selection defined in its metadata?

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

              hi ronM71,

              just read the docs:

              QListView derives from QAbstractItemView, and QAbstractItemView owns a selectionModel.
              The delegate gets everything via QStyleOptionViewItem and QModelIndex.

              QStyleOptionViewItem is derived from QStyleOption and "QStyleOption::state":http://doc.qt.nokia.com/4.7/qstyleoption.html#state-var and "state":http://doc.qt.nokia.com/4.7/qstyle.html#StateFlag-enum contains QStyle::State_Selected

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                Every view has a selection model by default. However, being able to set one, makes it possible to share such a model between different views. Thus keeping selections synchronized between the two (or more) views. It is not as bizare as you think :-)

                And yes: a model does contain data. Data on selections.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  If you look through "this":http://doc.qt.nokia.com/4.7/model-view-programming.html document, you get all this information about delegates, selections etc.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                    It also helps to look at the source code of the delegates that Qt provides. You can learn a lot from there :-)

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

                    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