#ifdef _MSC_VER #pragma execution_character_set("utf-8") // Qt VS 中文兼容(UTF-8) #endif #include "PersonListForm.h" #include "ui_PersonListForm.h" #include <QDebug> PersonListForm::PersonListForm(QWidget *parent) : QWidget(parent), ui(new Ui::PersonListForm) { ui->setupUi(this); // 1. 初始化 QTableView QStringList tbLabels; tbLabels << "人员Id" << "姓名" << "部门" << ""; QStandardItemModel * tbModel = new QStandardItemModel(this); tbModel->setHorizontalHeaderLabels(tbLabels); // 项选择模型 QItemSelectionModel * oldModel = ui->tableView->selectionModel(); // We recommend that you delete the old selection model if it is no longer required. ui->tableView->setModel(tbModel); if (oldModel != nullptr) { oldModel->deleteLater(); // 手动删除 } // 调用 setColumnWidth() 设置列宽 ---- 在 setModel() 之后 ui->tableView->setColumnWidth(0, 200); ui->tableView->setColumnWidth(1, 180); ui->tableView->setColumnWidth(2, 160); } PersonListForm::~PersonListForm() { delete ui; } void PersonListForm::initPersonListDialog() { current = 1; caculatePage(); // 最后,查询数据库,并在界面显示 loadPersonListFromDatabase(); } void PersonListForm::loadPersonListFromDatabase() { QVector<QVariantMap> personList = personDao.findRecordsByNameAndDept(ui->inputName->text(), "", limit, offset); // 2. 界面表格显示 QStandardItemModel * tbModel = dynamic_cast<QStandardItemModel *>(ui->tableView->model()); // 获取 已绑定的数据模型 QStandardItemModel if (tbModel == nullptr) { return; } if (tbModel->rowCount() > 0) { tbModel->removeRows(0, tbModel->rowCount()); // 删除所有行 } for (QVariantMap person : personList) { QList<QStandardItem *> row; QStandardItem * idItem = new QStandardItem(person.value("id").toString()); idItem->setTextAlignment(Qt::AlignCenter); idItem->setEditable(false); row.append(idItem); QStandardItem * nameItem = new QStandardItem(person.value("name").toString()); nameItem->setTextAlignment(Qt::AlignCenter); nameItem->setEditable(false); row.append(nameItem); QStandardItem * deptItem = new QStandardItem(person.value("deptname").toString()); deptItem->setTextAlignment(Qt::AlignCenter); deptItem->setEditable(false); row.append(deptItem); tbModel->appendRow(row); } } void PersonListForm::caculatePage() { quint16 total = personDao.findRecordCountByNameAndDept(ui->inputName->text(), ""); page = total % limit == 0 ? (total / limit) : (total / limit + 1); offset = (current - 1) * limit; ui->labelPage->setText(QString("%1 / %2").arg(current).arg(page)); } void PersonListForm::on_tableView_doubleClicked(const QModelIndex &index) { int row = index.row(); QStandardItemModel * tbModel = dynamic_cast<QStandardItemModel *>(ui->tableView->model()); // 获取 已绑定的数据模型 QStandardItemModel QList<QStandardItem *> colsInRow = tbModel->takeRow(row); QString personId = colsInRow.at(0)->text(); QString name = colsInRow.at(1)->text(); QString deptName = colsInRow.at(2)->text(); emit personRecordSelected(personId, name, deptName); } void PersonListForm::on_btnFirst_clicked() { current = 1; caculatePage(); loadPersonListFromDatabase(); } void PersonListForm::on_btnPrev_clicked() { current -= 1; if (current < 1) current = 1; caculatePage(); loadPersonListFromDatabase(); } void PersonListForm::on_btnNext_clicked() { current += 1; if (current > page) current = page; caculatePage(); loadPersonListFromDatabase(); } void PersonListForm::on_btnLast_clicked() { current = page; caculatePage(); loadPersonListFromDatabase(); } void PersonListForm::on_btnQuery_clicked() { current = 1; caculatePage(); loadPersonListFromDatabase(); }