我有一个数据库驱动的 qt 应用程序,它显示客户列表,当您单击客户时,它会打开另一个窗口,允许您编辑客户详细信息并将其保存到数据库中。
这很好,但我想知道如何在另一个窗口中触发 tableView 的刷新。
在 Objective-C 中有一种叫做委托的东西,我觉得这很相似,但有人可以澄清这样做的正确方法吗?
谢谢。
患者名单:
#include "patientlist.h"
#include "ui_patientlist.h"
#include "database.h"
#include <QtSql>
#include <QtDebug>
PatientList::PatientList(QWidget *parent) :
QWidget(parent),
ui(new Ui::PatientList)
{
ui->setupUi(this);
QSqlQuery qry;
if(qry.exec("SELECT patient_id,firstname,lastname FROM patients")){
qDebug() << "Success";
while(qry.next()){
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);
QString label = QString(qry.value(0).toString());
itm->setText(0,label);
label = QString(qry.value(1).toString());
itm->setText(1,label);
label = QString(qry.value(2).toString());
itm->setText(2,label);
ui->treeWidget->addTopLevelItem(itm);
}
}
else
{
qDebug() << "Error";
}
connect(
ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT(OnMouseDoubleClicked(QTreeWidgetItem*, int))
);
}
PatientList::~PatientList()
{
delete ui;
}
void PatientList::OnMouseDoubleClicked(QTreeWidgetItem *item, int id)
{
int patient_id = item->text(0).toInt();
editPatient = new EditPatient();
editPatient->loadPatient(patient_id);
editPatient->show();
}
编辑病人:
#include "editpatient.h"
#include "ui_editpatient.h"
EditPatient::EditPatient(QWidget *parent) :
QWidget(parent),
ui(new Ui::EditPatient)
{
ui->setupUi(this);
}
EditPatient::~EditPatient()
{
delete ui;
}
void EditPatient::loadPatient(int patient_id)
{
clearForm();
patient = new Patient();
patient->Load(patient_id);
if(patient->getPatientId() > 0){
qDebug() << "Patient ID: " << patient->getPatientId();
ui->patientidInput->setText(QString::number(patient->getPatientId()));
ui->firstnameInput->setText(patient->getFirstname());
ui->lastnameInput->setText(patient->getLastName());
}
}
void EditPatient::clearForm()
{
ui->patientidInput->setText("");
ui->firstnameInput->setText("");
ui->lastnameInput->setText("");
}
void EditPatient::on_saveButton_clicked()
{
patient->setFirstname(ui->firstnameInput->text());
patient->setLastname(ui->lastnameInput->text());
if(patient){
patient->Save();
}
}