Vadim Zabolotniy
Vadim Zabolotniy

Reputation: 387

QSqlRelationalTableModel insertRecord doesn't work if call setRelation

editlistofcustomers.h

QSqlRelationalTableModel *RelationalModel;
TreeModel *CategoriesModel;
QSqlRecord Record;

editlistofcustomers.cpp EditListOfCustomers::EditListOfCustomers // constructor

RelationalModel = new QSqlRelationalTableModel(this, *SupportObj->GetDataBase());
RelationalModel->setTable(SupportObj->GetCustomersTableName());
RelationalModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
RelationalModel->select();
RelationalModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Название/имя покупателя"));
Record = RelationalModel->record();
RelationalModel->setRelation(2, QSqlRelation(SupportObj->GetCategoriesOfCustomersTableName(),
                         SupportObj->GetCategoriesOfCustomersPattern()[0].GetName(), // ID.
                         SupportObj->GetCategoriesOfCustomersPattern()[2].GetName())); // Name.
CategoriesModel = new TreeModel(QObject::tr("Категории"),
                SupportObj->GetCategoriesOfCustomersTableName(),
                SupportObj, this);


//Setup model view delegate.
ui->TV_ListOfCustomers->setModel(RelationalModel);
ui->TV_ListOfCustomers->setItemDelegate(new QSqlRelationalDelegate(ui->TV_ListOfCustomers));
ui->TV_ListOfCustomers->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->TV_ListOfCustomers->setSortingEnabled(true);

ui->TV_CategoryOfCustomer->setModel(CategoriesModel);
SupportObj->GetDataBase()->transaction()

EditListOfCustomers::AddCustomer

QString customerName = ui->LE_CustomerName->text();
if(!customerName.isEmpty())
{
    Record.setValue(SupportObj->GetCustomersPattern()[1].GetName(), QVariant(customerName)); // Name.
    int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
    Record.setValue(SupportObj->GetCustomersPattern()[2].GetName(), QVariant(categoryID)); // Category ID.
    Record.setValue(SupportObj->GetCustomersPattern()[3].GetName(), QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
    Record.setValue(SupportObj->GetCustomersPattern()[4].GetName(), QVariant(ui->LE_CustomerAddress->text())); // Address.
    Record.setValue(SupportObj->GetCustomersPattern()[5].GetName(), QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
    RelationalModel->insertRecord(-1, Record);
    if(!RelationalModel->submitAll())
    {
        QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
    }

    // Clear fields
    ui->LE_CustomerName->clear();
    ui->TV_CategoryOfCustomer->setCurrentIndex(QModelIndex());
    ui->LE_CustomerTelephoneNumbers->clear();
    ui->LE_CustomerAddress->clear();
    ui->TE_CustomerComment->clear();

    ui->LE_CustomerName->setFocus();
    ui->TV_ListOfCustomers->sortByColumn(0, Qt::AscendingOrder);
}

If comment "RelationalModel->setRelation( ... )" everything work fine: transaction, adding, deleting, commit or rollback. If uncomment nothing work, but show everything right(ID replaced by category name), but I can't insert new row using "insertRecord". I try this suggestion, but unsuccessfully.

Any suggestion?

Upvotes: 3

Views: 1641

Answers (2)

user2422819
user2422819

Reputation: 177

Had the same issue. This only needs to happen for the field that has a setRelation. Here's the solution for PyQt:

def insertRow(self):      
    rec = self.record()
    rec.setValue("id", "new id")
    rec.setValue("type", 1)
    rec.setValue("title", "new title")
    return self.insertRecord(-1, rec)
    self.submit()

Apologies. Wanted to comment, but don't yet have a reputation of 50+.

Upvotes: 3

Vadim Zabolotniy
Vadim Zabolotniy

Reputation: 387

The problem was in the column name. setRelation changes column name category_of_customer_id to category_of_customers_name_2.

The solution use void setValue(int index, const QVariant &val) instead of void setValue(const QString & name, const QVariant &val).

QSqlRecord record = RelationalModel->record();
record.setValue(1, QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
record.setValue(2, QVariant(categoryID)); // Category ID.
record.setValue(3, QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
record.setValue(4, QVariant(ui->LE_CustomerAddress->text())); // Address.
record.setValue(5, QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, record);
if(!RelationalModel->submitAll())
{
    QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}

Upvotes: 4

Related Questions