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. Multiple QListViews with one QAbstratListModel
Qt 6.11 is out! See what's new in the release blog

Multiple QListViews with one QAbstratListModel

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.4k 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.
  • T Offline
    T Offline
    tesmai4
    wrote on last edited by
    #1

    Dear all,

    I have one QAbstractListModel and I it in two QListViews in different ways. First QListView is a pop-up and will display only a selection of the items in QAbstractListModel. I hide this view when escape key is pressed. Whereas, second QListView is part of the QMainWindow and will display all items in the QAbstractListModel.

    I am stuck at two points:

    1. How to make selection from QAbstractListModel for the first QListView?
    2. I need to clear the items displayed in second QListView when escape key is pressed for hiding the first QListView.

    any idea how t o do this? Any little help will be appreciated.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      for the first QListView you should use a QSortFilterProxyModel in order to filter data to show.

      I not understand question about clear data

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        poorBob
        wrote on last edited by
        #3

        Can You show us a little code?

        1. How to make selection from QAbstractListModel for the first QListView?:
          Did You read this: "Sharing selections among views in Qt's MVC":https://qt-project.org/doc/qt-4.8/model-view-programming.html#sharing-selections-among-views ? And as mcosta wrote You should use the QSorFilterProxyModel

        2. I need to clear the items displayed in second QListView when escape key is pressed for hiding the first QListView.
          I don't understand this at all...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tesmai4
          wrote on last edited by
          #4

          Thanks for your reply. Below is the code snippen for making selection QAbstractlistmodel
          @
          Class myframe : public QFame
          {
          ...
          ...
          ...
          private:
          QItemSelectionModel *mpSelectionModel;
          ListModel *mpListModel;
          ListView *mpListView;
          ...
          ...
          ...
          };
          myframe()
          {

          mpListModel = new ListModel(this);
          mpListView->setModel(mpListModel);
          mpSelectionModel = mpListView->selectionModel();
          mpListView->populateListView(mpListModel);
          }
          @

          in my hide function, I am not clearing the listmodel from the listview. I simply hide it. I have a clear function that removes items from ListModel, however, this function is not removing the contents..
          What can I do?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi,

            I still don't understand; when you update model (if correctly done) the view automatically update its contents.

            This code works good for me
            @
            void ModelViewWidget::on_clearButton_clicked()
            {
            int rowCount = m_model->rowCount();

            m_model->removeRows(0, rowCount);
            

            }
            @

            If you implemented your model you must pay attention to reimplements removeRows

            read "here":http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginRemoveRows for details

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            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