2017-08-27 105 views
0

我做了這個簡單的對話框來顯示我遇到的問題,對話框只是一個QTableViewQSqlRelationalTableModel。問題是我無法保存視圖,即使數據更改不是任何相關表。但是如果我註釋掉關係,就像下面的代碼一樣,我可以更新沒有問題。當有外鍵在場時是否有額外的步驟來保存信息?更新關係表視圖

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" 

回答

-1

您還可以提供您的表格定義嗎?這是SQLITE的一個最基本的工作示例。

mainwindow.cpp:

#include "mainwindow.hpp" 
#include "ui_mainwindow.h" 

#include <QSqlDatabase> 
#include <QSqlRelationalTableModel> 
#include <QSqlRelationalDelegate> 
#include <QSqlQuery> 
#include <QSqlError> 
#include <QFile> 
#include <QMessageBox> 

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

    QString fileName = "test.sqlite"; 
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); 
    db.setDatabaseName(fileName); 
    // This part is just to have sample data ... 
    if (!QFile(fileName).exists()) { 
     db.open(); 

     QSqlQuery query; 
     query.exec("DROP TABLE SUPPLIER"); 
     query.exec("DROP TABLE USER"); 

     query.exec("CREATE TABLE USER(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(40) NOT NULL)"); 
     query.exec("CREATE TABLE SUPPLIER(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(40) NOT NULL, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES USER)"); 

     query.exec("INSERT INTO USER(name) VALUES('USER_1'), ('USER_2'), ('USER_3')"); 
     query.exec("INSERT INTO SUPPLIER(name, userId) VALUES('SUPPLIER_1', 1), ('SUPPLIER_2', 2), ('SUPPLIER_3', 3)"); 

     db.close(); 
    } 
    // End of part for sample data. 

    QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this); 
    model->setTable("SUPPLIER"); 
    model->setEditStrategy(QSqlTableModel::OnManualSubmit); 
    model->setHeaderData(0,Qt::Horizontal,"ID"); 
    model->setHeaderData(1,Qt::Horizontal,"NAME"); 
    model->setRelation(2,QSqlRelation("USER", "id", "name")); 
    model->select(); 

    ui->tableView->setModel(model); 
    ui->tableView->setItemDelegate(new QSqlRelationalDelegate(this)); 

    connect(ui->pushButton, &QPushButton::clicked, [=]() { 
     model->submitAll(); 
    }); 
} 

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

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>MainWindow</class> 
<widget class="QMainWindow" name="MainWindow"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>300</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>MainWindow</string> 
    </property> 
    <widget class="QWidget" name="centralWidget"> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QTableView" name="tableView"/> 
    </item> 
    <item> 
    <widget class="QPushButton" name="pushButton"> 
     <property name="text"> 
     <string>Commit</string> 
     </property> 
    </widget> 
    </item> 
    </layout> 
    </widget> 
    <widget class="QMenuBar" name="menuBar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>22</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QToolBar" name="mainToolBar"> 
    <attribute name="toolBarArea"> 
    <enum>TopToolBarArea</enum> 
    </attribute> 
    <attribute name="toolBarBreak"> 
    <bool>false</bool> 
    </attribute> 
    </widget> 
    <widget class="QStatusBar" name="statusBar"/> 
</widget> 
<layoutdefault spacing="6" margin="11"/> 
<resources/> 
<connections/> 
</ui> 

希望幫助!

問候

+0

顯示的.ui文件是沒有必要的,在OP在談論如何與QSqlRelationalTableModel做到這一點,利用QSqlQuery是這樣做的一個老式的方法,因爲它需要上千行的,而不是幾十個,如果你正確使用QSqlRelationalTableModel。 – eyllanesc

+0

請閱讀以下內容以改進您的答案。 [我如何寫出一個好的答案?](https://stackoverflow.com/help/how-to-answer) – eyllanesc

+0

數據庫是使用模型工具直接在MySql上創建的。'supplier',它只有一個主鍵有3個外鍵鏈接到OP中註釋掉的表。 – Dan3460