新增数据库翻页查询

master
feiyangqingyun 2019-10-12 17:01:25 +08:00
parent 1a270bd1a5
commit 4971dd5e30
12 changed files with 1182 additions and 1 deletions

View File

@ -18,3 +18,4 @@ Qt编写的一些开源的demo包括串口调试助手、网络调试助手
| 12 | framelesswidget | 通用无边框拖动拉伸类 |
| 13 | ipaddress | IP地址输入控件 |
| 14 | bgdemo | 无边框背景透明窗体 |
| 15 | dbpage | 通用数据库翻页查询 |

BIN
dbpage/TA.db Normal file

Binary file not shown.

487
dbpage/dbpage.cpp Normal file
View File

@ -0,0 +1,487 @@
#include "dbpage.h"
SqlQueryModel::SqlQueryModel(QObject *parent) : QSqlQueryModel(parent)
{
allCenter = false;
alignCenterColumn.clear();
alignRightColumn.clear();
}
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (allCenter) {
if(role == Qt::TextAlignmentRole ) {
value = Qt::AlignCenter;
}
} else {
//逐个从列索引中查找是否当前列在其中
int column = index.column();
bool existCenter = alignCenterColumn.contains(column);
bool existRight = alignRightColumn.contains(column);
if(role == Qt::TextAlignmentRole) {
if (existCenter) {
value = Qt::AlignCenter;
}
if (existRight) {
value = (QVariant)(Qt::AlignVCenter | Qt::AlignRight);
}
}
}
return value;
}
void SqlQueryModel::setAllCenter(bool allCenter)
{
this->allCenter = allCenter;
}
void SqlQueryModel::setAlignCenterColumn(const QList<int> &alignCenterColumn)
{
this->alignCenterColumn = alignCenterColumn;
}
void SqlQueryModel::setAlignRightColumn(const QList<int> &alignRightColumn)
{
this->alignRightColumn = alignRightColumn;
}
DbCountThread::DbCountThread(QObject *parent) : QThread(parent)
{
connName = "qt_sql_default_connection";
sql = "select 1";
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void DbCountThread::run()
{
//计算用时
QDateTime dtStart = QDateTime::currentDateTime();
QSqlQuery query(QSqlDatabase::database(connName));
query.exec(sql);
query.next();
int count = query.value(0).toUInt();
QDateTime dtEnd = QDateTime::currentDateTime();
double msec = dtStart.msecsTo(dtEnd);
emit receiveCount(count, msec);
}
void DbCountThread::setConnName(const QString &connName)
{
this->connName = connName;
}
void DbCountThread::setSql(const QString &sql)
{
this->sql = sql;
}
QScopedPointer<DbPage> DbPage::self;
DbPage *DbPage::Instance()
{
if (self.isNull()) {
QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new DbPage);
}
}
return self.data();
}
DbPage::DbPage(QObject *parent) : QObject(parent)
{
startIndex = 0;
tempSql = "";
sql = "";
queryModel = new SqlQueryModel;
pageCurrent = 1;
pageCount = 0;
resultCount = 0;
resultCurrent = 0;
labPageCount = 0;
labPageCurrent = 0;
labResultCount = 0;
labResultCurrent = 0;
labResult = 0;
labInfo = 0;
tableView = 0;
btnFirst = 0;
btnPre = 0;
btnNext = 0;
btnLast = 0;
countName = "*";
connName = "qt_sql_default_connection";
dbType = DbType_Sqlite;
pageCurrent = 0;
pageCount = 0;
resultCount = 0;
resultCurrent = 30;
tableName = "";
selectColumn = "*";
orderSql = "";
whereSql = "";
columnNames.clear();
columnWidths.clear();
insertColumnIndex = -1;
insertColumnName = "";
insertColumnWidth = 50;
}
void DbPage::bindData(const QString &columnName, const QString &orderColumn, const QString &tableName,
QComboBox *cbox, const QString &connName)
{
QSqlQuery query(QSqlDatabase::database(connName));
query.exec("select " + columnName + " from " + tableName + " order by " + orderColumn + " asc");
while (query.next()) {
cbox->addItem(query.value(0).toString());
}
}
void DbPage::bindData(const QString &columnName, const QString &orderColumn, const QString &tableName,
QList<QComboBox *> cboxs, const QString &connName)
{
QSqlQuery query(QSqlDatabase::database(connName));
query.exec("select " + columnName + " from " + tableName + " order by " + orderColumn + " asc");
while (query.next()) {
foreach (QComboBox *cbox, cboxs) {
cbox->addItem(query.value(0).toString());
}
}
}
void DbPage::bindData(const QString &sql)
{
queryModel->setQuery(sql, QSqlDatabase::database(connName));
tableView->setModel(queryModel);
//依次设置列标题列宽
int columnCount = tableView->model()->columnCount();
int nameCount = columnNames.count();
columnCount = columnCount > nameCount ? nameCount : columnCount;
QList<QString> columnNames = this->columnNames;
QList<int> columnWidths = this->columnWidths;
//根据设置添加新列,将对应新列的标题名称和宽度按照索引位置插
if (insertColumnIndex >= 0) {
columnCount++;
columnNames.insert(insertColumnIndex, insertColumnName);
columnWidths.insert(insertColumnIndex, insertColumnWidth);
queryModel->insertColumn(insertColumnIndex);
}
//设置列标题和列宽度
for (int i = 0; i < columnCount; i++) {
queryModel->setHeaderData(i, Qt::Horizontal, columnNames.at(i));
tableView->setColumnWidth(i, columnWidths.at(i));
}
if (labPageCurrent != 0) {
labPageCurrent->setText(QString("第 %1 页").arg(pageCurrent));
}
if (labPageCount != 0) {
labPageCount->setText(QString("共 %1 页").arg(pageCount));
}
if (labResultCount != 0) {
labResultCount->setText(QString("共 %1 条").arg(resultCount));
}
if (labResultCurrent != 0) {
labResultCurrent->setText(QString("每页 %1 条").arg(resultCurrent));
}
if (labInfo != 0) {
labInfo->setText(QString("共 %1 条 每页 %2 条 共 %3 页 第 %4 页").arg(resultCount).arg(resultCurrent).arg(pageCount).arg(pageCurrent));
}
//发送结果信号
emit receivePage(pageCurrent, pageCount, resultCount, resultCurrent);
}
void DbPage::slot_receiveCount(quint32 count, double msec)
{
if (labResult != 0) {
labResult->setText(QString("查询用时 %1 秒").arg(QString::number(msec / 1000, 'f', 3)));
}
resultCount = count;
int yushu = resultCount % resultCurrent;
//不存在余数,说明是整行,例如300%5==0
if (yushu == 0) {
if (resultCount > 0 && resultCount < resultCurrent) {
pageCount = 1;
} else {
pageCount = resultCount / resultCurrent;
}
} else {
pageCount = (resultCount / resultCurrent) + 1;
}
//2014-10-9增加翻页按钮可用不可用处理,如果只有一页数据,则翻页按钮不可用
if (pageCount <= 1) {
btnFirst->setEnabled(false);
btnLast->setEnabled(false);
btnNext->setEnabled(false);
btnPre->setEnabled(false);
} else {
btnFirst->setEnabled(true);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
btnPre->setEnabled(true);
}
tempSql = QString("select %1 from %2 %3 order by %4").arg(selectColumn).arg(tableName).arg(whereSql).arg(orderSql);
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent); //组织分页SQL语句
bindData(sql);
}
void DbPage::first()
{
if (pageCount > 1) {
startIndex = 0;
pageCurrent = 1;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
bindData(sql);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
}
btnFirst->setEnabled(false);
btnPre->setEnabled(false);
}
void DbPage::previous()
{
if (pageCurrent > 1) {
pageCurrent--;
startIndex -= resultCurrent;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
bindData(sql);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
}
if (pageCurrent == 1) {
btnFirst->setEnabled(false);
btnPre->setEnabled(false);
}
}
void DbPage::next()
{
if (pageCurrent < pageCount) {
pageCurrent++;
startIndex += resultCurrent;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
bindData(sql);
btnFirst->setEnabled(true);
btnPre->setEnabled(true);
}
if (pageCurrent == pageCount) {
btnLast->setEnabled(false);
btnNext->setEnabled(false);
}
}
void DbPage::last()
{
if (pageCount > 0) {
startIndex = (pageCount - 1) * resultCurrent;
pageCurrent = pageCount;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
bindData(sql);
btnFirst->setEnabled(true);
btnPre->setEnabled(true);
}
btnLast->setEnabled(false);
btnNext->setEnabled(false);
}
//设置显示数据的表格控件,当前翻页信息的标签控件等
void DbPage::setControl(QTableView *tableView,
QLabel *labPageCount, QLabel *labPageCurrent,
QLabel *labResultCount, QLabel *labResultCurrent,
QLabel *labResult, QLabel *labInfo,
QAbstractButton *btnFirst, QAbstractButton *btnPre,
QAbstractButton *btnNext, QAbstractButton *btnLast,
const QString &countName, const QString &connName)
{
this->tableView = tableView;
this->labPageCount = labPageCount;
this->labPageCurrent = labPageCurrent;
this->labResultCount = labResultCount;
this->labResultCurrent = labResultCurrent;
this->labResult = labResult;
this->labInfo = labInfo;
this->btnFirst = btnFirst;
this->btnPre = btnPre;
this->btnNext = btnNext;
this->btnLast = btnLast;
this->countName = countName;
this->connName = connName;
this->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
//挂载翻页按钮事件
connect(btnFirst, SIGNAL(clicked()), this, SLOT(first()));
connect(btnPre, SIGNAL(clicked()), this, SLOT(previous()));
connect(btnNext, SIGNAL(clicked()), this, SLOT(next()));
connect(btnLast, SIGNAL(clicked()), this, SLOT(last()));
}
void DbPage::setConnName(const QString &connName)
{
this->connName = connName;
}
void DbPage::setDbType(const DbPage::DbType &dbType)
{
this->dbType = dbType;
}
void DbPage::setTableName(const QString &tableName)
{
this->tableName = tableName;
}
void DbPage::setSelectColumn(const QString &selectColumn)
{
this->selectColumn = selectColumn;
}
void DbPage::setOrderSql(const QString &orderSql)
{
this->orderSql = orderSql;
}
void DbPage::setWhereSql(const QString &whereSql)
{
this->whereSql = whereSql;
}
void DbPage::setResultCurrent(int resultCurrent)
{
this->resultCurrent = resultCurrent;
}
void DbPage::setColumnNames(const QList<QString> &columnNames)
{
this->columnNames = columnNames;
}
void DbPage::setColumnWidths(const QList<int> &columnWidths)
{
this->columnWidths = columnWidths;
}
void DbPage::setAllCenter(bool allCenter)
{
queryModel->setAllCenter(allCenter);
}
void DbPage::setAlignCenterColumn(const QList<int> &alignCenterColumn)
{
queryModel->setAlignCenterColumn(alignCenterColumn);
}
void DbPage::setAlignRightColumn(const QList<int> &alignRightColumn)
{
queryModel->setAlignRightColumn(alignRightColumn);
}
void DbPage::setInsertColumnIndex(int insertColumnIndex)
{
this->insertColumnIndex = insertColumnIndex;
}
void DbPage::setInsertColumnName(const QString &insertColumnName)
{
this->insertColumnName = insertColumnName;
}
void DbPage::setInsertColumnWidth(int insertColumnWidth)
{
this->insertColumnWidth = insertColumnWidth;
}
void DbPage::select()
{
//重置开始索引
startIndex = 0;
pageCurrent = 1;
//假设只有一页
slot_receiveCount(resultCurrent, 0);
//全部禁用按钮,文本显示正在查询...
btnFirst->setEnabled(false);
btnLast->setEnabled(false);
btnNext->setEnabled(false);
btnPre->setEnabled(false);
QString info = "正在查询...";
if (labInfo != 0) {
labInfo->setText(info);
}
if (labPageCurrent != 0) {
labPageCurrent->setText(info);
}
if (labPageCount != 0) {
labPageCount->setText(info);
}
if (labResultCount != 0) {
labResultCount->setText(info);
}
if (labResultCurrent != 0) {
labResultCurrent->setText(info);
}
if (labResult != 0) {
labResult->setText(info);
}
//开始分页绑定数据前,计算好总数据量以及行数
tempSql = QString("select count(%1) from %2 %3").arg(countName).arg(tableName).arg(whereSql);
//采用线程执行查询复合条件的记录行数
DbCountThread *dbCountThread = new DbCountThread(this);
//绑定查询结果信号槽,一旦收到结果则立即执行
connect(dbCountThread, SIGNAL(receiveCount(quint32, double)), this, SIGNAL(receiveCount(quint32, double)));
connect(dbCountThread, SIGNAL(receiveCount(quint32, double)), this, SLOT(slot_receiveCount(quint32, double)));
//设置数据库连接名称和查询语句,并启动线程
dbCountThread->setConnName(connName);
dbCountThread->setSql(tempSql);
dbCountThread->start();
}

