Issue with QSqlRelationalTableModel updating Sqlite views



  • I am attempting to use a "subclassed" QSqlRelationalTableModel object that I wish to use to update a Sqlite view (which passes an update to a table via an "instead-of" trigger.)

    I have the following code:

    SqlRelationalTableModel.hpp:
    @
    #ifndef SQLRELATIONALTABLEMODEL_HPP
    #define SQLRELATIONALTABLEMODEL_HPP

    #include <QModelIndex>
    #include <QSqlRelationalTableModel>

    class SqlRelationalTableModel : public QSqlRelationalTableModel {
    Q_OBJECT
    public:
    SqlRelationalTableModel(
    QObject *parent = (QObject *)0
    , QSqlDatabase db = QSqlDatabase());

    QString selectStatement() const;
    bool insertRow(int row, const QModelIndex &parent = QModelIndex());
    bool removeRow(int row, const QModelIndex &parent = QModelIndex());
    

    private slots:
    bool submit();

    signals:
    void dataSubmitted();
    };

    #endif // SQLRELATIONALTABLEMODEL_HPP
    @

    SqlRelationalTableModel.cpp:
    @
    #include <QtGui>
    #include <QtSql>
    #include <QDebug>
    #include "SqlRelationalTableModel.hpp"

    SqlRelationalTableModel::SqlRelationalTableModel(
    QObject *parent, QSqlDatabase db)
    : QSqlRelationalTableModel(parent, db) {
    // intentionally empty function
    }

    QString SqlRelationalTableModel::selectStatement() const {
    return QSqlRelationalTableModel::selectStatement();
    }

    bool SqlRelationalTableModel::insertRow(int row, const QModelIndex &parent) {
    bool isRunningOkay = true;

    isRunningOkay = QSqlRelationalTableModel::insertRow(row, parent);

    emit dataSubmitted();

    return isRunningOkay;
    }

    bool SqlRelationalTableModel::removeRow(int row, const QModelIndex &parent) {
    bool isRunningOkay = true;

    isRunningOkay = QSqlRelationalTableModel::removeRow(row, parent);

    emit dataSubmitted();

    return isRunningOkay;
    }

    bool SqlRelationalTableModel::submit() {
    bool isRunningOkay = QSqlRelationalTableModel::submit();

    emit dataSubmitted();

    return isRunningOkay;
    }
    @

    ItemForm.cpp
    @
    inItemModel = new SqlRelationalTableModel(this);
    inItemModel->setTable("inItemMapView");
    inItemModel->setRelation(
    InItemMapView_CategoryId
    // , QSqlRelation("inCategoryMapView", "categoryId", "categoryName"));
    , QSqlRelation("inCategoryMapView", "categoryId", "categoryId"));
    inItemModel->setSort(InItemMapView_ItemName, Qt::AscendingOrder);
    inItemModel->setHeaderData(InItemMapView_ItemName, Qt::Horizontal, tr("Item"));
    inItemModel->setHeaderData(InItemMapView_CategoryId, Qt::Horizontal, tr("Category"));
    inItemModel->setEditStrategy(QSqlTableModel::OnRowChange);
    inItemModel->select();

    inItemView = new TableView(this);
    inItemView->setModel(inItemModel);
    inItemView->setItemDelegate(new QSqlRelationalDelegate(inItemView));
    @

    The above does allow an update when editing the view, but it shows the categoryId which is an ugly UUID instead of the categoryName. When I comment that out and instead set the relation to categoryName, it does not keep my edits. Nor does it let me delete. Inserts work though, for some reason.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.