Qt數(shù)據(jù)庫應用之實現(xiàn)通用數(shù)據(jù)庫分頁
一、前言
數(shù)據(jù)庫分頁展示,在所有的涉及到數(shù)據(jù)庫記錄的項目中都是需要的,除了簡單的設備信息表、用戶信息表這種很少幾條幾十條數(shù)據(jù)量的表除外,其余的日志記錄表等都需要分頁展示數(shù)據(jù),少量的數(shù)據(jù)可以滾動條下拉查看完,超過百條以上的一般建議要加上分頁的功能處理,不然用戶查看起來明顯不舒服,體驗不友好,最明顯的例子就是網(wǎng)頁的搜索結果,一次性展示上萬條,不僅是多余的增加了查詢的壓力,而且用戶往往只關注最前面的幾頁記錄,再比如查詢報警記錄,一般是按照時間降序排序,最近最新的報警記錄顯示在最前面,用戶最關心的也就是最前面的結果。
數(shù)據(jù)庫分頁一般是通過sql語句的limit去處理,根據(jù)用戶選擇的條件構建sql語句傳入數(shù)據(jù)庫執(zhí)行,每當單擊上一頁、下一頁、第一頁、末一頁等按鈕的時候,也是重新執(zhí)行了sql語句返回結果。最開始這個數(shù)據(jù)庫分頁類是將分頁按鈕都集中在一起,后面單獨拆分了出來,邏輯代碼一個類,界面單獨成分頁導航控件專門展示UI外觀,這個外觀可以設置各種顏色和樣式等。
數(shù)據(jù)庫通用翻頁類特點:
- 可設置每頁多少行記錄,自動按照設定的值進行分頁。
- 可設置要查詢的表名、字段集合、條件語句、排序語句。
- 可設置第一頁、上一頁、下一頁、末一頁、翻頁按鈕。
- 可設置當前頁、總頁數(shù)、總記錄數(shù)、每頁記錄數(shù)、查詢用時標簽頁。
- 多線程查詢總記錄數(shù),數(shù)據(jù)量巨大時候不會卡主界面。
- 建議條件字段用整型類型的主鍵,速度極快。
- 提供查詢結果返回信號,包括當前頁、總頁數(shù)、總記錄數(shù)、查詢用時等信息。
- 可設置所有列或者某一列對齊樣式例如居中或者右對齊。
- 可增加列用于標識該條記錄,設定列的位置、標題、寬度。
- 提供函數(shù)直接執(zhí)行第一頁、上一頁、下一頁、末一頁。
- 提供函數(shù)直接跳轉(zhuǎn)到指定頁。
- 根據(jù)是否第一頁、末一頁自動禁用對應的按鈕。
- 本控件是翻頁功能類,和翻頁控件navpage完美搭配,形成超級牛逼的翻頁控件。
分頁導航控件特點:
- 可設置頁碼按鈕的個數(shù)。
- 可設置字體大小。
- 可設置邊框圓角角度、大小、顏色。
- 可設置正常狀態(tài)背景顏色、文字顏色。
- 可識別懸停狀態(tài)背景顏色、文字顏色。
- 可設置按下狀態(tài)背景顏色、文字顏色。
- 可設置選中狀態(tài)背景顏色、文字顏色。
- 可設置導航位置居中對齊、左對齊、右對齊。
- 可設置是否顯示提示標簽控件。
- 自動計算總頁碼數(shù)顯示隱藏多余按鈕。
- 自動計算切換頁碼導航。
- 和分頁導航功能類無縫對接完美融合。
關于Qt數(shù)據(jù)庫相關開發(fā)的一些經(jīng)驗總結
二、功能特點
同時支持多種數(shù)據(jù)庫比如odbc、sqlite、mysql、postgresql、sqlserver、oracle、人大金倉等。
一個數(shù)據(jù)庫類即可管理本地數(shù)據(jù)庫通信,也支持遠程數(shù)據(jù)庫通信等。
數(shù)據(jù)庫線程支持執(zhí)行各種sql語句,包括單條和批量。
組件中的所有類打印信息、錯誤信息、執(zhí)行結果都信號發(fā)出去。
集成數(shù)據(jù)庫通用翻頁類(負責具體處理邏輯),搭配分頁導航控件(負責外觀),形成超級牛逼的翻頁控件。
集成數(shù)據(jù)庫自動清理類,設定最大記錄數(shù)后臺自動清理早期數(shù)據(jù)。
集成自定義委托類,支持復選框、文本框、下拉框、日期框、微調(diào)框、進度條等。
同時支持Qt4-Qt6,親測Qt4.6到Qt6.3任意版本,任意系統(tǒng)和編譯器。
本組件無故障 360天7乘24小時 運行在至少上萬個現(xiàn)場,商業(yè)級別品質(zhì)保證。
每個類都對應完整詳細的使用示例,注釋詳細,非常適合閱讀學習。
可以作為獨立的程序運行,比如自動清理早期數(shù)據(jù),同步數(shù)據(jù)到云端。
全部線程處理,不卡界面,自動重連數(shù)據(jù)庫。
普通測試情況,sqlite數(shù)據(jù)庫,數(shù)據(jù)庫發(fā)生器每秒鐘插入1000條記錄約0.003秒鐘,同時自動清理數(shù)據(jù)類每秒鐘刪除1000條記錄約0.13秒,不同線程互不干擾。
三、體驗地址
體驗地址:https://pan.baidu.com/s/15ZKAlptW-rDcNq8zlzdYLg 提取碼:uyes 文件名:bin_dbtool.zip
國內(nèi)站點:https://gitee.com/feiyangqingyun
國際站點:https://github.com/feiyangqingyun
四、效果圖