213
dbpage/dbpage.h Normal file
View File

@ -0,0 +1,213 @@
#ifndef DBPAGE_H
#define DBPAGE_H
/**
* :feiyangqingyun(QQ:517216493) 2017-1-15
* 1:
* 2:////
* 3:
* 4:,
* 5:线,
* 6:,///
* 7:
* 8:,,,
* 9:
*/
#include <QtGui>
#include <QtSql>
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
//自定义模型设置列居中和右对齐
class SqlQueryModel: public QSqlQueryModel
{
public:
explicit SqlQueryModel(QObject *parent = 0);
protected:
QVariant data(const QModelIndex &index, int role) const;
private:
bool allCenter; //所有居中
QList<int> alignCenterColumn; //居中对齐列
QList<int> alignRightColumn; //右对齐列
public:
//设置所有列居中
void setAllCenter(bool allCenter);
//设置居中对齐列索引集合
void setAlignCenterColumn(const QList<int> &alignCenterColumn);
//设置右对齐列索引集合
void setAlignRightColumn(const QList<int> &alignRightColumn);
};
//计算复合条件的记录总行数,以便分页
class DbCountThread : public QThread
{
Q_OBJECT
public:
explicit DbCountThread(QObject *parent = 0);
private:
QString connName; //数据库连接名称
QString sql; //要执行的查询语句
protected:
void run();
signals:
void receiveCount(quint32 count, double msec);
public slots:
//设置数据库连接名称
void setConnName(const QString &connName);
//设置要执行的查询语句
void setSql(const QString &sql);
};
class DbPage : public QObject
{
Q_OBJECT
public:
enum DbType {
DbType_Sqlite = 0, //sqlite数据库
DbType_MySql = 1, //mysql数据库
DbType_SqlServer = 3, //sqlserver数据库
DbType_Access = 4, //access数据库
DbType_PostgreSQL = 5 //postgresql数据库
};
static DbPage *Instance();
explicit DbPage(QObject *parent = 0);
//绑定数据到下拉框
static void bindData(const QString &columnName, const QString &orderColumn, const QString &tableName,
QComboBox *cbox, const QString &connName = "qt_sql_default_connection");
static void bindData(const QString &columnName, const QString &orderColumn, const QString &tableName,
QList<QComboBox *> cboxs, const QString &connName = "qt_sql_default_connection");
private:
static QScopedPointer<DbPage> self;
int startIndex; //分页开始索引,每次翻页都变动
QString tempSql; //临时SQL语句
QString sql; //sql语句
SqlQueryModel *queryModel; //查询模型
QLabel *labPageCount; //总页数标签
QLabel *labPageCurrent; //当前页标签
QLabel *labResultCount; //总记录数标签
QLabel *labResultCurrent; //每页记录数标签
QLabel *labResult; //显示查询用时标签
QLabel *labInfo; //总页数当前页总记录数每页记录数
QTableView *tableView; //显示数据的表格对象
QAbstractButton *btnFirst; //第一页按钮对象
QAbstractButton *btnPre; //上一页按钮对象
QAbstractButton *btnNext; //下一页按钮对象
QAbstractButton *btnLast; //末一页按钮对象
QString countName; //统计表行数用字段
QString connName; //所使用的数据库连接名
DbType dbType; //数据库类型
quint32 pageCurrent; //当前第几页
quint32 pageCount; //总页数
quint32 resultCount; //总记录数
quint32 resultCurrent; //每页显示记录数
QString tableName; //表名
QString selectColumn; //要查询的字段集合
QString orderSql; //排序语句
QString whereSql; //条件语句
QList<QString> columnNames; //列名集合
QList<int> columnWidths; //列宽集合
int insertColumnIndex; //插入的列的索引位置
QString insertColumnName; //插入的列的标题
int insertColumnWidth; //插入的列的宽度
private slots:
void first(); //第一页
void previous(); //上一页
void next(); //下一页
void last(); //末一页
private slots:
//绑定sql语句到表格
void bindData(const QString &sql);
//收到记录行数
void slot_receiveCount(quint32 count, double msec);
signals:
void receivePage(quint32 pageCurrent, quint32 pageCount, quint32 resultCount, quint32 resultCurrent);
void receiveCount(quint32 count, double msec);
public slots:
//设置需要显示数据的表格,数据翻页对应的按钮
void setControl(QTableView *tableView,
QLabel *labPageCount, QLabel *labPageCurrent,
QLabel *labResultCount, QLabel *labResultCurrent,
QLabel *labResult, QLabel *labInfo,
QAbstractButton *btnFirst, QAbstractButton *btnPre,
QAbstractButton *btnNext, QAbstractButton *btnLast,
const QString &countName, const QString &connName = "qt_sql_default_connection");
//设置数据库连接名称
void setConnName(const QString &connName);
//设置数据库类型
void setDbType(const DbType &dbType);
//设置要查询的表名
void setTableName(const QString &tableName);
//设置要查询的字段列名集合
void setSelectColumn(const QString &selectColumn);
//设置排序sql
void setOrderSql(const QString &orderSql);
//设置条件sql
void setWhereSql(const QString &whereSql);
//设置每页显示多少行数据
void setResultCurrent(int resultCurrent);
//设置列名称集合
void setColumnNames(const QList<QString> &columnNames);
//设置列宽度集合
void setColumnWidths(const QList<int> &columnWidths);
//设置所有列居中
void setAllCenter(bool allCenter);
//设置居中对齐列索引集合
void setAlignCenterColumn(const QList<int> &alignCenterColumn);
//设置右对齐列索引集合
void setAlignRightColumn(const QList<int> &alignRightColumn);
//设置插入的列的索引
void setInsertColumnIndex(int insertColumnIndex);
//设置插入的列的标题
void setInsertColumnName(const QString &insertColumnName);
//设置插入的列的宽度
void setInsertColumnWidth(int insertColumnWidth);
//执行查询
void select();
};
#endif // DBPAGE_H

