Search code examples
c++qtsqliteqt5qsqltablemodel

QSqlTableModel::setData() returns false also with Qt::EditMode


The documentation of QSqlTableModel::setData() says:

Returns false if the role is not Qt::EditRole. To set data for roles other than EditRole, either use a custom proxy model or subclass QSqlTableModel.

but I still get false even if the the role is Qt::EditRole.

My model:

#ifndef MODELOPERATORS_H
#define MODELOPERATORS_H

#include <QSqlTableModel>

enum
{
    MODEL_OPERATORS_COL_ID,
    MODEL_OPERATORS_COL_NAME,
    MODEL_OPERATORS_COL_SIGNATURE,

    MODEL_OPERATORS_COL_COUNT
};

class ModelOperators : public QSqlTableModel
{
    Q_OBJECT

public:
    typedef struct
    {
        QString name;
        QString signature;
    } item_t;

    ModelOperators(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
    ~ModelOperators();
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
};

#endif // MODELOPERATORS_H

#include "modeloperators.h"

ModelOperators::ModelOperators(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db)
{
    setTable("operators");
    setEditStrategy(QSqlTableModel::OnFieldChange);
    select();
}

ModelOperators::~ModelOperators() { }

QVariant ModelOperators::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal)
    {
        if (role == Qt::TextAlignmentRole) return Qt::AlignCenter;
        if (role == Qt::DisplayRole)
        {
            switch(section)
            {
            case MODEL_OPERATORS_COL_NAME: return tr("Name");
            case MODEL_OPERATORS_COL_SIGNATURE: return tr("Signature");
            default: return "";
            }
        }
    }
    return QVariant();
}

Qt::ItemFlags ModelOperators::flags(const QModelIndex &index) const
{
    switch (index.column())
    {
    case MODEL_OPERATORS_COL_NAME: return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    case MODEL_OPERATORS_COL_SIGNATURE: return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    default: return Qt::NoItemFlags;
    }
}

Table definition:

bool DatabaseManager::appCreateTableOperators()
{
    QSqlQuery query(QSqlDatabase::database("dbApp"));
    query.prepare("CREATE TABLE operators ("
                  "id INTEGER PRIMARY KEY, "
                  "name TEXT, "
                  "signature TEXT)");

    return query.exec();
}

Usage:

DialogSettings::DialogSettings(QWidget *parent) : QDialog(parent), ui(new Ui::DialogSettings)
{
    ui->setupUi(this);

    _modelOperators = new ModelOperators(this, QSqlDatabase::database("dbApp"));
    ui->tableOperators->setModel(_modelOperators);
    ui->tableOperators->hideColumn(MODEL_OPERATORS_COL_ID);
    ui->tableOperators->sortByColumn(MODEL_OPERATORS_COL_NAME);
    ui->tableOperators->setItemDelegate(&_delegateOperators);
}

void DialogSettings::on_btnOperatorsBrowse_clicked()
{
    if (ui->tableOperators->selectionModel()->hasSelection())
    {
        QString filename = QFileDialog::getOpenFileName(this, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)"));
        if (filename.isEmpty()) return;
        int row = ui->tableOperators->selectionModel()->currentIndex().row();
        QModelIndex index = _modelOperators->index(row, MODEL_OPERATORS_COL_SIGNATURE);
        qDebug() << _modelOperators->setData(index, filename, Qt::EditRole);
    }
}

But it returns false and of course the db is not updated;


Solution

  • If the source code is revised, the problem is understood:

    bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        // ...
    
        if (!(flags(index) & Qt::ItemIsEditable))
            return false;
        // ...
    }
    

    It is seen that if the QModelIndex is not editable then setData will do nothing and return false as in this case.

    So the solution is to enable the edition but I guess you do not want that column to be editable so instead of using the flag we will place a delegate that does not allow the editing:

    Qt::ItemFlags ModelOperators::flags(const QModelIndex &index) const
    {
        switch (index.column())
        {
        case MODEL_OPERATORS_COL_NAME: return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
        case MODEL_OPERATORS_COL_SIGNATURE: return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
        default: return Qt::NoItemFlags;
        }
    }
    

    class ReadOnlyDelegate: public QStyledItemDelegate
    {
    public:
        using QStyledItemDelegate::QStyledItemDelegate;
        QWidget *createEditor(QWidget */*parent*/, const QStyleOptionViewItem & /*option*/, const QModelIndex &/*index*/) const override
        {
            return nullptr;
        }
    };
    

    _modelOperators = new ModelOperators(this, QSqlDatabase::database("dbApp"));
    ui->tableOperators->setModel(_modelOperators);
    ui->tableOperators->hideColumn(MODEL_OPERATORS_COL_ID);
    ui->tableOperators->sortByColumn(MODEL_OPERATORS_COL_NAME);
    ui->tableOperators->setItemDelegate(&_delegateOperators);
    
    ReadOnlyDelegate *delegate = new ReadOnlyDelegate(ui->tableOperators);
    ui->tableOperators->setItemDelegateForColumn(MODEL_OPERATORS_COL_SIGNATURE, delegate);