QSqlTableModel :: setData()也使用Qt :: EditMode

时间:2019-01-23 19:47:06

标签: c++ qt sqlite qt5 qsqltablemodel

QSqlTableModel::setData()的{​​{3}}说:

  

如果角色不是Qt :: EditRole,则返回false。要为除EditRole以外的角色设置数据,请使用自定义代理模型或子类QSqlTableModel。

但是即使角色是false,我仍然得到Qt::EditRole

我的模特:

#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;
    }
}

表定义:

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

    return query.exec();
}

用法:

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);
    }
}

但是它返回false并且数据库当然不会更新;

1 个答案:

答案 0 :(得分:1)

如果source code被修订,则可以理解该问题:

bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    // ...

    if (!(flags(index) & Qt::ItemIsEditable))
        return false;
    // ...
}

可以看出,如果QModelIndex是不可编辑的,则setData将不执行任何操作,并且在这种情况下将返回false。

因此解决方案是启用版本,但我想您不希望该列可编辑,因此,代替使用标记,我们将放置一个不允许编辑的委托:

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);