保存到数据库时出现问题,QTableView + QSqlTableModel

时间:2018-12-30 09:36:24

标签: c++ postgresql qt

表本身会打开,可以在表内部进行编辑,但是更新时不会保存新数据。我尝试更改策略并点击 在表格的不同部分。问题不在于更新视图中的数据,而在于它们没有输入数据库。

请告诉我我可能是错的。 我使用了QTableView和QSqlTableModel

h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "cls_connectionform.h"
#include <QtSql>
#include <QtWidgets>
#include <string>
#include <QSqlTableModel>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent =nullptr);
    ~MainWindow();
private:
    QSqlDatabase db;
    QGridLayout layout;

    QTableView *view_students;

    QSqlQueryModel model_groups;
    QSqlQueryModel model_subjects;
    QSqlTableModel *model_students;

    QWidget* wgt_body;
    cls_connectionForm* connection_form;

signals:
    void need_update_view();
private slots:
    void slot_update_view();
    void slot_connect_to_serv();
    void slot_create_connect_form();

};

#endif // MAINWINDOW_H

cpp:

 #include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
   view_students= new QTableView();

    QMenu* pmnuFile = new QMenu("&Connect");
    pmnuFile->addAction("&Connect to server...", this, SLOT(slot_create_connect_form()), QKeySequence("CTRL+O"));
    menuBar()->addMenu(pmnuFile);

    connection_form = new cls_connectionForm();
    connection_form->dck_formConnection->setWindowTitle(("Connecting to database"));
    connect(connection_form->pbtnConnect,SIGNAL(clicked()), this, SLOT(slot_connect_to_serv()));


    menuBar()->addMenu(pmnuFAQ);
    db = QSqlDatabase::addDatabase("QPSQL");
model_students = new QSqlTableModel(nullptr, db);

//    layout.addWidget(view_students,1,2);

    QHBoxLayout* hLayout = new QHBoxLayout();

  qDebug()<<"Begin";
    layout.addLayout(hLayout,2,0,1,3);

    wgt_body =new QWidget();
    wgt_body->setLayout(&layout);
    setCentralWidget(wgt_body);

    view_students->setModel(model_students);


}

void MainWindow::slot_create_connect_form(){

    connection_form->dck_formConnection->show();

}

void MainWindow::slot_connect_to_serv(){

    db.setDatabaseName(connection_form->nameDB->text());
    db.setUserName(connection_form->nameUser->text());
    db.setHostName(connection_form->Host->text());
    db.setPort(connection_form->Port->text().toInt());
    db.setPassword(connection_form->Password->text());

    if (!db.open()) {
        qDebug()<<"db not open";
        QMessageBox* pmbx =
         new QMessageBox(QMessageBox::Warning,
         "Warning",
         "Error");
        pmbx->show();
        statusBar()->showMessage("Error with database", 3000);
     }
    else {statusBar()->showMessage("Bd is open", 3000);
        qDebug()<<"Bd is open" ;


model_students->setTable("name");
         model_students->setEditStrategy(QSqlTableModel::OnFieldChange);
         model_students->select();

         view_students->setModel(model_students);
         view_students->show();
      // emit need_update_view();

    }
    connection_form->dck_formConnection->close();
}

0 个答案:

没有答案