[Interest] Displaying QTreeView leaf nodes in a QTableView
László Papp
lpappyt at gmail.com
Wed Sep 20 11:17:39 CEST 2023
Laszlo Papp <lpapp at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20230920/fb7c21ec/attachment.htm>
More information about the Interest
mailing list