personModel::personModel(QObject *parent, QSqlDatabase db) : baseModel(parent, db)
{
}
int personModel::rowCount(const QModelIndex &parent) const
{
int totalCount = Database::GetInstance()->getPersonCount(m_filter);
return qMin(m_pageSize, totalCount - (m_currentPage - 1) * m_pageSize);
}
int personModel::columnCount(const QModelIndex &parent) const
{
return 7;
}
QVariant personModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.column() == 5) {
if (role == Qt::EditRole || role == Qt::DecorationRole || role == Qt::DisplayRole) {
QByteArray imageData = QSqlTableModel::data(index, Qt::EditRole).toByteArray();
if (role == Qt::EditRole || role == Qt::DisplayRole) {
// 直接返回原始字节数据
return imageData;
}
if (!imageData.isEmpty()) {
QPixmap pixmap;
if (pixmap.loadFromData(imageData)) {
return pixmap.scaled(95, 95, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
}
}
}
return QVariant();
}
//编辑和显示模式确保模型可以拿到数据
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return QSqlTableModel::data(index, role);
}
return QVariant();
}
QVariant personModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return "序号";
case 1:
return "工号";
case 2:
return "姓名";
case 3:
return "性别";
case 4:
return "部门";
case 5:
return "照片";
case 6:
return "操作";
default:
return QVariant();
}
}
return QVariant();
}
void personModel::setPage(int page)
{
if (page != m_currentPage) {
beginResetModel();
m_currentPage = page;
m_filter = "limit" +
endResetModel();
}
}void PersonManagement::initUI()
{
setObjectName("PersonManagement");
m_personModel = new personModel(this, Database::GetInstance()->getDB());
m_personModel->setTable("personInfo");
m_personModel->select();
// 创建数据模型
m_personTable = new QTableView(this);
m_personTable->setModel(m_personModel);
QHeaderView* horizontalHeader = m_personTable->horizontalHeader();
// 设置除第五列外的所有列自由拉伸
for (int i = 0; i < m_personModel->columnCount(); ++i) {
if (i != 5 || i != 6) {
horizontalHeader->setSectionResizeMode(i, QHeaderView::Stretch);
}
}
// 设置第五列固定宽度
horizontalHeader->setSectionResizeMode(5, QHeaderView::Fixed);
m_personTable->setColumnWidth(5, 100);
// 设置第六列固定宽度
horizontalHeader->setSectionResizeMode(6, QHeaderView::Fixed);
m_personTable->setColumnWidth(6, 130);
// 其他设置保持不变
m_personTable->verticalHeader()->setVisible(false);
m_personTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_personTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_personTable->hideColumn(0);
m_personTable->verticalHeader()->setDefaultSectionSize(100);
// 创建搜索框
m_searchLineEdit = new QLineEdit(this);
m_searchLineEdit->setPlaceholderText("输入姓名搜索人员...");
m_searchLineEdit->setMinimumWidth(250);
// 使用 Qt 自带的搜索图标(兼容性版本)
QIcon searchIcon = style()->standardIcon(QStyle::SP_FileDialogContentsView);
QAction *searchAction = new QAction(searchIcon, "搜索", this);
m_searchLineEdit->addAction(searchAction, QLineEdit::TrailingPosition);
// 创建按钮
m_addPersonButton = new QPushButton("增加人员", this);
m_addPersonButton->setFixedSize(95, 30);
m_exportButton = new QPushButton("导出数据", this);
m_exportButton->setFixedSize(95, 30);
m_importButton = new QPushButton("导入数据", this);
m_importButton->setFixedSize(95, 30);
QPushButton *rename = new QPushButton("修改照片名", this);
connect(rename, &QPushButton::clicked, this, &PersonManagement::onrename);
// 按钮布局(添加间距和对齐)
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(m_searchLineEdit); // 搜索框居左
buttonLayout->addStretch(1); // 间距
buttonLayout->addWidget(m_addPersonButton);
buttonLayout->addSpacing(10); // 按钮间距
buttonLayout->addWidget(m_exportButton);
buttonLayout->addSpacing(10);
buttonLayout->addWidget(m_importButton);
buttonLayout->addWidget(rename);
buttonLayout->setContentsMargins(10, 10, 10, 10); // 布局边距
//分页
m_upPageBtn = new QPushButton("上一页", this);
m_upPageBtn->setFixedSize(80, 30);
m_downPageBtn = new QPushButton("下一页", this);
m_downPageBtn->setFixedSize(80, 30);
m_jumpBtn = new QPushButton("跳转", this);
m_jumpBtn->setFixedSize(80, 30);
m_currentPage = new QLabel("当前 页", this);
m_currentPage->setFixedSize(80, 30);
//跳转输入页
m_jumpBox = new QSpinBox(this);
m_jumpBox->setRange(1,999);
m_jumpBox->setFixedSize(50, 30);
QHBoxLayout *pageTurnLayout = new QHBoxLayout();
pageTurnLayout->addWidget(m_upPageBtn);
pageTurnLayout->addSpacing(35);
pageTurnLayout->addWidget(m_currentPage);
pageTurnLayout->addSpacing(35);
pageTurnLayout->addWidget(m_downPageBtn);
QHBoxLayout *pageJumpLayout = new QHBoxLayout();
pageJumpLayout->addWidget(m_jumpBox);
pageJumpLayout->addSpacing(10);
pageJumpLayout->addWidget(m_jumpBtn);
pageJumpLayout->setContentsMargins(5, 5, 5, 5); // 布局边距
QHBoxLayout *pageLayout = new QHBoxLayout();
pageLayout->addLayout(pageTurnLayout);
pageLayout->addLayout(pageJumpLayout);
// 主布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(m_personTable);
mainLayout->addLayout(pageLayout);
mainLayout->setContentsMargins(10, 10, 10, 10);
mainLayout->setSpacing(10);
qDebug() << " 1111111111 ";
// 设置操作委托到操作列
EditDeleteButtonDelegate *delegate = new EditDeleteButtonDelegate(this);
m_personTable->setItemDelegateForColumn(6, delegate);
m_totalRows = Database::GetInstance()->getPersonCount();
m_totalPages = (m_totalRows + m_pageSize - 1) / m_pageSize;
if (m_totalPages == 0) m_totalPages = 1; // 没有数据时显示第1页
m_jumpBox->setRange(1, m_totalPages);
qDebug() << " 000000000 " << m_totalRows << m_totalPages;
updatePage();
connect(delegate, &EditDeleteButtonDelegate::editClicked, this, [this](const QModelIndex &index) {
onEditButtonClicked(index);
});
connect(delegate, &EditDeleteButtonDelegate::deleteClicked, this, [this](const QModelIndex &index) {
onDelButtonClicked(index);
});
// 连接信号
connect(m_addPersonButton, &QPushButton::clicked, this, &PersonManagement::onAddPersonClicked);
connect(m_exportButton, &QPushButton::clicked, this, &PersonManagement::onExportClicked);
connect(m_importButton, &QPushButton::clicked, this, &PersonManagement::onImportClicked);
connect(searchAction, &QAction::triggered, this, &PersonManagement::onSearchClicked);
connect(m_searchLineEdit, &QLineEdit::returnPressed, this, &PersonManagement::onSearchClicked);
connect(m_upPageBtn, &QPushButton::clicked, this, &PersonManagement::onPrevPage);
connect(m_downPageBtn, &QPushButton::clicked, this, &PersonManagement::onNextPage);
connect(m_jumpBtn, &QPushButton::clicked, this, &PersonManagement::onJumpPage);
} int Database::getPersonCount(const QString &filter)
{
if (!db.isOpen()) {
if (!openDatabase()) {
return 0;
}
}
QReadLocker locker(&m_rwLock);
QSqlQuery query(db);
QString queryStr = "SELECT COUNT(*) FROM personInfo";
if (!filter.isEmpty()) {
queryStr += " WHERE " + filter;
}
if (query.exec(queryStr)) {
if (query.next()) {
return query.value(0).toInt();
}
} else {
qDebug() << query.lastError().text();
}
qDebug() <<"666666666666";
return 0;
}
基于上面的代码实现分页,我的数据比较多,分页这边要使用数据库查询进行限制,每页20行数据