0
我做了這個簡單的對話框來顯示我遇到的問題,對話框只是一個QTableView
和QSqlRelationalTableModel
。問題是我無法保存視圖,即使數據更改不是任何相關表。但是如果我註釋掉關係,就像下面的代碼一樣,我可以更新沒有問題。當有外鍵在場時是否有額外的步驟來保存信息?更新關係表視圖
PS。以防萬一是重要的,我在網絡上運行一個MySql
服務器。
#include "xtest.h"
#include "ui_xtest.h"
XTest::XTest(QWidget *parent) : QDialog(parent), ui(new Ui::XTest)
{
ui->setupUi(this);
modelSuppliers=new QSqlRelationalTableModel(this);
modelSuppliers->setTable("supplier");
modelSuppliers->setEditStrategy(QSqlTableModel::OnManualSubmit);
modelSuppliers->setHeaderData(0,Qt::Horizontal,"ID");
modelSuppliers->setHeaderData(1,Qt::Horizontal,"Name");
modelSuppliers->setHeaderData(2,Qt::Horizontal,"Address");
modelSuppliers->setHeaderData(3,Qt::Horizontal,"Address1");
modelSuppliers->setHeaderData(4,Qt::Horizontal,"City");
modelSuppliers->setHeaderData(5,Qt::Horizontal,"User");
modelSuppliers->setHeaderData(6,Qt::Horizontal,"Country");
modelSuppliers->setHeaderData(7,Qt::Horizontal,"Sta/Reg");
modelSuppliers->setHeaderData(8,Qt::Horizontal,"Active");
// modelSuppliers->setRelation(5,QSqlRelation("user","iduser","email"));
// modelSuppliers->setRelation(6,QSqlRelation("country","idcountry","name"));
// modelSuppliers->setRelation(7,QSqlRelation("sta_reg","idsta_reg","name"));
modelSuppliers->select();
ui->suppliers->setModel(modelSuppliers);
ui->suppliers->resizeColumnsToContents();
ui->suppliers->resizeRowsToContents();
ui->suppliers->setItemDelegate(new QSqlRelationalDelegate(ui->suppliers));
ui->suppliers->show();
}
XTest::~XTest()
{
delete ui;
}
void XTest::on_save_clicked()
{
modelSuppliers->submitAll();
}
我一直在玩這一段時間,我發現了一些可能相關的東西。我將模型更新之前的信號連接到一個顯示行和QSqlRecord的函數。我做了兩次關係「用戶」激活的運行,另一次沒有激活。在第5行,在我看來,模型試圖用相關表的描述來更新記錄,而不是用密鑰。第二輪,更新的那個,在那個位置上有關鍵。這是QT的錯誤嗎?
2
QSqlRecord(9)
0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4"
1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other"
2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion"
3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd"
4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago"
5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "N/A"
6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2"
7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5"
8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
""
.
2
QSqlRecord(9)
0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4"
1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other"
2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion"
3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd"
4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago"
5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "1"
6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2"
7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5"
8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
顯示的.ui文件是沒有必要的,在OP在談論如何與QSqlRelationalTableModel做到這一點,利用QSqlQuery是這樣做的一個老式的方法,因爲它需要上千行的,而不是幾十個,如果你正確使用QSqlRelationalTableModel。 – eyllanesc
請閱讀以下內容以改進您的答案。 [我如何寫出一個好的答案?](https://stackoverflow.com/help/how-to-answer) – eyllanesc
數據庫是使用模型工具直接在MySql上創建的。'supplier',它只有一個主鍵有3個外鍵鏈接到OP中註釋掉的表。 – Dan3460