将数据添加到数据库

时间:2015-06-29 08:23:08

标签: c++ qt qt5

在向数据库添加数据后如何重新加载qabstract项模型。所以我想要的是当我设法将数据添加到数据库时,QAbstractItemModel正在刷新或重新加载新数据。但在这种情况下,我成功地将数据添加到数据库并且树视图不更新,因此要更新此数据,我必须关闭并再次打开窗口小部件。请帮我看一下我应该在文件teachersmodel.cpp上写一行(//将数据添加到数据库后刷新代理模型)? 的 teachersmodel.cpp

#include <QtWidgets>
#include <QDebug>
#include <iostream>
#include "mainwindow/teacherspage/teachersmodel.h"

using namespace std;
using namespace mongo;

TeachersModel::TeachersModel()
{
    int countRow = 0;
}

QDate TeachersModel::DateOfBirth(const BSONElement &datebirth)
{
    return QDate(QDateTime::fromTime_t(datebirth.date()/1000).date());
}

void TeachersModel::AddTeacher(QAbstractItemModel* model,
                               const QString &induk_no,
                               const QString &name,
                               const QString &phone,
                               const QDate &datebirth,
                               const QString &sex,
                               const QString &certificate,
                               const QString &position,
                               const QString &teach,
                               const QString &fieldofstudy,
                               const QString &id)
{
    model->insertRow(0);
    model->setData(model->index(0, 0), induk_no);
    model->setData(model->index(0, 1), name);
    model->setData(model->index(0, 2), phone);
    model->setData(model->index(0, 3), datebirth);
    model->setData(model->index(0, 4), sex);
    model->setData(model->index(0, 5), certificate);
    model->setData(model->index(0, 6), position);
    model->setData(model->index(0, 7), teach);
    model->setData(model->index(0, 8), fieldofstudy);
    model->setData(model->index(0, 9), id);
}

QAbstractItemModel* TeachersModel::CreateTeacherModel(QObject* parent)
{
    QStandardItemModel *model = new QStandardItemModel(0, 10, parent);

    model->setHeaderData(0, Qt::Horizontal, QObject::tr("No Induk"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Nama"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Telepon"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("Tanggal Lahir"));
    model->setHeaderData(4, Qt::Horizontal, QObject::tr("Jenis Kelamin"));
    model->setHeaderData(5, Qt::Horizontal, QObject::tr("Ijazah"));
    model->setHeaderData(6, Qt::Horizontal, QObject::tr("Jabatan"));
    model->setHeaderData(7, Qt::Horizontal, QObject::tr("Mengajar"));
    model->setHeaderData(8, Qt::Horizontal, QObject::tr("Bidang Studi"));
    model->setHeaderData(9, Qt::Horizontal, QObject::tr("Id"));

    #ifdef Q_OS_WIN
        client::initialize();
    #endif // Q_OS_WIN

    c.connect("localhost");
    unique_ptr<DBClientCursor> cursor = c.query("sia.teachers", BSONObj());
    while (cursor->more()) {
        BSONObj p = cursor->next();
        AddTeacher(model, p.getStringField("induk_no"),
                   p.getStringField("name"),
                   p.getStringField("phone"),
                   DateOfBirth(p.getField("datebirth")),
                   p.getStringField("sex"),
                   p.getStringField("certificate"),
                   p.getStringField("position"),
                   p.getStringField("teach"),
                   p.getStringField("fieldofstudy"),
                   QString::fromStdString(p["_id"].OID().toString()));
        }
    return model;
}

void TeachersModel::SaveTeacher(const QString &induk_no,
                                const QString &name,
                                const QString &phone,
                                const QDate &datebirth,
                                const QString &sex,
                                const QString &certificate,
                                const QString &position,
                                const QString &teach,
                                const QString &fieldofstudy)
{
    #ifdef Q_OS_WIN
        client::initialize();
    #endif // Q_OS_WIN

    c.connect("localhost");
    BSONObjBuilder b;
    b.append("induk_no", induk_no.toStdString());
    b.append("name", name.toStdString());
    b.append("phone", phone.toStdString());
    b.appendDate("datebirth", QDateTime(datebirth).toMSecsSinceEpoch());
    b.append("sex", sex.toStdString());
    b.append("certificate", certificate.toStdString());
    b.append("position", position.toStdString());
    b.append("teach", teach.toStdString());
    b.append("fieldofstudy", fieldofstudy.toStdString());
    BSONObj p = b.obj();
    c.insert("sia.teachers", p);

    // refresh proxymodel after add data to database
}

TeachersModel::~TeachersModel() {}

这是调用teachersmodel.cpp的代码:
teachers.cpp

#include <iostream>
#include <exception>
#include "mainwindow/teacherspage/teachers.h"
#include "mainwindow/teacherspage/crud_teacher.h"

using namespace std;

Teachers::Teachers(QWidget* parent) : QWidget(parent)
{
    proxyModel_ = new QSortFilterProxyModel;

    proxyView_ = new QTreeView;
    proxyView_->setRootIsDecorated(false);
    proxyView_->setAlternatingRowColors(true);
    proxyView_->setModel(proxyModel_);
    proxyView_->setSortingEnabled(true);
    proxyView_->sortByColumn(1, Qt::AscendingOrder);

    filterPatternLineEdit_ = new QLineEdit;
    filterPatternLabel_ = new QLabel("Cari data:");

    filterColumnComboBox_ = new QComboBox;
    filterColumnComboBox_->addItem("No Induk");
    filterColumnComboBox_->addItem("Nama");
    filterColumnComboBox_->setFixedWidth(200);

    addButton_ = new QPushButton("Tambah");
    addButton_->setFixedWidth(100);
    editButton_ = new QPushButton("Edit");
    editButton_->setFixedWidth(100);
    deleteButton_ = new QPushButton("Hapus");
    deleteButton_->setFixedWidth(100);

    connect(filterPatternLineEdit_, SIGNAL(textChanged(QString)),
            this, SLOT(filterRegExpChanged()));
    connect(filterColumnComboBox_, SIGNAL(currentIndexChanged(int)),
            this, SLOT(filterColumnChanged()));
    connect(addButton_, SIGNAL(clicked()), this, SLOT(addTeacher()));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *filterLayout = new QHBoxLayout;
    QHBoxLayout *sourceLayout = new QHBoxLayout;

    filterLayout->addWidget(filterPatternLabel_, 1);
    filterLayout->addWidget(filterPatternLineEdit_, 2);
    filterLayout->addWidget(filterColumnComboBox_, 3);
    filterLayout->addWidget(addButton_, 4);
    filterLayout->addWidget(editButton_, 5);
    filterLayout->addWidget(deleteButton_, 6);
    sourceLayout->addWidget(proxyView_);

    mainLayout->addLayout(filterLayout);
    mainLayout->addLayout(sourceLayout);
    setLayout(mainLayout);

    teachersModel = new TeachersModel;
    try {
        SetSourceModel(teachersModel->CreateTeacherModel(this));
    } catch (exception& e) {
        qDebug() << "Caught " << e.what();
        QMessageBox::critical(this, "Error", e.what());
    }
}

void Teachers::filterRegExpChanged()
{
    QRegExp regExp(filterPatternLineEdit_->text(), Qt::CaseInsensitive, QRegExp::RegExp);
    proxyModel_->setFilterRegExp(regExp);
}

void Teachers::filterColumnChanged()
{
    proxyModel_->setFilterKeyColumn(filterColumnComboBox_->currentIndex());
}

void Teachers::addTeacher()
{
    CrudTeacher *crudTeacher = new CrudTeacher(this);
    crudTeacher->show();
}

void Teachers::SetSourceModel(QAbstractItemModel* model)
{
    proxyModel_->setSourceModel(model);
    proxyView_->setColumnHidden(9, true);
}


Teachers::~Teachers() {}

0 个答案:

没有答案