Laszlo Papp <lp...@kde.org>
9:57 AM (19 minutes ago)
to interest

Hi,

I have a simple test bed based on the simple tree model Qt example. I have
a tree model thereof displaying in a QTreeView. This is working fine.

I was requested to display the leaf nodes from the QTreeView in a separate
QTableView. Only those leaf nodes that correspond to the currently selected
branch node in the QTreeView.

So, I tried to write this code:

**TablePRoxyModel.h**

    #ifndef TABLEPROXYMODEL_H
    #define TABLEPROXYMODEL_H

    #include <QSortFilterProxyModel>

    class TableProxyModel : public QSortFilterProxyModel
    {
      Q_OBJECT

    public:
      explicit TableProxyModel(QObject* parent = nullptr);

    protected:
      bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent)
const override;
    };

    #endif

**TableProxyModel.cpp**

    #include "TableProxyModel.h"

    #include "CustomModelItem.h"

    TableProxyModel::TableProxyModel(QObject* parent)
      : QSortFilterProxyModel(parent)
    {
    }

    bool TableProxyModel::filterAcceptsRow(int sourceRow,
                                           const QModelIndex& sourceParent)
const
    {
      QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
      CustomModelItem* item =
static_cast<CustomModelItem*>(index.internalPointer());
      return !item->childCount();
    }

Then, I tried setting this proxy on my table as follows:

    _tableView = new QTableView(this);
    _tableProxyModel = new TableProxyModel();
    _tableProxyModel->setSourceModel(_model);
    _tableProxyModel->setRecursiveFilteringEnabled(true);
    _tableView->setModel(_tableProxyModel);

But somehow, this is not working. Only the "root" node gets displayed in
the table view.

Do you have any ideas what I could be missing?

Essentially, I would like to achieve the same as this stackoverflow
question:

https://stackoverflow.com/questions/3384005/qt-table-and-tree-view-with-the-same-model

Thank you in advance.

Kind regards,
Laszlo
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to