Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue with QSqlRelationalTableModel updating Sqlite views

Issue with QSqlRelationalTableModel updating Sqlite views

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 945 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swivelhead
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved