如何在具有关系的列上恢复一个零值?

问题描述 投票:0回答:1
在QT 6.8.2 i分类

QTableView

为了捕获
Key_Delete
的压力:

#include <QTableView>
#include <QKeyEvent>

class TableView : public QTableView
{
    Q_OBJECT

public:
    explicit TableView(QWidget *parent = nullptr) : QTableView(parent) { }

protected:
    void keyPressEvent(QKeyEvent *event) override
    {
        if (event->key() == Qt::Key_Delete) emit keyCancelPressed(currentIndex());
        else QTableView::keyPressEvent(event);
    }

signals:
    void keyCancelPressed(const QModelIndex &current);
};

a

QSqlRelationalTableModel
被分配给
TableView
,并设置了关系:

QSqlDatabase db = QSqlDatabase::database(DATABASE_NAME);
QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this, db);
model->setTable(TABLE_NAME);
model->setEditStrategy(QSqlRelationalTableModel::OnRowChange);
model->setRelation(MODEL_COL_ID_HUE_MODE), QSqlRelation("hueModes", "id", "name"));    
model->select();
ui->myTableView->setModel(_model);
ui->myTableView->setItemDelegate(new QSqlRelationalDelegate(ui->myTableView));

如果按下删除键,则创建了一个lambda,以设置为

null
字段:

connect(ui->myTableView, &TableView::keyCancelPressed, this, [=](const QModelIndex &current)
{
    model->setData(current, QVariant());
});

实际行为如下:

  • 如果当前单元格包含一个平均值,则代码可行,并且一个值还原回到模型

    
    

  • 如果当前单元格包含一个关系,代码没有效果 - 当前值仍然存在
  • 如果我们仍然处于模型级别,那么我的SQL列没有
null

子句:

NOT NULL

通过方式,该代码甚至在

CREATE TABLE timelines \ (idProgram INT NOT NULL, \ time INT NOT NULL, \ h REAL, \ s REAL, \ l REAL, \ idHueMode INT, \ PRIMARY KEY (idProgram, time), \ FOREIGN KEY (idProgram) REFERENCES programs(id) ON DELETE CASCADE ON UPDATE CASCADE, \ FOREIGN KEY (idHueMode) REFERENCES hueModes(id) ON DELETE SET DEFAULT ON UPDATE CASCADE);"; 列上工作,因为我们正在使用所述模型。当然,当我尝试提交时,我会在这种情况下收到错误。 toming进行调试,我将代码更改为:

time

输出为:

qDebug() << model->setData(current, QVariant()); qDebug() << model->lastError();
确实,确实非常有用。
如何在具有关系的列上恢复一个

false QSqlError("", "", "")

值?
	

i在sourcecode

中找到了解释:
null

sqlite qt qt6 qsqlrelationaltablemodel
1个回答
0
投票

对于关系列,[...]索引也必须存在于引用 表,否则该功能返回false。

我唯一能看到的解决限制的解决方法是创建一个新记录(最初将所有列设置为
/*!
    Sets the data for the \a role in the item with the specified \a
    index to the \a value given. Depending on the edit strategy, the
    value might be applied to the database at once, or it may be
    cached in the model.

    Returns \c true if the value could be set, or false on error (for
    example, if \a index is out of bounds).

    For relational columns, \a value must be the index, not the
    display value. The index must also exist in the referenced
    table, otherwise the function returns \c false.

    \sa editStrategy(), data(), submit(), revertRow()
*/
bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant &value,
                                       int role)
{
    Q_D(QSqlRelationalTableModel);
    if ( role == Qt::EditRole && index.column() > 0 && index.column() < d->relations.size()
            && d->relations.at(index.column())->isValid()) {
        auto relation = d->relations.at(index.column());
        if (!relation->isDictionaryInitialized())
            relation->populateDictionary();
        if (!relation->dictionary.contains(value.toString()))
            return false;
    }
    return QSqlTableModel::setData(index, value, role);
}
),复制所有当前值,但是用户唯一要还原,提交并最终删除了上一张。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.