Multiple QListViews with one QAbstratListModel



  • 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.



  • Hi,

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

    I not understand question about clear data



  • 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...



  • 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?



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.