Код:
#include "documentdialog.h"
#include "mainwindow.h"
#include "ui_documentdialog.h"
DocumentDialog::DocumentDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::DocumentDialog)
{
ui->setupUi(this);
QSqlQuery qDok("SELECT * FROM tbl_vidy_dok");
while (qDok.next()) {
ui->comboBox_vid->addItem(qDok.value("VID_DOK").toString());
}
QSqlQuery qIzd("SELECT * FROM tbl_izdat");
while (qIzd.next()) {
ui->comboBox_izd->addItem(qIzd.value("IZDAT").toString());
}
mp = new QDataWidgetMapper(this);
model = new QSqlRelationalTableModel(this);
model->setTable("tbl_documents");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
model->setRelation(1,QSqlRelation("tbl_vidy_dok","ID_VID_DOK","VID_DOK"));
model->setRelation(2,QSqlRelation("tbl_izdat","ID_IZDAT","IZDAT"));
mp->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mp->setModel(model);
mp->setItemDelegate(new QSqlRelationalDelegate(mp));
mp->addMapping(ui->comboBox_vid, 1);
mp->addMapping(ui->lineEdit_vhod,3);
mp->addMapping(ui->lineEdit_ish,5);
mp->addMapping(ui->comboBox_izd, 2);
mp->addMapping(ui->dateEdit_vhod,4);
mp->addMapping(ui->dateEdit_ish,6);
mp->addMapping(ui->plainTextEdit_rez,7);
connect(this,&DocumentDialog::rejected,this,&DocumentDialog::ready);
}
void DocumentDialog::setIdDok(int iddok)
{
idDok = iddok;
model->setFilter("ID_DOK = "+ QString::number(iddok));
model->select();
mp->toFirst();
}
void DocumentDialog::setIdMer(int idmer)
{
idMer = idmer;
}
int DocumentDialog::getIdDok()
{
return idDok;
}
int DocumentDialog::getIdMer()
{
return idMer;
}
DocumentDialog::~DocumentDialog()
{
delete ui;
}
void DocumentDialog::accept()
{
}
void DocumentDialog::ready()
{
QString tst = ui->comboBox_izd->currentText();
QString tst2 = ui->comboBox_vid->currentText();
mp->submit();
tst = ui->comboBox_izd->currentText();
tst2 = ui->comboBox_vid->currentText();
model->submitAll();
model->select();
}