Problem with adding a new column to QSortFilterProxyModel: columns shift

334 views Asked by At

I'm trying to add a column to a proxy model.

Long story short, I'm adding a column in an overloaded setSourceModel method with an QSortFilterProxyModel::insertColumn() method.

.h

class CompoundItemsTableModel : public QSortFilterProxyModel {
    Q_OBJECT
public:
    explicit CompoundItemsTableModel(QObject* parent = nullptr);
    void setSourceModel(QAbstractItemModel* sourceModel) override;
}

.cpp

void CompoundItemsTableModel::setSourceModel(QAbstractItemModel* sourceModel)
{
   QSortFilterProxyModel::setSourceModel(sourceModel);

   beginInsertColumns(QModelIndex(), 3, 3);
   QSortFilterProxyModel::insertColumn(3);
   endInsertColumns();
}

Then we use this model in a table:

.h

class VisibleTable : public QTableView {
public:
    explicit VisibleTable(QWidget* parent = nullptr);

.cpp

VisibleTable::VisibleTable(QWidget* parent)
    : QTableView(parent)
{      
    auto* proxyModel = new Database::CompoundItemsTableModel(this);
    proxyModel->setSourceModel(
        Database::SqLiteModel::initializeModel());
    setModel(proxyModel);
}

Result: with any input all cells in the edited row to the right of the column are shifted one cell left.

enter image description here

What I found out:

  1. QSortFilterProxyModel::data shows different values before and after any change. If I disable QSortFilterProxyModel::setData() it doesn't shift. But of course, it doesn't set data. setData() surely sends a signal.
  2. Delegates shift as well. If I input after the row has shifted, it will be inserted correctly into the SQL
  3. Certainly, a shift occurs after the column that was added, not before.

UPD 1: The shift is triggered by QTableView::commitData(editor);, so sa soon as it works, the row is redrawn with a shift.

UPD 2: I've tried adding a column to the model, in my case it's QSqlRelationalTableModel.

    auto* model { new QSqlRelationalTableModel };

    model->setTable("table");
    model->select();
    model->insertColumn(0);

Result: the same, see pic:

enter image description here

UPD 3: I've tried doing the same with the underlying QSqlRelationalTableModel, and it proved to be unsuccessful. Entirelly the same effect.

0

There are 0 answers