33
dbpage/dbpage.pro Normal file
View File

@ -0,0 +1,33 @@
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-15T14:34:50
#
#-------------------------------------------------
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = dbpage
TEMPLATE = app
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = bin
PRECOMPILED_HEADER = head.h
INCLUDEPATH += $$PWD
CONFIG += warn_off
SOURCES += \
main.cpp \
dbpage.cpp \
frmdbpage.cpp
HEADERS += \
dbpage.h \
frmdbpage.h
FORMS += \
frmdbpage.ui

67
dbpage/frmdbpage.cpp Normal file
View File

@ -0,0 +1,67 @@
#include "frmdbpage.h"
#include "ui_frmdbpage.h"
#include "dbpage.h"
frmDbPage::frmDbPage(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbPage)
{
ui->setupUi(this);
this->initForm();
}
frmDbPage::~frmDbPage()
{
delete ui;
}
void frmDbPage::initForm()
{
columnNames.clear();
columnWidths.clear();
tableName = "LogInfo";
countName = "rowid";
columnNames.append("防区号");
columnNames.append("防区名称");
columnNames.append("设备IP");
columnNames.append("日志类型");
columnNames.append("事件内容");
columnNames.append("触发时间");
columnNames.append("告警详情");
columnNames.append("告警数据");
columnNames.append("告警图像");
columnWidths.append(70);
columnWidths.append(120);
columnWidths.append(120);
columnWidths.append(80);
columnWidths.append(150);
columnWidths.append(160);
columnWidths.append(160);
columnWidths.append(160);
columnWidths.append(160);
//设置需要显示数据的表格和翻页的按钮
dbPage = new DbPage(this);
//设置所有列居中显示
dbPage->setAllCenter(true);
dbPage->setControl(ui->tableMain, ui->labPageCount, ui->labPageCurrent, ui->labResultCount, ui->labResultCurrent, ui->labResult, 0,
ui->btnFirst, ui->btnPre, ui->btnNext, ui->btnLast, countName);
ui->tableMain->horizontalHeader()->setStretchLastSection(true);
ui->tableMain->verticalHeader()->setDefaultSectionSize(25);
}
void frmDbPage::on_btnSelect_clicked()
{
ui->labResult->setText("正在查询...");
//绑定数据到表格
QString sql = "where 1=1";
dbPage->setTableName(tableName);
dbPage->setOrderSql(QString("%1 %2").arg(countName).arg("asc"));
dbPage->setWhereSql(sql);
dbPage->setResultCurrent(20);
dbPage->setColumnNames(columnNames);
dbPage->setColumnWidths(columnWidths);
dbPage->select();
}