五、相關代碼
#include "frmdbpage.h"
#include "ui_frmdbpage.h"
#include "quihelper.h"
#include "dbconnthread.h"
#include "dbpage.h"
#include "dbdelegate.h"
frmDbPage::frmDbPage(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbPage)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmDbPage::~frmDbPage()
{
delete ui;
}
void frmDbPage::initForm()
{
ui->frame->setFixedWidth(AppConfig::RightWidth);
QUIHelper::initTableView(ui->tableView);
//實例化數(shù)據(jù)庫通信類
dbConn = new DbConnThread(this);
dbConn->setDbFlag("分頁");
//實例化翻頁類
dbPage = new DbPage(this);
//關聯(lián)查詢記錄總數(shù)
connect(dbConn, SIGNAL(receiveCount(QString, int, int)), dbPage, SLOT(receiveCount(QString, int, int)));
dbPage->setAllCenter(true);
dbPage->setRecordsPerpage(30);
dbPage->setWhereSql("where 1=1");
//分頁導航控件
connect(ui->navPage, SIGNAL(selectPage(int)), dbPage, SLOT(selectPage(int)));
connect(dbPage, SIGNAL(receivePage(quint32, quint32, quint32, quint32)),
ui->navPage, SLOT(receivePage(quint32, quint32, quint32, quint32)));
QLabel *labInfo = ui->navPage->getLabInfo();
QList<QPushButton *> btns = ui->navPage->getBtnAll();
dbPage->setControl(ui->tableView, ui->labPageTotal, 0, ui->labRecordsTotal, 0, ui->labSelectTime,
labInfo, btns.at(0), btns.at(1), btns.at(2), btns.at(3));
//采用系統(tǒng)默認的樣式并設置按鈕固定尺寸
ui->navPage->setShowStyle(false);
ui->navPage->setShowGoPage(true);
foreach (QPushButton *btn, btns) {
btn->setFixedWidth(50);
}
}
void frmDbPage::initConfig()
{
ui->cboxDbType->addItems(DbHelper::getDbType());
ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::DbType4));
connect(ui->cboxDbType, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtDbName->setText(AppConfig::DbName4);
connect(ui->txtDbName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtHostName->setText(AppConfig::HostName4);
connect(ui->txtHostName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtHostPort->setText(QString::number(AppConfig::HostPort4));
connect(ui->txtHostPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtUserName->setText(AppConfig::UserName4);
connect(ui->txtUserName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtUserPwd->setText(AppConfig::UserPwd4);
connect(ui->txtUserPwd, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtColumnName->setText(AppConfig::ColumnName4);
connect(ui->txtColumnName, SIGNAL(textChanged()), this, SLOT(saveConfig()));
ui->txtColumnWidth->setText(AppConfig::ColumnWidth4);
connect(ui->txtColumnWidth, SIGNAL(textChanged()), this, SLOT(saveConfig()));
ui->txtCountName->setText("LogID");
ui->txtOrderSql->setText("LogID asc");
}
void frmDbPage::saveConfig()
{
AppConfig::DbType4 = ui->cboxDbType->currentText();
AppConfig::DbName4 = ui->txtDbName->text();
AppConfig::HostName4 = ui->txtHostName->text();
AppConfig::HostPort4 = ui->txtHostPort->text().toInt();
AppConfig::UserName4 = ui->txtUserName->text();
AppConfig::UserPwd4 = ui->txtUserPwd->text();
AppConfig::ColumnName4 = ui->txtColumnName->toPlainText();
AppConfig::ColumnWidth4 = ui->txtColumnWidth->toPlainText();
AppConfig::writeConfig();
}
void frmDbPage::on_btnOpen_clicked()
{
if (ui->btnOpen->text() == "打開數(shù)據(jù)庫") {
DbInfo dbInfo;
dbInfo.connName = this->objectName();
dbInfo.dbName = AppConfig::DbName4;
dbInfo.hostName = AppConfig::HostName4;
dbInfo.hostPort = AppConfig::HostPort4;
dbInfo.userName = AppConfig::UserName4;
dbInfo.userPwd = AppConfig::UserPwd4;
QString dbType = AppConfig::DbType4.toUpper();
if (dbType == "SQLITE") {
dbInfo.dbName = DbHelper::getDbDefaultFile();
}
dbConn->setConnInfo(DbHelper::getDbType(dbType), dbInfo);
if (dbConn->openDb()) {
dbConn->start();
ui->btnOpen->setText("關閉數(shù)據(jù)庫");
} else {
QString error = dbConn->getDatabase().lastError().text();
QUIHelper::showMessageBoxError("打開數(shù)據(jù)庫失敗!\n" + error, 3);
}
} else {
dbConn->stop();
dbConn->closeDb();
ui->btnOpen->setText("打開數(shù)據(jù)庫");
dbPage->clear();
}
QTimer::singleShot(100, this, SLOT(getTables()));
QTimer::singleShot(1000, this, SLOT(on_btnDo_clicked()));
}
void frmDbPage::on_btnCopy_clicked()
{
//將數(shù)據(jù)庫設置參數(shù)一鍵粘貼過來
ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::LocalDbType));
ui->txtDbName->setText(AppConfig::LocalDbName);
ui->txtHostName->setText(AppConfig::LocalHostName);
ui->txtHostPort->setText(QString::number(AppConfig::LocalHostPort));
ui->txtUserName->setText(AppConfig::LocalUserName);
ui->txtUserPwd->setText(AppConfig::LocalUserPwd);
}
void frmDbPage::getTables()
{
if (!dbConn->getOk()) {
return;
}
//取出數(shù)據(jù)庫對應的表集合
QStringList tables = dbConn->getDatabase().tables();
ui->cboxTables->clear();
ui->cboxTables->addItems(tables);
if (tables.contains("LogInfo")) {
ui->cboxTables->setCurrentIndex(ui->cboxTables->findText("LogInfo"));
}
}
void frmDbPage::on_btnDo_clicked()
{
if (!dbConn->getOk()) {
return;
}
//表格字段 名稱+寬度
QList<QString> columnNames = AppConfig::ColumnName4.split(",");
QList<int> columnWidths;
QStringList listWidth = AppConfig::ColumnWidth4.split(",");
foreach (QString width, listWidth) {
columnWidths << width.toInt();
}
//具體函數(shù)解釋參見對應類頭文件
//不設置字段中文名和寬度也行,默認取數(shù)據(jù)庫字段名
dbPage->setColumnNames(columnNames);
dbPage->setColumnWidths(columnWidths);
dbPage->setDbType(AppConfig::DbType4);
dbPage->setConnName(dbConn->getDatabase().connectionName());
QString tableName = ui->cboxTables->currentText();
QString countName = ui->txtCountName->text();
QString orderSql = ui->txtOrderSql->text();
dbPage->setTableName(tableName);
dbPage->setOrderSql(orderSql);
dbPage->select();
dbConn->selectCount(tableName, countName, "where 1=1");
}
void frmDbPage::on_cboxTables_currentIndexChanged(int)
{
QString table = ui->cboxTables->currentText();
QSqlRecord records = dbConn->getDatabase().record(table);
ui->cboxRecords->clear();
for (int i = 0; i < records.count(); ++i) {
ui->cboxRecords->addItem(records.fieldName(i));
}
}
void frmDbPage::on_cboxRecords_currentIndexChanged(int)
{
QString record = ui->cboxRecords->currentText();
ui->txtCountName->setText(record);
ui->txtOrderSql->setText(record + " asc");
}
到此這篇關于Qt數(shù)據(jù)庫應用之實現(xiàn)通用數(shù)據(jù)庫分頁的文章就介紹到這了,更多相關Qt通用數(shù)據(jù)庫分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解析C++編程中virtual聲明的虛函數(shù)以及單個繼承
這篇文章主要介紹了C++編程中virtual聲明的虛函數(shù)以及單個繼承,剖析虛函數(shù)和單個基類所能夠繼承的成員,要的朋友可以參考下2016-01-01
c++中string類成員函數(shù)c_str()的用法
c_str()函數(shù)返回一個指向正規(guī)c字符串的指針,內(nèi)容和string類的本身對象是一樣的,通過string類的c_str()函數(shù)能夠把string對象轉(zhuǎn)換成c中的字符串的樣式2013-09-09
Matlab實現(xiàn)讀寫txt文件數(shù)據(jù)與進制轉(zhuǎn)換
這篇文章主要為大家詳細介紹了Matlab實現(xiàn)讀寫txt文件數(shù)據(jù)與進制轉(zhuǎn)換的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12

