qt 上下页布局与代码规范

 转自:http://www.cnblogs.com/caomingongli/archive/2011/11/17/2252745.html
//声明本头文件宏   
#ifndef _DATABASE_H
#define _DATABASE_H
 
//包含头文件
#include <QSqlDatabase>
#include <QSqlQuery>
 
//**********************************************************************
// 函数: createDatabase
// 功能: 创建数据库
//*********************************************************************
static  bool  createDatabase()
{
   //添加数据
  QSqlDatabase database = QSqlDatabase::addDatabase( "QSQLITE" );
   //设置数据库名称
  database.setDatabaseName( "database.db" );
   //判断是否打开
   if (!database.open())
     //返回
     return  false ;
   //声明数据库查询对象
  QSqlQuery query;
   //创建表
  query.exec( "create table student(id int primary key, name vchar, sex vchar, age int, deparment vchar)" );
   //添加记录
  query.exec( "insert into student values(1,'张三1','男',20,'计算机')" );
  query.exec( "insert into student values(2,'李四1','男',19,'经管')" );
  query.exec( "insert into student values(3,'王五1','男',22,'机械')" );
  query.exec( "insert into student values(4,'赵六1','男',21,'法律')" );
  query.exec( "insert into student values(5,'小明1','男',20,'英语')" );
  query.exec( "insert into student values(6,'小李1','女',19,'计算机')" );
  query.exec( "insert into student values(7,'小张1','男',20,'机械')" );
  query.exec( "insert into student values(8,'小刚1','男',19,'经管')" );
  query.exec( "insert into student values(9,'张三2','男',21,'计算机')" );
  query.exec( "insert into student values(10,'张三3','女',20,'法律')" );
  query.exec( "insert into student values(11,'王五2','男',19,'经管')" );
  query.exec( "insert into student values(12,'张三4','男',20,'计算机')" );
  query.exec( "insert into student values(13,'小李2','男',20,'机械')" );
  query.exec( "insert into student values(14,'李四2','女',19,'经管')" );
  query.exec( "insert into student values(15,'赵六3','男',21,'英语')" );
  query.exec( "insert into student values(16,'李四2','男',19,'法律')" );
  query.exec( "insert into student values(17,'小张2','女',22,'经管')" );
  query.exec( "insert into student values(18,'李四3','男',21,'英语')" );
  query.exec( "insert into student values(19,'小李3','女',19,'法律')" );
  query.exec( "insert into student values(20,'王五3','女',20,'机械')" );
  query.exec( "insert into student values(21,'张三4','男',22,'计算机')" );
  query.exec( "insert into student values(22,'小李2','男',20,'法律')" );
  query.exec( "insert into student values(23,'张三5','男',19,'经管')" );
  query.exec( "insert into student values(24,'小张3','女',20,'计算机')" );
  query.exec( "insert into student values(25,'李四4','男',22,'英语')" );
  query.exec( "insert into student values(26,'赵六2','男',20,'机械')" );
  query.exec( "insert into student values(27,'小李3','女',19,'英语')" );
  query.exec( "insert into student values(28,'王五4','男',21,'经管')" );
   //返回
   return  true ;
}
 
#endif //假如未定义_DATABASE_H宏

 主窗口头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//**********************************************************************
// Copyright (c) 2011
// 迪斯特软件开发小组.
// 文件: mainwindow.h
// 内容:
// 历史:
//  序号  修改时间        修改人     修改内容
//  1   2011-11-17  Administrator   首次生成
//*********************************************************************
 
//声明本头文件宏   
#ifndef _MAINWINDOW_H
#define _MAINWINDOW_H
 
//包含头文件
#include <QWidget>
//类前向声明
class  QTableView;
class  QSqlQueryModel;
class  QLabel;
class  QLineEdit;
class  QPushButton;
 
//**********************************************************************
// 类名:  MyMainWindow
// 目的:  自定义窗口类
//*********************************************************************
class  MyMainWindow :  public  QWidget
{
   //宏声明
  Q_OBJECT
 
//成员函数
public :
   //构造函数
  MyMainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
   //析构函数
  ~MyMainWindow();
 
//私有槽函数
private  slots:
   void  OnPrevButtonClick();       //前一页按钮按下
   void  OnNextButtonClick();       //后一页按钮按下
   void  OnSwitchPageButtonClick();   //转到页按钮按下
 
//私有成员函数
private :
   void  CreateWindow();         //创建窗口
   void  SetTableView();         //设置表格
   int   GetTotalRecordCount();     //得到记录数
   int   GetPageCount();         //得到页数
   void  RecordQuery( int  limitIndex);  //记录查询
   void  UpdateStatus();       //刷新状态
   void  SetTotalPageLabel();  //设置总数页文本
 
//成员变量
private :
  QSqlQueryModel    *queryModel;       //查询模型
  QTableView    *tableView;       //数据表
  QLabel    *totalPageLabel;    //总数页文本
  QLabel    *currentPageLabel;     //当前页文本
  QLineEdit *switchPageLineEdit;  //转到页输入框
  QPushButton   *prevButton;       //前一页按钮
  QPushButton   *nextButton;       //下一页按钮
  QPushButton   *switchPageButton;     //转到页按钮
   int        currentPage;       //当前页
   int        totalPage;     //总页数
   int        totalRecrodCount;      //总记录数
   enum       {PageRecordCount = 5}; //每页显示记录数
};
 
