Recently, I saw MySQL browsing data paging in QT. Someone used qtablewidget to flip the page and manually added
Just wondering, since QT provides the view and model, why isn't it necessary?
A simple implementation, record it
First, re-inherit the qsqltablemodel, and modify the number of records during page turning.
Usersqltablemodel. h
# Ifndef define # define usersqltablemodel_h # include <qsqltablemodel> class usersqltablemodel: Public qsqltablemodel {q_objectpublic: explicit usersqltablemodel (qobject * parent = 0, qsqldatabase DB = qsqldatabase ()); void setstart (INT start) {This-> m_nstart = start;} int getstart () {return this-> m_nstart;} public: qvariant headerdata (INT section, QT :: orientation orientation, int role) const; private: int m_nstart; // The start ID of the Record Data}; # endif // usersqltablemodel_h
Usersqltablemodel. cpp
#include "usersqltablemodel.h"UserSqlTableModel::UserSqlTableModel(QObject *parent,QSqlDatabase db) : QSqlTableModel(parent,db){ m_nStart = 0;}QVariant UserSqlTableModel::headerData(int section, Qt::Orientation orient, int role) const{ if (orient == Qt::Vertical && role == Qt::DisplayRole) return section + m_nStart; return QSqlTableModel::headerData(section,orient,role);}
Declare several variables in the dialog box class:
PRIVATE: usersqltablemodel * usermodel; int m_ntotal; // total number of records int m_npagesize; // number of entries displayed on each page int m_ncurpagesize; // number of data entries on the current page int m_nstart; // number of start records
Initialization variable:
This-> m_nstart =-1; this-> m_npagesize = 6; this-> m_ncurpagesize = 0; this-> m_ntotal = 0; usermodel = new usersqltablemodel (this ); usermodel-> settable ("user"); usermodel-> setsort (user_name, QT: ascendingorder); usermodel-> setheaderdata (user_name, QT: horizontal, TR ("Account Number"); usermodel-> setheaderdata (user_groupchname, QT: horizontal, TR ("group"); usermodel-> setheaderdata (user_description, QT :: horizontal, TR ("Remarks"); userview-> setmodel (usermodel); userview-> setselectionmode (q1_actitemview: singleselection); userview-> setselectionbehavior (q1_actitemview: selectrows ); userview-> setcolumnhidden (user_id, true); userview-> setcolumnhidden (user_pwd, true); userview-> setcolumnhidden (user_group, true); userview-> resizecolumnstocontents (); userview-> horizontalheader ()-> setstretchlastsection (true); this-> m_ntotal = This-> gettotal (); on_pushbutton_first_clicked ();
Paging slot:
Void userdialog: on_pushbutton_first_clicked () // home page {If (this-> m_nstart = 0 & this-> m_ncurpagesize = This-> m_npagesize) return; this-> m_nstart = 0; this-> m_ncurpagesize = This-> m_ntotal> This-> m_npagesize? This-> m_npagesize: This-> m_ntotal; updateinfo ();} void userdialog: on_pushbutton_previus_clicked () // Previous Page {If (this-> m_nstart = 0 | this-> m_ntotal <= 0) return; this-> m_nstart-= This-> m_npagesize; this-> m_ncurpagesize = This-> m_npagesize; updateinfo ();} void userdialog: on_pushbutton_next_clicked () // next page {If (this-> m_ncurpagesize <this-> m_npagesize | this-> m_ntotal <= 0) return; this-> m_nstart + = This-> m_npagesize; this-> m_ncurpagesize = This-> m_ntotal-this-> m_nstart> This-> m_npagesize? This-> m_npagesize: This-> m_ntotal-this-> m_nstart; updateinfo ();} void userdialog: on_pushbutton_last_clicked () // The end page {If (this-> m_ncurpagesize <this-> m_npagesize | this-> m_ntotal <= 0) return; this-> m_ncurpagesize = This-> m_ntotal % This-> m_npagesize; this-> m_nstart = This-> m_ntotal-this-> m_ncurpagesize; updateinfo ();}
void UserDialog::updateInfo(){ QString strFilter = QString(" 1=1 limit %1,%2").arg(this->m_nStart).arg(this->m_nCurPageSize); userModel->setStart(this->m_nStart + 1); userModel->setFilter(strFilter); userModel->select();}
To sum up, there are two key points:
1. Reload qsqltablemodel and reset the number of records when turning pages
2. Set filter conditions for the model.
At that time, the direct setfilter ("limit;") never worked, but the direct execution of SQL statements with limit is no problem.
Finally, we can see this sentence in the manual.
The filter is a SQLWhereClause without the keywordWhere(For example,Name = 'nginx ').
That is to say, the filter condition is the condition statement where the WHERE clause is omitted.
Select * from table where limit; certainly not.
Add a permanent real condition.
Also pay attention to the last ';' If QT 2.3.0 is not used, the execution error will be reported:
D: \ QT \ 1010 \ tengxun \ fontsampler-Build-Desktop \ debug \ fontsampler.exe starting...
Assert: "idx> = 0 & idx <s" in file
.. \.. \ Include/qtcore /.. /.. /.. /.. /.. /.. /ndk_buildrepos/Qt-desktop/src/corelib/tools/qvarlengtharray. h, line 107