M
M
Mercury132016-01-14 17:31:38
Qt
Mercury13, 2016-01-14 17:31:38

QAbstractTableModel: why only one row is available and the rest are grey?

We have a QSqlQueryModel that (hopefully) performs as reliably as Delphi's database components. And I need to add one "left" line to it. I am writing such rubbish (slave is the same QSqlQueryModel).
Everything works, only all the lines, except for this newest one, are gray. What did I miss?
Meaning. On a new line row=0, column=*, internal=this.
On old ones, they correspond to the old table.

QModelIndex FmDbImportDialog::InteractivePreviewModel::index(
        int row, int column, const QModelIndex &parent) const
{
    if (row <= 0) {
        return createIndex(0, column, const_cast<InteractivePreviewModel*>(this));
    } else {
        return slave.index(row - 1, column, parent);
    }
}

int FmDbImportDialog::InteractivePreviewModel::rowCount(
        const QModelIndex &parent) const
{
    return slave.rowCount(parent) + 1;
}

int FmDbImportDialog::InteractivePreviewModel::columnCount(
        const QModelIndex &parent) const
{
    return slave.columnCount(parent);
}


QVariant FmDbImportDialog::InteractivePreviewModel::data(
        const QModelIndex &index, int role) const
{
    if (index.internalPointer() == this) {
        switch (role) {
        case Qt::DisplayRole:
            return QString("Test");
        default:
            return QVariant();
        }
    } else {
        return slave.data(index, role);
    }
}

QVariant FmDbImportDialog::InteractivePreviewModel::headerData(
        int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Vertical) {
        return slave.headerData(section, orientation, role);
    } else if (section == 0) {
        return QVariant();
    } else {
        return slave.headerData(section - 1, Qt::Vertical, role);
    }
}

I also tried to inherit from QSqlQueryModel. In general, a circus: the selection behaves unpredictably.
PS It worked when I made continuous line numbering and data() builds a new index, decreasing the line by 1. Whether it works reliably with the database, I don’t know; DB is a delicate thing. But still explain how it works with indexes...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-07-01
@Mercury13

My decision.
QModelIndex has a model reference! Therefore...
1. index should create ITS index.
Wrong, you need to create your own index. And in general, if the model is tabular, the index will be trivial and can be taken from QAbstractTableModel.
2. But data - exactly data, not index - should ask the slave to create a new index.
Wrong, let the slave first convert the coordinates to its index, and then refer to its data with this index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question