#endif //假如未定义_MAINWINDOW_H宏

 主窗口源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//**********************************************************************
// Copyright (c) 2011
// 迪斯特软件开发小组.
// 文件: mainwindow.cpp
// 内容:
// 历史:
//  序号  修改时间        修改人     修改内容
//  1   2011-11-17  Administrator   首次生成
//*********************************************************************
 
//包含头文件
#include <QtGui>
#include <QRegExp>
#include <QSqlQueryModel>
#include "mainwindow.h"
 
//**********************************************************************
// 函数: MyMainWindow
// 功能: 构造函数
//*********************************************************************
MyMainWindow::MyMainWindow(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags)
{
   //设置窗口属性
  setMinimumSize(600,300);
  setWindowTitle( "分页查询示例" );
   //创建窗口
  CreateWindow();
   //设置表格
  SetTableView();
   //信号槽连接
  connect(prevButton,SIGNAL(clicked()), this ,SLOT(OnPrevButtonClick()));
  connect(nextButton,SIGNAL(clicked()), this ,SLOT(OnNextButtonClick()));
  connect(switchPageButton,SIGNAL(clicked()), this ,SLOT(OnSwitchPageButtonClick()));
}
 
//**********************************************************************
// 函数: ~MyMainWindow
// 功能: 析构函数
//*********************************************************************
MyMainWindow::~MyMainWindow()
{
}
 
//**********************************************************************
// 函数: CreateWindow
// 功能: 创建窗口
//*********************************************************************
void  MyMainWindow::CreateWindow()
{
   //操作布局
   QHBoxLayout *operatorLayout =  new   QHBoxLayout;
  prevButton =  new  QPushButton( "前一页" );
  nextButton =  new  QPushButton( "下一页" );
  switchPageButton =  new  QPushButton( "Go" );
  switchPageLineEdit =  new  QLineEdit;
  switchPageLineEdit->setFixedWidth(40);
  QLabel *switchPage =  new  QLabel( "转到第" );
  QLabel *page =  new  QLabel( "页" );
  operatorLayout->addWidget(prevButton);
  operatorLayout->addWidget(nextButton);
  operatorLayout->addWidget(switchPage);
  operatorLayout->addWidget(switchPageLineEdit);
  operatorLayout->addWidget(page);
  operatorLayout->addWidget(switchPageButton);
  operatorLayout->addWidget( new  QSplitter());
   //状态布局
  QHBoxLayout *statusLayout =  new  QHBoxLayout;
  totalPageLabel =  new  QLabel;
  totalPageLabel->setFixedWidth(70);
  currentPageLabel =  new  QLabel;
  currentPageLabel->setFixedWidth(70);
  statusLayout->addWidget(totalPageLabel);
  statusLayout->addWidget(currentPageLabel);
  statusLayout->addWidget( new  QSplitter());
   //设置表格属性
  tableView =  new  QTableView;
  tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
  tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
   //创建界面
   QVBoxLayout *mainLayout =  new   QVBoxLayout( this );
  mainLayout->addLayout(operatorLayout);
  mainLayout->addWidget(tableView);
  mainLayout->addLayout(statusLayout);
}
 
//**********************************************************************
// 函数: SetTableView
// 功能: 设置表格
//*********************************************************************
void  MyMainWindow::SetTableView()
{
   //声明查询模型
  queryModel =  new  QSqlQueryModel( this );
   //设置当前页
  currentPage = 1;
   //得到总记录数
  totalRecrodCount = GetTotalRecordCount();
   //得到总页数
  totalPage = GetPageCount();
   //刷新状态
  UpdateStatus();
   //设置总页数文本
  SetTotalPageLabel();
   //记录查询
  RecordQuery(0);
   //设置模型
  tableView->setModel(queryModel);
   //设置表格表头
  queryModel->setHeaderData(0,Qt::Horizontal, "编号" ); 
  queryModel->setHeaderData(1,Qt::Horizontal, "姓名" );
  queryModel->setHeaderData(2,Qt::Horizontal, "性别" );
  queryModel->setHeaderData(3,Qt::Horizontal, "年龄" );
  queryModel->setHeaderData(4,Qt::Horizontal, "院系" );
}
 
