Mark
Mark

Reputation: 5102

How to know when a row has been submitted with the OnRowChange edit strategy?

In Qt 6.8.2 when using a QSqlTableModel or a QSqlRelationalTableModel you can set the edit strategy to OnRowChange that automatically submit the row when the focus changes to another row.

The signals of those classes are:

all of them are emitted before a row is added (or updated). Instead I need to know when it has already been submitted to the database successfully.

For example, this is mandatory to update other widgets or related table since the models are not aware of what happens to the database.

Unfortunately, most of the Qt examples (like this) are based on the OnManualSubmit edit strategy. Of course this provides more control to the developer but also requires more interaction by the end user.

I can detect whenever the user changes the row on QTableView, but the QSqlDatabase (docs) does not offer neither a signal nor a function to query the last operation.

Using the OnRowChange edit strategy, how can I know when a row is submitted?

Upvotes: 2

Views: 45

Answers (1)

Mark
Mark

Reputation: 5102

I found a way to achieve what I want, even it's odd Qt6 does not provide a direct way to do this:

class TableModel : public QSqlRelationalTableModel
{
    Q_OBJECT

public:
    explicit TableModel(QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase()) : QSqlRelationalTableModel(parent, db)
    {
    }

public slots:
    bool submit() override
    {
        bool ret = QSqlRelationalTableModel::submit();
        if (ret) emit rowSubmitted();
        return ret;
    }

signals:
    void rowSubmitted();
};

Subclassing QSqlRelationalTableModel allowed me to emit a signal every time the submit() slot is invoked by the delegate, as stated here:

/*!
    This reimplemented slot is called by the item delegates when the
    user stopped editing the current row.
    Submits the currently edited row if the model's strategy is set
    to OnRowChange or OnFieldChange. Does nothing for the OnManualSubmit
    strategy.
    [..]
*/
bool QSqlTableModel::submit()
{
    Q_D(QSqlTableModel);
    if (d->strategy == OnRowChange || d->strategy == OnFieldChange)
        return submitAll();
    return true;
}

Upvotes: 1

Related Questions