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. Got some troubles using QSqlRelationalTableModel and QSqlRelationalDelegate
QtWS25 Last Chance

Got some troubles using QSqlRelationalTableModel and QSqlRelationalDelegate

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.0k Views
  • 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.
  • C Offline
    C Offline
    Chaleng
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chaleng
      wrote on last edited by
      #2

      I have made a screenshot also:
      !http://i052.radikal.ru/1311/0b/98ce41d78fc7.jpg(picture)!

      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