//**********************************************************************
// 函数: GetTotalRecordCount
// 功能: 得到记录数
//*********************************************************************
int  MyMainWindow::GetTotalRecordCount()
{
  queryModel->setQuery( "select * from student" );
   return  queryModel->rowCount();
}
 
//**********************************************************************
// 函数: GetPageCount
// 功能: 得到页数
//*********************************************************************
int  MyMainWindow::GetPageCount()
{
   return  (totalRecrodCount % PageRecordCount == 0) ? (totalRecrodCount / PageRecordCount) : (totalRecrodCount / PageRecordCount + 1);
}
 
//**********************************************************************
// 函数: RecordQuery
// 功能: 记录查询
//*********************************************************************
void  MyMainWindow::RecordQuery( int  limitIndex)
{
  QString szQuery = QString( "select * from student limit %1,%2" ).arg(QString::number(limitIndex)).arg(QString::number(PageRecordCount));
  queryModel->setQuery(szQuery);
}
 
//**********************************************************************
// 函数: UpdateStatus
// 功能: 刷新状态
//*********************************************************************
void  MyMainWindow::UpdateStatus()
{
   //设置当前页文本
  QString szCurrentText = QString( "当前第%1页" ).arg(QString::number(currentPage));
  currentPageLabel->setText(szCurrentText);
   //设置按钮是否可用
   if (currentPage == 1)
  {
    prevButton->setEnabled( false );
    nextButton->setEnabled( true );
  }
   else  if (currentPage == totalPage)
  {
    prevButton->setEnabled( true );
    nextButton->setEnabled( false );
  }
   else
  {
    prevButton->setEnabled( true );
    nextButton->setEnabled( true );
  }
}
 
//**********************************************************************
// 函数: SetTotalPageLabel
// 功能: 设置总数页文本
//*********************************************************************
void  MyMainWindow::SetTotalPageLabel()
{
  QString szPageCountText = QString( "总共%1页" ).arg(QString::number(totalPage));
  totalPageLabel->setText(szPageCountText);
}
 
//**********************************************************************
// 函数: OnPrevButtonClick
// 功能: 前一页按钮按下
//*********************************************************************
void  MyMainWindow::OnPrevButtonClick()
{
   int  limitIndex = (currentPage - 2) * PageRecordCount;
  RecordQuery(limitIndex);
  currentPage -= 1;
  UpdateStatus();
}
 
//**********************************************************************
// 函数: OnNextButtonClick
// 功能: 后一页按钮按下
//*********************************************************************
void  MyMainWindow::OnNextButtonClick()
{
   int  limitIndex = currentPage * PageRecordCount;
  RecordQuery(limitIndex);
  currentPage += 1;
  UpdateStatus();
}
 
//**********************************************************************
// 函数: OnSwitchPageButtonClick
// 功能: 转到页按钮按下
//*********************************************************************
void  MyMainWindow::OnSwitchPageButtonClick()
{
   //得到输入字符串
  QString szText = switchPageLineEdit->text();
   //数字正则表达式
  QRegExp regExp( "-?[0-9]*" );
   //判断是否为数字
   if (!regExp.exactMatch(szText))
  {
    QMessageBox::information( this , tr( "提示" ), tr( "请输入数字" ));
        return ;
  }
   //是否为空
   if (szText.isEmpty())
  {
    QMessageBox::information( this , tr( "提示" ), tr( "请输入跳转页面" ));
        return ;
  }
   //得到页数
   int  pageIndex = szText.toInt();
   //判断是否有指定页
   if (pageIndex > totalPage || pageIndex < 1)
  {
    QMessageBox::information( this , tr( "提示" ), tr( "没有指定的页面,请重新输入" ));
        return ;
  }
   //得到查询起始行号
   int  limitIndex = (pageIndex-1) * PageRecordCount;
   //记录查询
  RecordQuery(limitIndex);
   //设置当前页
  currentPage = pageIndex;
   //刷新状态
  UpdateStatus();
}

 

 主函数源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//**********************************************************************
// Copyright (c) 2011
// 迪斯特软件开发小组.
// 文件: main.cpp
// 内容:
// 历史:
//  序号  修改时间        修改人     修改内容
//  1   2011-11-17  Administrator   首次生成
//*********************************************************************
 
//包含头文件
#include <QTextCodec>
#include <QApplication>
#include "database.h"
#include "mainwindow.h"
 
//**********************************************************************
// 函数: main
// 功能: 主函数
//*********************************************************************
int  main( int  argc,  char  *argv[])
{
   //声明应用程序变量
  QApplication app(argc, argv);
   //设置中文显示
  QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
  QTextCodec::setCodecForCStrings(QTextCodec::codecForName( "GB2312" ));
   //创建数据库
   if (!createDatabase())
     //返回
     return  1;
   //创建窗口
  MyMainWindow window;
   //显示窗口
  window.show();
   //返回
   return  app.exec();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值