QTreeView 在模型和视图之间使用包装器时不显示任何项目
QTreeView does not show any item when using a wrapper between model and view
我正在尝试在 GitAhead 中创建 diff 文件的不同视图。他们已经提供了一个可以正常工作的树模型。我试图在模型和视图之间放置一个包装器以仅显示更改的文件。目前我只想转发模型,但视图没有显示任何内容。 rowCount() 被调用得很好,然后 index() 也被调用,但 data() 永远不会被调用。有谁知道为什么我不能转发模型吗?
TreeWrapper.h:
#include "TreeWrapper.h"
#include "TreeModel.h"
#include "conf/Settings.h"
#include "git/Blob.h"
#include "git/Diff.h"
#include "git/RevWalk.h"
#include "git/Submodule.h"
#include <QStringBuilder>
#include <QUrl>
namespace {
const QString kLinkFmt = "<a href='%1'>%2</a>";
} // anon. namespace
TreeWrapper::TreeWrapper(TreeModel* model, QObject *parent):
mModel(model),
QAbstractItemModel(parent)
{
}
TreeWrapper::~TreeWrapper()
{
}
void TreeWrapper::setTree(const git::Tree &tree, const git::Diff &diff)
{
beginResetModel();
mModel->setTree(tree, diff);
endResetModel();
}
int TreeWrapper::rowCount(const QModelIndex &parent) const
{
int count = mModel->rowCount(parent);
return count;
}
int TreeWrapper::columnCount(const QModelIndex &parent) const
{
return mModel->columnCount(parent);
}
bool TreeWrapper::hasChildren(const QModelIndex &parent) const
{
bool children = mModel->hasChildren(parent);
return children;
}
QModelIndex TreeWrapper::parent(const QModelIndex &index) const
{
return mModel->parent(index);
}
QModelIndex TreeWrapper::index(
int row,
int column,
const QModelIndex &parent) const
{
QModelIndex index = mModel->index(row, column, parent);
bool valid = index.isValid();
return index;
}
QVariant TreeWrapper::data(const QModelIndex &index, int role) const
{
return mModel->data(index, role);
}
bool TreeWrapper::setData(
const QModelIndex &index,
const QVariant &value,
int role)
{
return mModel->setData(index, value, role);
}
Qt::ItemFlags TreeWrapper::flags(const QModelIndex &index) const
{
return mModel->flags(index);
}
TreeWrapper.h:
#ifndef TREEWRAPPER
#define TREEWRAPPER
#include "git/Diff.h"
#include "git/Index.h"
#include "git/Tree.h"
#include "git/Repository.h"
#include <QAbstractItemModel>
#include <QFileIconProvider>
class TreeModel;
class TreeWrapper : public QAbstractItemModel
{
Q_OBJECT
public:
TreeWrapper(TreeModel* model, QObject *parent = nullptr);
virtual ~TreeWrapper();
void setTree(const git::Tree &tree, const git::Diff &diff = git::Diff());
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex()) const override;
QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole) const override;
bool setData(
const QModelIndex &index,
const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
TreeModel* mModel{nullptr};
};
#endif // TREEWRAPPER
下面的treeview直接使用了Treemodel,在上面的,我想把Wrapper放在model和view之间。
我通过在模型和视图之间放置一个 QSortFilterProxyModel 解决了这个问题:
https://github.com/Murmele/gitahead/blob/AlternativeTreeView/src/ui/TreeProxy.cpp
此代理已使用两次。一次代理过滤 staged,第二次过滤 unstaged。
我正在尝试在 GitAhead 中创建 diff 文件的不同视图。他们已经提供了一个可以正常工作的树模型。我试图在模型和视图之间放置一个包装器以仅显示更改的文件。目前我只想转发模型,但视图没有显示任何内容。 rowCount() 被调用得很好,然后 index() 也被调用,但 data() 永远不会被调用。有谁知道为什么我不能转发模型吗?
TreeWrapper.h:
#include "TreeWrapper.h"
#include "TreeModel.h"
#include "conf/Settings.h"
#include "git/Blob.h"
#include "git/Diff.h"
#include "git/RevWalk.h"
#include "git/Submodule.h"
#include <QStringBuilder>
#include <QUrl>
namespace {
const QString kLinkFmt = "<a href='%1'>%2</a>";
} // anon. namespace
TreeWrapper::TreeWrapper(TreeModel* model, QObject *parent):
mModel(model),
QAbstractItemModel(parent)
{
}
TreeWrapper::~TreeWrapper()
{
}
void TreeWrapper::setTree(const git::Tree &tree, const git::Diff &diff)
{
beginResetModel();
mModel->setTree(tree, diff);
endResetModel();
}
int TreeWrapper::rowCount(const QModelIndex &parent) const
{
int count = mModel->rowCount(parent);
return count;
}
int TreeWrapper::columnCount(const QModelIndex &parent) const
{
return mModel->columnCount(parent);
}
bool TreeWrapper::hasChildren(const QModelIndex &parent) const
{
bool children = mModel->hasChildren(parent);
return children;
}
QModelIndex TreeWrapper::parent(const QModelIndex &index) const
{
return mModel->parent(index);
}
QModelIndex TreeWrapper::index(
int row,
int column,
const QModelIndex &parent) const
{
QModelIndex index = mModel->index(row, column, parent);
bool valid = index.isValid();
return index;
}
QVariant TreeWrapper::data(const QModelIndex &index, int role) const
{
return mModel->data(index, role);
}
bool TreeWrapper::setData(
const QModelIndex &index,
const QVariant &value,
int role)
{
return mModel->setData(index, value, role);
}
Qt::ItemFlags TreeWrapper::flags(const QModelIndex &index) const
{
return mModel->flags(index);
}
TreeWrapper.h:
#ifndef TREEWRAPPER
#define TREEWRAPPER
#include "git/Diff.h"
#include "git/Index.h"
#include "git/Tree.h"
#include "git/Repository.h"
#include <QAbstractItemModel>
#include <QFileIconProvider>
class TreeModel;
class TreeWrapper : public QAbstractItemModel
{
Q_OBJECT
public:
TreeWrapper(TreeModel* model, QObject *parent = nullptr);
virtual ~TreeWrapper();
void setTree(const git::Tree &tree, const git::Diff &diff = git::Diff());
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex()) const override;
QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole) const override;
bool setData(
const QModelIndex &index,
const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
TreeModel* mModel{nullptr};
};
#endif // TREEWRAPPER
下面的treeview直接使用了Treemodel,在上面的,我想把Wrapper放在model和view之间。
我通过在模型和视图之间放置一个 QSortFilterProxyModel 解决了这个问题: https://github.com/Murmele/gitahead/blob/AlternativeTreeView/src/ui/TreeProxy.cpp
此代理已使用两次。一次代理过滤 staged,第二次过滤 unstaged。