gpt4 book ai didi

c++ - 在 QtableView 中更改行标签起始索引(垂直标题)

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:29 26 4
gpt4 key购买 nike

在 QTableView 上显示数据时,由于内部原因,我无法显示第一行,因此必须隐藏它,使用

(qtableobj)->hideRow(0);

问题是,现在行标签从 2 开始。

enter image description here

如何从 1 开始索引,同时隐藏第一行?

谢谢。

最佳答案

您可以尝试使用 QSortFilterProxyModel 来过滤掉模型中的第一行。代码可能如下所示:

class FilterModel : QSortFilterProxyModel
{
[..]
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
if (index.row() == 0) // The first row to filter.
return false;
else
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
}

最后您需要将此模型设置为您的 TableView :

QTableView *table = new QTableView;
MyItemModel *sourceModel = new MyItemModel;
QSortFilterProxyModel *proxyModel = new FilterModel;

proxyModel->setSourceModel(sourceModel);
table->setModel(proxyModel);

更新

由于问题在于标题 View 如何显示行号,因此这是基于对模型中标题数据的特殊处理的替代解决方案:

class Model : public QAbstractTableModel
{
public:
[..]
virtual QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Vertical) {
// Decrease the row number value for vertical header view.
return section - 1;
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
[..]
};

设置隐藏第一行的表格 View 。

QTableView *table = new QTableView;
Model *sourceModel = new Model;
table->setModel(sourceModel);
table->hideRow(0);
table->show();

关于c++ - 在 QtableView 中更改行标签起始索引(垂直标题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980067/

26 4 0