Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Page turning query in Qt General Database

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize page turning query in Qt general database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

In the process of programming with the combination of Qt and database, when there are many records, basically you need to turn pages to view records. One advantage of page turning is that it can reduce the pressure on tables that display data. There is no need to display all the records of database tables at once, and basically no one needs to display all records on one page at once, and the results searched by search engines are basically displayed by page flipping, so here comes the problem. Is there a general way to automatically turn pages just by passing in the table name and query conditions? the answer is yes, Qt's encapsulation of database operations is also quite perfect, and the display is also the same, so it is specially encapsulated into a class and can be used directly.

Main functions:

Automatically paginate data according to the set number of rows per page

Just pass in the table name / field collection / rows per page / page indicator button / text indication label

Provides a public static method to bind field data to a drop-down box

It is recommended that the condition field use a numeric primary key, which is extremely fast.

Increase the total number of qualified records queried by threads. When there is a large amount of data, it will not card the main interface.

Provide query result return signal, including current page / total number of pages / total number of records / query time

You can set all columns or a column alignment style, such as center or right alignment

Can be set to add a column, column position, title, width

You can set the collection of fields to query

2. Code idea void DbPage::bindData (const QString & sql) {queryModel- > setQuery (sql, QSqlDatabase::database (connName)); tableView- > setModel (queryModel); / / set column header column width int columnCount = tableView- > model ()-> columnCount (); int nameCount = columnNames.count (); columnCount = columnCount > nameCount? NameCount: columnCount; QList columnNames = this- > columnNames; QList columnWidths = this- > columnWidths; / / add a new column according to the setting, and insert the title name and width of the corresponding new column into if (insertColumnIndex > = 0) {columnCount++; columnNames.insert (insertColumnIndex, insertColumnName); columnWidths.insert (insertColumnIndex, insertColumnWidth); queryModel- > insertColumn (insertColumnIndex) } / / set column header and column width 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 ("page 1") .arg (pageCurrent));} if (labPageCount! = 0) {labPageCount- > setText (QString (of% 1) .arg (pageCount) } if (labResultCount! = 0) {labResultCount- > setText (QString ("% 1") .arg (resultCount));} if (labResultCurrent! = 0) {labResultCurrent- > setText (QString ("% 1 per page") .arg (resultCurrent)) } if (labInfo! = 0) {labInfo- > setText (QString) .arg (resultCount) .arg (resultCurrent) .arg (pageCount) .arg (pageCurrent);} / / send result signal emit receivePage (pageCurrent, pageCount, resultCount, resultCurrent) } void DbPage::slot_receiveCount (quint32 count, double msec) {if (labResult! = 0) {labResult- > setText (QString ("query time% 1 second") .arg (QString::number (msec / 1000, 'fags, 3));} resultCount = count; int yushu = resultCount% resultCurrent; / / there is no remainder, which means it is the whole line, for example, 300% 5 seconds 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 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% 2jie% 3;") .arg (tempSql) .arg (startIndex) .arg (resultCurrent); / / Organization pagination SQL statement 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);} III.

This is the end of the content of "how to realize the page turning query of Qt general database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report