当前位置:网站首页>QT database realizes page turning function
QT database realizes page turning function
2022-06-12 14:19:00 【silence_ heart】
qt Use qsqltablemodel and tableview Control to operate and display the database , however qsqltablemodel Page flipping is not supported by itself .
however qsqltablemodel Of select Function will call selectStatement Statement returned by function , Query the database , That is, you can inherit qsqltablemodel class , Override the selectStatement Function to display a specific number of pages of data
Custom class mySqlTableModel Inherit qsqltablemodel
class mySqlTableModel : public QSqlTableModel
{
public:
mySqlTableModel();
QString selectStatement() const;
void totalRowCount();
void setSelectAllData(bool choose);
private:
int totalRows;
int currentPage;
bool selectAllData;
};
overwrite selectStatement function
QString mySqlTableModel::selectStatement()const{
QString strRet;
if(selectAllData){
return this->QSqlTableModel::selectStatement();
}else{
strRet+="select * from table_1 limit 5 offset "+QString::number((currentPage-1)*5);
}
return strRet;
}
Why add one selectAllData Variable , because QSqlTableModel Of filter The function will also use selectStatement function , To query the integrity of data , In the use of filter Function will selectAllData Assignment true , Use native selectStatement function
Statistics of the total number of rows
void mySqlTableModel::totalRowCount(){
QSqlQuery query;
query.exec("select * from table_1");
QSqlQueryModel *querymodel=new QSqlQueryModel();
querymodel->setQuery(query);
totalRows=querymodel->rowCount();
}
mySqlTableModel Page up and page down functions
// Page down
bool mySqlTableModel::pageDown(){
if(totalRows>currentPage*5){
currentPage++;
return true;
}
return false;
}
// Page up
bool mySqlTableModel::pageUp(){
if(currentPage>1){
currentPage--;
return true;
}
return false;
}
The main program uses mySqlTableModel Turn pages (model by mySqlTableModel Example )
// Page down
void MainWindow::on_pushButton_3_clicked()
{
model->pageDown();
model->setSelectAllData(false);
model->select();
}
// Page up
void MainWindow::on_pushButton_2_clicked()
{
model->pageUp();
model->setSelectAllData(false);
model->select();
}
边栏推荐
- 拆改廣告機---業餘解壓
- 通信流量分析
- NotePad 常用设置
- Word insert picture blocked by text
- Socket model of punctual atom stm32f429 core board
- 基于Profibus-DP协议的PLC智能从站设计
- Player practice 26 adding slider and window maximization
- 初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
- Detailed explanation of C language memset
- Des File Encryptor based on MFC framework
猜你喜欢

How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

Copy word content to excel and automatically divide it into multiple columns

Redis data deletion policy in 2022

2022版Redis数据删除策略

阿裏雲開發板HaaS510報送設備屬性

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

PostgreSQL14安装使用教程

En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre

Is Shell Scripting really a big technology?
随机推荐
Redis核心配置和高级数据类型
Player practice 17 xvideowidget
基于Profibus-DP协议的PLC智能从站设计
Programmer interview golden classic good question / interview question 01.05 Edit once
Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
What is automatic bidding? What are its advantages?
Player practice 20 audio thread and video thread
Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
对某热水软件的加密参数逆向
2000. reverse word prefix
Tcp/ip network communication knowledge record
Tlm/systemc: TLM socket binding problem
[early knowledge of activities] list of recent activities of livevideostack
WinDbg preview debug analysis createtoolhelp32snapshot
Bridging and net
动态搜索广告智能查找匹配关键字
Introduction to functions (inline functions and function overloading)
Three common methods of C language array initialization ({0}, memset, for loop assignment) and their principles
完美收官|详解 Go 分布式链路追踪实现原理
969. pancake sorting