37
dbpage/frmdbpage.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef FRMDBPAGE_H
#define FRMDBPAGE_H
#include <QWidget>
class DbPage;
namespace Ui {
class frmDbPage;
}
class frmDbPage : public QWidget
{
Q_OBJECT
public:
explicit frmDbPage(QWidget *parent = 0);
~frmDbPage();
private:
Ui::frmDbPage *ui;
QList<QString> columnNames; //字段名集合
QList<int> columnWidths; //字段宽度集合
DbPage *dbPage; //数据库翻页类
QString tableName; //表名称
QString countName; //统计行数字段名称
private slots:
void initForm();
private slots:
void on_btnSelect_clicked();
};
#endif // FRMDBPAGE_H

296
dbpage/frmdbpage.ui Normal file
View File

@ -0,0 +1,296 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmDbPage</class>
<widget class="QWidget" name="frmDbPage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="opaqueResize">
<bool>false</bool>
</property>
<widget class="QTableView" name="tableMain">
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QFrame" name="frameBottom">
<property name="minimumSize">
<size>
<width>190</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>190</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QPushButton" name="btnSelect">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>查询</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPre">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>上一页</string>
</property>
<property name="iconSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnNext">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>下一页</string>
</property>
<property name="iconSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnFirst">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>第一页</string>
</property>
<property name="iconSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnLast">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>末一页</string>
</property>
<property name="iconSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labPageCount">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labResultCount">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labPageCurrent">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labResultCurrent">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labResult">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

8
dbpage/head.h Normal file
View File

@ -0,0 +1,8 @@
#include <QtCore>
#include <QtGui>
#include <QtSql>
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#pragma execution_character_set("utf-8")

38
dbpage/main.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "frmdbpage.h"
#include <QtGui>
#include <QtSql>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
//打开数据库,整个应用程序可用
QSqlDatabase dbConn = QSqlDatabase::addDatabase("QSQLITE");
dbConn.setDatabaseName(qApp->applicationDirPath() + "/TA.db");
if (dbConn.open()) {
qDebug() << "连接数据库成功!";
} else {
qDebug() << "连接数据库失败!";
}
frmDbPage w;
w.setWindowTitle("数据库分页示例(作者:517216493)");
w.show();
return a.exec();
}

1
dbpage/readme.txt Normal file
View File

@ -0,0 +1 @@
记得将源码下的TA.db复制到可执行文件同一目录

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB