当前位置:网站首页>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();
}
边栏推荐
- Player practice 18 xresample
- Lua tvalue structure
- 3. Hidden processes under the ring
- To SystemC Beginners: the first program
- Alibaba cloud development board haas510 responds to UART serial port instructions
- 1414. minimum number of Fibonacci numbers with sum K
- Word insert picture blocked by text
- After reading the question, you will point to offer 16 Integer power of numeric value
- Chapter IV expression
- Is Shell Scripting really a big technology?
猜你喜欢

Redis core configuration and advanced data types

动态搜索广告智能查找匹配关键字

对某热水软件的加密参数逆向

TestEngine with ID ‘junit-vintage‘ failed to discover tests

Player actual combat 22 to solve the problems of flower screen and Caton

Player practice 17 xvideowidget

Crack WinRAR to ad pop-up window

什么是自动出价?它的优势是什么?

Llvm pass-- virtual function protection

Dynamic search advertising intelligent search for matching keywords
随机推荐
1414. minimum number of Fibonacci numbers with sum K
Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
Player actual combat 25 unpacking module add close
QA of some high frequency problems in oauth2 learning
工具笔记 —— 常用自定义工具类(正则,随机数等)
Implementation and debug of process hiding under x64
How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem
Wait function in SystemC
Write policy of cache
How to use Android studio to create an Alibaba cloud Internet of things app
注重点击,追求更多用户进入网站,可以选择什么出价策略?
Socket model of punctual atom stm32f429 core board
Lua common built-in functions
Communication flow analysis
Codeforces Round #798 (Div. 2)(A~D)
OAuth2学习中的一些高频问题的QA
Design of PLC intelligent slave station based on PROFIBUS DP protocol
Chapter IV expression
SystemC learning materials
The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)