在 QTableView 上显示数据时,由于内部原因,我无法显示第一行,因此必须隐藏它,使用
(qtableobj)->hideRow(0);
问题是,现在行标签从 2 开始。
如何从 1 开始索引,同时保持第一行隐藏?
谢谢。
在 QTableView 上显示数据时,由于内部原因,我无法显示第一行,因此必须隐藏它,使用
(qtableobj)->hideRow(0);
问题是,现在行标签从 2 开始。
如何从 1 开始索引,同时保持第一行隐藏?
谢谢。
You can try to involve the QSortFilterProxyModel
that will filter out the first row in your model.
The code could look like:
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);
}
}
Finally you need to set this model to your table view:
QTableView *table = new QTableView;
MyItemModel *sourceModel = new MyItemModel;
QSortFilterProxyModel *proxyModel = new FilterModel;
proxyModel->setSourceModel(sourceModel);
table->setModel(proxyModel);
UPDATE
Since the issue is in how the header view displays row numbers, here is the alternative solution which based on special handling of header data in the model:
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);
}
[..]
};
Set up the table view with the hidden first row.
QTableView *table = new QTableView;
Model *sourceModel = new Model;
table->setModel(sourceModel);
table->hideRow(0);
table->show();