Got some troubles using QSqlRelationalTableModel and QSqlRelationalDelegate



  • Hi all!

    I am coding an application which works with MS Access database using ODBC for Windows.
    I use QSqlRelationalTableModel for editing my database. It shows all data correctly, but when i am trying to change a value of foreign key (which is shown correctly) and press at item needed, empty combobox appiers and value in the cell also dissapiers, when the cell loses the focus, value is shown again.

    I can't solve it by myself, so i am asking for help.

    Code of the class:
    @
    #include "databasedialog.h"
    #include "ui_databasedialog.h"

    #include <QDebug>
    #include <QSqlQuery>
    #include <QSqlDatabase>
    #include <QSqlRelationalDelegate>

    #include <QSqlError>

    DatabaseDialog::DatabaseDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DatabaseDialog)
    {
    ui->setupUi(this);

    setTablesList();
    
    ui->tableView->setItemDelegate(new QSqlRelationalDelegate(this));
    

    }

    DatabaseDialog::~DatabaseDialog()
    {
    delete ui;
    }

    void DatabaseDialog::setTablesList()
    {
    QSqlQuery query=QSqlQuery("Select * From Query1",
    QSqlDatabase::database("accessConnection"));

    while (query.next())
    {
        ui->cBoxDatabaseTable->addItem(query.value(0).toString());
    }
    

    }
    void DatabaseDialog::on_cBoxDatabaseTable_currentTextChanged(const QString &table)
    {
    refreshTableView(table);
    connect(ui->btnSave, SIGNAL(clicked()), ui->tableView->model(), SLOT(submitAll()));
    }

    void DatabaseDialog::refreshTableView(QString tableName)
    {
    QSqlRelationalTableModel *tableModel=new QSqlRelationalTableModel(this, QSqlDatabase::database("accessConnection"));

    tableModel->setTable(tableName);
    
    tableModel->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
    
    setRelations(tableModel);
    
    tableModel->select();
    
    ui->tableView->setModel(tableModel);
    
    ui->tableView->show();
    
    adjustTableSize();
    

    }

    void DatabaseDialog::setRelations(QSqlRelationalTableModel *tableModel)
    {
    if (tableModel->tableName()=="Групи")
    {
    tableModel->setRelation(2, QSqlRelation("Спеціальності", "ID", "[Назва спеціальності]"));
    }

    if (tableModel->tableName()=="Студенти")
    {
        tableModel->setRelation(3, QSqlRelation("Групи", "ID", "[Назва групи]"));
    }
    

    }

    void DatabaseDialog::adjustTableSize()
    {
    ui->tableView->resizeColumnsToContents();
    }

    void DatabaseDialog::on_btnExit_clicked()
    {
    this->accept();
    }

    void DatabaseDialog::on_btnAddRow_clicked()
    {
    ui->tableView->model()->insertRow(ui->tableView->model()->rowCount());
    }

    void DatabaseDialog::on_btnDeleteRow_clicked()
    {
    QItemSelectionModel *select = ui->tableView->selectionModel();

    auto selectedIndexList=select->selectedRows();
    
    foreach (QModelIndex selectedIndex, selectedIndexList)
    {
        ui->tableView->model()->removeRow(selectedIndex.row());
    }
    

    }

    @
    Sorry for my poor english, i haven't used it for a long time.




Log in to reply
 

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