QSqlRelationalModel model/view update after changes

1.4k views Asked by At

I've got a problem with updating model/view after changes.

To explain better what I mean I wrote a simple example with SQLite.

So the main.cpp file:

#include <QApplication>

#include "MainForm.h"

void createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("deptemployee.dat");
    if (!db.open()) {
        QMessageBox::information(0, QObject::tr("Database Error"), db.lastError().text());
        db.close();
        return;
    }
}

void createFakeData()
{
    QSqlQuery query;
    query.exec("DROP TABLE Department");
    query.exec("DROP TABLE Employee");

    query.exec("CREATE TABLE Department(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(40) NOT NULL)");
    query.exec("CREATE TABLE Employee(id INTEGER PRIMARY KEY AUTOINCREMENT, departmentid INTEGER NOT NULL, FOREIGN KEY (departmentid) REFERENCES Department)");

    query.exec("INSERT INTO Department(name) VALUES('SomeDepartment')");
    query.exec("INSERT INTO Department(name) VALUES('AnotherDepartment')");

    query.exec("INSERT INTO Employee(departmentid) VALUES(1)");
    query.exec("INSERT INTO Employee(departmentid) VALUES(2)");
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    createConnection();
    createFakeData();

    MainForm form;
    form.resize(500, 600);
    form.show();

    return a.exec();
}

The MainForm.h file:

#pragma once

#include <QWidget>
#include <QObject>

#include <QtWidgets>
#include <QtSql>

enum {
    Employee_Id = 0,
    Employee_DepartmentId = 1
};

class MainForm : public QWidget
{
    Q_OBJECT

public:
    MainForm()
    {
        model = new QSqlRelationalTableModel(this);
        model->setTable("Employee");
        model->setRelation(Employee_DepartmentId, QSqlRelation("Department", "id", "name"));
        model->select();

        view = new QTableView;
        view->setModel(model);
        view->setItemDelegate(new QSqlRelationalDelegate(this));

        addButton = new QPushButton("Add");
        connect(addButton, SIGNAL(clicked()), SLOT(addButtonClicked()));

        QVBoxLayout* mainLayout = new QVBoxLayout;
        mainLayout->addWidget(view);
        mainLayout->addWidget(addButton);
        setLayout(mainLayout);

        model->select();
    }

private slots:
    void addButtonClicked()
    {
        int row = model->rowCount();
        model->insertRow(row);
        QModelIndex index = model->index(row, Employee_DepartmentId);
        view->setCurrentIndex(index);
        view->edit(index);
    }

private:
    QPushButton*                addButton;
    QTableView*                 view;
    QSqlRelationalTableModel*   model;
};

So about bug... After editing the cell which contains foreign keys the displayed value becomes wrong. So for example after change of department it is displayed as integer rather than string. Why? Is it Qt bug?

After adding a new row the same problem.

1

There are 1 answers

0
cyberbudy On

Try to add model->setEditStrategy(QSqlTableModel::OnManualSubmit)