Pregunta

While displaying data, on a QTableView , due to an internal reason, i cannot display the first row and hence have to hide it, using

(qtableobj)->hideRow(0);

The problem is that, now the row labels start from 2.

enter image description here

How is it possible to start the index from 1, while keeping the first row hidden ?

Thanks.

¿Fue útil?

Solución

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top