1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update vegetables price

This commit is contained in:
tianduanrui 2017-11-20 20:08:21 +08:00
parent 16d0f82c4c
commit afe8df7ab9
9 changed files with 311 additions and 193 deletions

View File

@ -22,9 +22,9 @@ SUBDIRS = src/qqt.pro
#SUBDIRS += examples/tabwidgetexamples #SUBDIRS += examples/tabwidgetexamples
#SUBDIRS += examples/svgtest #SUBDIRS += examples/svgtest
#SUBDIRS += examples/treeviewexample #SUBDIRS += examples/treeviewexample
#SUBDIRS += examples/qrcodeexample
# #
SUBDIRS += examples/qrcodeexample SUBDIRS += examples/gumbo_query_test
#SUBDIRS += examples/gumbo_query_test
#can't support ios #can't support ios
#SUBDIRS += examples/qqtprintsupportexample #SUBDIRS += examples/qqtprintsupportexample

View File

@ -1,13 +1,21 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "QDebug" #include <GumboQueryDocument.h>
#include <GumboQuerySelection.h>
#include <GumboQueryNode.h>
#include <QStringList>
#include <QStringListIterator>
MainWindow::MainWindow(QWidget* parent) : MainWindow::MainWindow ( QWidget* parent ) :
QMainWindow(parent), QMainWindow ( parent ),
ui(new Ui::MainWindow) ui ( new Ui::MainWindow )
{ {
ui->setupUi(this); ui->setupUi ( this );
m_client = new QQtWebWorkClient ( this );
connect ( m_client, SIGNAL ( replyFinished ( QNetworkReply* ) ),
this, SLOT ( replyFinished ( QNetworkReply* ) ) );
QStringList vl; QStringList vl;
vl << "西葫芦"; vl << "西葫芦";
@ -35,13 +43,15 @@ MainWindow::MainWindow(QWidget* parent) :
vl << "蒜薹"; vl << "蒜薹";
vl << "豆角"; vl << "豆角";
vl << "茴香"; vl << "茴香";
ui->lw->addItems(vl); m_mapVege["西葫芦"] = "xihulu" ;
m_mapVege["土豆"] = "tudou";
ui->lw->addItems ( m_mapVege.keys() );
connect(ui->lw, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), connect ( ui->lw, SIGNAL ( currentItemChanged ( QListWidgetItem*, QListWidgetItem* ) ),
this, SLOT(currentItemChanged(QListWidgetItem*, QListWidgetItem*))); this, SLOT ( currentItemChanged ( QListWidgetItem*, QListWidgetItem* ) ) );
ui->lw->setCurrentRow(0); ui->lw->setCurrentRow ( 0 );
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -49,12 +59,106 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }
void MainWindow::currentItemChanged(QListWidgetItem* cur, QListWidgetItem* prev) void MainWindow::currentItemChanged ( QListWidgetItem* cur, QListWidgetItem* prev )
{ {
qDebug() << cur << prev; qDebug() << cur << prev;
if (!cur)
if ( !cur )
return; return;
QString url = QString("http://www.baidu.com/s?wd=%1%2").arg(cur->text()).arg("价格"); //QString url = QString ( "http://www.baidu.com/s?wd=%1%2" ).arg ( cur->text() ).arg ( "价格" );
QString url = QString ( "http://nong.gold600.com/cx?pz=%1&c=shandong" ).arg ( m_mapVege[cur->text()] );
m_client->sendGetRequest ( url );
}
void MainWindow::replyFinished ( QNetworkReply* reply )
{
//pline() << reply->readAll();
int nHttpCode = reply->attribute ( QNetworkRequest::HttpStatusCodeAttribute ).toInt(); //http返回码
if ( nHttpCode == 200 ) //成功
{
pline() << "success";
}
else
{
pline() << "fail";
return;
}
QByteArray resultContent = reply->readAll();
//pline() << QString ( resultContent );
/*用页面源文件的编码来进行解码 GB2312 or UTF-8*/
QTextCodec* pCodec = QTextCodec::codecForName ( "GB2312" );
QString strResult = pCodec->toUnicode ( resultContent );
//pline() << strResult;
QTextCodec* pCodec2 = QTextCodec::codecForName ( "GB2312" );
QByteArray strResult2 = pCodec2->fromUnicode ( strResult );
QString txt0 = strResult2;
//pline() << txt0;
QString cc = resultContent;
//pline() << cc.toLatin1().constData();
GumboQueryDocument doc;
doc.parse ( strResult.toLocal8Bit().constData() );
GumboQuerySelection s = doc.find ( "table" );
GumboQueryNode pNode = s.nodeAt ( 0 );
QString txt = QString::fromStdString ( pNode.text() );
//pline() << txt;
QStringList tList = txt.split ( "\n", QString::SkipEmptyParts );
for ( int i = 0; i < tList.count(); i++ )
{
QString txt = tList.at ( i );
txt = txt.trimmed();
if ( txt.isEmpty() )
{
tList.removeAt ( i );
i--;
continue;
}
tList[i] = txt;
}
/*
for ( QStringList::Iterator itor = tList.begin();
itor != tList.end(); itor++ )
{
QString txt = ( QString ) * itor;
if ( "" == txt.trimmed() )
{
tList.erase ( itor );
itor--;
continue;
}
*itor = txt.trimmed();
}
*/
ui->tb->clear();
ui->tb->setColumnCount ( 5 );
ui->tb->horizontalHeader()->setSectionResizeMode ( QHeaderView::ResizeToContents );
for ( int i = 0; i < tList.count(); )
{
ui->tb->setRowCount ( i / 5 + 1 );
for ( int j = 0; j < 5; j++ )
{
QString cc = tList.value ( i );
qDebug() << i / 5 << j << cc;
QTableWidgetItem* item = new QTableWidgetItem ( cc );
ui->tb->setItem ( i / 5, j, item );
i++;
}
}
} }

View File

@ -4,8 +4,10 @@
#include <QMainWindow> #include <QMainWindow>
#include <QListWidget> #include <QListWidget>
namespace Ui #include "qqtcore.h"
{ #include "qqtwebworkclient.h"
namespace Ui {
class MainWindow; class MainWindow;
} }
@ -14,13 +16,15 @@ class MainWindow : public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget* parent = 0); explicit MainWindow ( QWidget* parent = 0 );
~MainWindow(); ~MainWindow();
private slots: private slots:
void currentItemChanged(QListWidgetItem*, QListWidgetItem*); void currentItemChanged ( QListWidgetItem*, QListWidgetItem* );
void replyFinished ( QNetworkReply* reply );
private: private:
Ui::MainWindow* ui; Ui::MainWindow* ui;
QQtWebWorkClient* m_client;
QMap<QString, QString> m_mapVege;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>889</width> <width>647</width>
<height>444</height> <height>330</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -32,7 +32,13 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QTableWidget" name="tb"/> <widget class="QTableWidget" name="tb">
<column>
<property name="text">
<string>fff</string>
</property>
</column>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -41,7 +47,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>889</width> <width>647</width>
<height>22</height> <height>22</height>
</rect> </rect>
</property> </property>

View File

@ -28,7 +28,7 @@ QQtWebWorkClient::QQtWebWorkClient ( QObject* parent ) : QNetworkAccessManager (
this, SLOT ( networkSessionConnected() ) ); this, SLOT ( networkSessionConnected() ) );
} }
void QQtWebWorkClient::getRequest ( QString& strUrl ) void QQtWebWorkClient::sendGetRequest ( QString& strUrl )
{ {
QQtWebWorkSession* s0 = new QQtWebWorkSession ( this ); QQtWebWorkSession* s0 = new QQtWebWorkSession ( this );
@ -52,6 +52,7 @@ void QQtWebWorkClient::getRequest ( QString& strUrl )
} }
s0->m_pNetworkReply = get ( netRequest ); //发起get请求 s0->m_pNetworkReply = get ( netRequest ); //发起get请求
m_listWebWorkSession.push_back ( s0 );
/*下面关联信号和槽*/ /*下面关联信号和槽*/
/*下载完成后开始一阵一阵堆取数据*/ /*下载完成后开始一阵一阵堆取数据*/
@ -71,7 +72,7 @@ void QQtWebWorkClient::getRequest ( QString& strUrl )
s0->m_pTimer->start ( ); s0->m_pTimer->start ( );
} }
void QQtWebWorkClient::postRequest ( QString& strUrl ) void QQtWebWorkClient::sendPostRequest ( QString& strUrl )
{ {
//post //post
// QString strBody; //http body部分可封装参数信息 // QString strBody; //http body部分可封装参数信息
@ -101,9 +102,11 @@ void QQtWebWorkClient::finished ( QNetworkReply* reply )
if ( !s0 ) if ( !s0 )
return; return;
pline() << s0->m_pNetworkReply << s0->m_pTimer;
s0->m_pTimer->stop();
s0->m_pNetworkReply->deleteLater(); s0->m_pNetworkReply->deleteLater();
m_listWebWorkSession.removeOne ( s0 ); m_listWebWorkSession.removeOne ( s0 );
} }
void QQtWebWorkClient::authenticationRequired ( QNetworkReply* r, QAuthenticator* a ) void QQtWebWorkClient::authenticationRequired ( QNetworkReply* r, QAuthenticator* a )

View File

@ -17,27 +17,15 @@
* Multi New Protocol QWebSocket * Multi New Protocol QWebSocket
* need ssl * need ssl
*/ */
class QQtWebWorkSession : QObject class QQtWebWorkSession;
{
Q_OBJECT
public:
explicit QQtWebWorkSession ( QObject* parent ) :
QObject ( parent ) {}
virtual ~QQtWebWorkSession() {}
QNetworkReply* m_pNetworkReply; //封装请求返回信息
QTimer* m_pTimer; //请求超时计时器
QString m_strUrl; //记录当前请求的url
} ;
class QQTSHARED_EXPORT QQtWebWorkClient : public QNetworkAccessManager class QQTSHARED_EXPORT QQtWebWorkClient : public QNetworkAccessManager
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QQtWebWorkClient ( QObject* parent = 0 ); explicit QQtWebWorkClient ( QObject* parent = 0 );
void getRequest ( QString& strUrl ); void sendGetRequest ( QString& strUrl );
void postRequest ( QString& strUrl ); void sendPostRequest ( QString& strUrl );
//void installProtocol(); //void installProtocol();
//void uninstallProtocol(); //void uninstallProtocol();
@ -70,4 +58,17 @@ private:
QList<QQtWebWorkSession*> m_listWebWorkSession; QList<QQtWebWorkSession*> m_listWebWorkSession;
}; };
class QQtWebWorkSession : QObject
{
Q_OBJECT
public:
explicit QQtWebWorkSession ( QObject* parent ) :
QObject ( parent ) {}
virtual ~QQtWebWorkSession() {}
QNetworkReply* m_pNetworkReply; //封装请求返回信息
QTimer* m_pTimer; //请求超时计时器
QString m_strUrl; //记录当前请求的url
} ;
#endif // QQTWEBWORKCLIENT_H #endif // QQTWEBWORKCLIENT_H

View File

@ -4,11 +4,11 @@
#include "qqtsql.h" #include "qqtsql.h"
#include "qqt-qt.h" #include "qqt-qt.h"
QQTMPTableWidget::QQTMPTableWidget(QWidget* parent) : QQtMPTableWidget::QQtMPTableWidget ( QWidget* parent ) :
QWidget(parent), QWidget ( parent ),
ui(new Ui::QQTMPTableWidget) ui ( new Ui::QQtMPTableWidget )
{ {
ui->setupUi(this); ui->setupUi ( this );
m_db = newDatabaseConn(); m_db = newDatabaseConn();
@ -18,57 +18,57 @@ QQTMPTableWidget::QQTMPTableWidget(QWidget* parent) :
altColor = true; altColor = true;
resizeMode = QHeaderView::ResizeToContents; resizeMode = QHeaderView::ResizeToContents;
ui->btnJump->setFixedWidth(30); ui->btnJump->setFixedWidth ( 30 );
ui->btnLeft->setFixedWidth(30); ui->btnLeft->setFixedWidth ( 30 );
ui->btnRight->setFixedWidth(30); ui->btnRight->setFixedWidth ( 30 );
ui->btnLeftHead->setFixedWidth(30); ui->btnLeftHead->setFixedWidth ( 30 );
ui->btnRightHead->setFixedWidth(30); ui->btnRightHead->setFixedWidth ( 30 );
ui->leNum->setFixedWidth(40); ui->leNum->setFixedWidth ( 40 );
ui->btnJump->setHidden(true); ui->btnJump->setHidden ( true );
ui->leNum->setHidden(true); ui->leNum->setHidden ( true );
ui->btnRightHead->setHidden(true); ui->btnRightHead->setHidden ( true );
ui->btnRight->setHidden(true); ui->btnRight->setHidden ( true );
ui->btnLeftHead->setHidden(true); ui->btnLeftHead->setHidden ( true );
ui->btnLeft->setHidden(true); ui->btnLeft->setHidden ( true );
} }
QQTMPTableWidget::~QQTMPTableWidget() QQtMPTableWidget::~QQtMPTableWidget()
{ {
delete ui; delete ui;
} }
void QQTMPTableWidget::setDB(QString db) void QQtMPTableWidget::setDB ( QString db )
{ {
m_name = db; m_name = db;
setDatabaseName(m_db, m_name); setDatabaseName ( m_db, m_name );
} }
void QQTMPTableWidget::setTable(QString table) void QQtMPTableWidget::setTable ( QString table )
{ {
m_table = table; m_table = table;
} }
void QQTMPTableWidget::setRelation(int column, const QSqlRelation& relation) void QQtMPTableWidget::setRelation ( int column, const QSqlRelation& relation )
{ {
m_columnRelation.insert(column, relation); m_columnRelation.insert ( column, relation );
} }
void QQTMPTableWidget::query(QString filter) void QQtMPTableWidget::query ( QString filter )
{ {
ptime(); //3ms ptime(); //3ms
//high? //high?
QQTSleep(478); QQTSleep ( 478 );
QSqlQuery query(m_db); QSqlQuery query ( m_db );
query.exec(QString("select count(*) from %1").arg(m_table)); query.exec ( QString ( "select count(*) from %1" ).arg ( m_table ) );
int num = 0; int num = 0;
if (query.next()) if ( query.next() )
{ {
num = query.value(0).toInt(); num = query.value ( 0 ).toInt();
pline() << "record num:" << num; pline() << "record num:" << num;
} }
@ -76,7 +76,7 @@ void QQTMPTableWidget::query(QString filter)
int pageNum = 0; int pageNum = 0;
if (num % m_numPerPage > 0) if ( num % m_numPerPage > 0 )
pageNum = num / m_numPerPage + 1; pageNum = num / m_numPerPage + 1;
else else
pageNum = num / m_numPerPage; pageNum = num / m_numPerPage;
@ -90,7 +90,7 @@ void QQTMPTableWidget::query(QString filter)
/* /*
* 10*14 * 10*14
*/ */
if (m_pageNum > ui->stWidgetPage->count()) if ( m_pageNum > ui->stWidgetPage->count() )
pix = m_pageNum - ui->stWidgetPage->count() + 10 pix = m_pageNum - ui->stWidgetPage->count() + 10
#else #else
/* /*
@ -101,250 +101,250 @@ void QQTMPTableWidget::query(QString filter)
#endif #endif
// pix >= 1 start work // pix >= 1 start work
for (int i = 0; i < pix; i++) for ( int i = 0; i < pix; i++ )
{ {
//ptime();//89ms //ptime();//89ms
QQtTableWidget* page = new QQtTableWidget(this); QQtTableWidget* page = new QQtTableWidget ( this );
//ptime();//2ms //ptime();//2ms
page->setDB(m_name); page->setDB ( m_name );
//ptime();//8ms //ptime();//8ms
page->setTable(m_table); page->setTable ( m_table );
//ptime();//14ms //ptime();//14ms
//query //query
//ptime();//3ms //ptime();//3ms
QAbstractItemModel* m_model = page->model(); QAbstractItemModel* m_model = page->model();
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
m_model->setHeaderData( m_model->setHeaderData (
i, Qt::Horizontal, i, Qt::Horizontal,
m_headerData.value(i, m_model->headerData(i, Qt::Horizontal).toString())); m_headerData.value ( i, m_model->headerData ( i, Qt::Horizontal ).toString() ) );
//ptime();//1ms //ptime();//1ms
page->setSelectionMode(selectionMode); page->setSelectionMode ( selectionMode );
page->setAlternatingRowColors(altColor); page->setAlternatingRowColors ( altColor );
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
page->horizontalHeader()->setResizeMode(resizeMode); page->horizontalHeader()->setResizeMode ( resizeMode );
#else #else
page->horizontalHeader()->setSectionResizeMode(resizeMode); page->horizontalHeader()->setSectionResizeMode ( resizeMode );
#endif #endif
//ptime();//0ms //ptime();//0ms
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
page->horizontalHeader()->setResizeMode(i, m_resizeMode.value(i, resizeMode)); page->horizontalHeader()->setResizeMode ( i, m_resizeMode.value ( i, resizeMode ) );
#else #else
page->horizontalHeader()->setSectionResizeMode(i, m_resizeMode.value(i, resizeMode)); page->horizontalHeader()->setSectionResizeMode ( i, m_resizeMode.value ( i, resizeMode ) );
#endif #endif
//ptime();//QHash(338ms) QMap(372ms) 400ms(QHash等几乎不耗时) //ptime();//QHash(338ms) QMap(372ms) 400ms(QHash等几乎不耗时)
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
page->setColumnWidth(i, m_columnWidth.value(i)); page->setColumnWidth ( i, m_columnWidth.value ( i ) );
//ptime(); //ptime();
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
page->setColumnHidden(i, m_columnHidden.value(i)); page->setColumnHidden ( i, m_columnHidden.value ( i ) );
//ptime();//219ms //ptime();//219ms
ui->stWidgetPage->addWidget(page); ui->stWidgetPage->addWidget ( page );
//ptime(); //ptime();
} }
for (int i = 0; i < m_pageNum; i++) for ( int i = 0; i < m_pageNum; i++ )
{ {
QQtTableWidget* page = (QQtTableWidget*)(ui->stWidgetPage->widget(i)); QQtTableWidget* page = ( QQtTableWidget* ) ( ui->stWidgetPage->widget ( i ) );
page->query(QString("%1 limit %2 offset %3") page->query ( QString ( "%1 limit %2 offset %3" )
.arg(filter) .arg ( filter )
.arg(m_numPerPage) .arg ( m_numPerPage )
.arg(i * m_numPerPage)); .arg ( i * m_numPerPage ) );
} }
} }
int QQTMPTableWidget::pageNum() int QQtMPTableWidget::pageNum()
{ {
return m_pageNum; return m_pageNum;
} }
int QQTMPTableWidget::currentPage() int QQtMPTableWidget::currentPage()
{ {
return ui->stWidgetPage->currentIndex() + 1; return ui->stWidgetPage->currentIndex() + 1;
} }
void QQTMPTableWidget::setCurrentPage(int index) void QQtMPTableWidget::setCurrentPage ( int index )
{ {
if (index < 1 || index > m_pageNum) if ( index < 1 || index > m_pageNum )
return; return;
ui->stWidgetPage->setCurrentIndex(index - 1); ui->stWidgetPage->setCurrentIndex ( index - 1 );
ui->lbPos->setText(QString("%1/%2").arg(index).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( index ).arg ( m_pageNum ) );
} }
void QQTMPTableWidget::setRecordNumPerPage(int num) void QQtMPTableWidget::setRecordNumPerPage ( int num )
{ {
m_numPerPage = num; m_numPerPage = num;
} }
void QQTMPTableWidget::setColumnHidden(int key, bool value) void QQtMPTableWidget::setColumnHidden ( int key, bool value )
{ {
m_columnHidden.insert(key, value); m_columnHidden.insert ( key, value );
} }
void QQTMPTableWidget::setSelectionMode(QAbstractItemView::SelectionMode mode) void QQtMPTableWidget::setSelectionMode ( QAbstractItemView::SelectionMode mode )
{ {
selectionMode = mode; selectionMode = mode;
} }
void QQTMPTableWidget::setResizeMode(QHeaderView::ResizeMode mode) void QQtMPTableWidget::setResizeMode ( QHeaderView::ResizeMode mode )
{ {
resizeMode = mode; resizeMode = mode;
} }
void QQTMPTableWidget::setResizeMode(int column, QHeaderView::ResizeMode mode) void QQtMPTableWidget::setResizeMode ( int column, QHeaderView::ResizeMode mode )
{ {
m_resizeMode.insert(column, mode); m_resizeMode.insert ( column, mode );
} }
void QQTMPTableWidget::setAlternatingRowColors(bool alt) void QQtMPTableWidget::setAlternatingRowColors ( bool alt )
{ {
altColor = alt; altColor = alt;
} }
void QQTMPTableWidget::setHeaderData(int i, Qt::Orientation, QString data) void QQtMPTableWidget::setHeaderData ( int i, Qt::Orientation, QString data )
{ {
m_headerData.insert(i, data); m_headerData.insert ( i, data );
} }
void QQTMPTableWidget::setColumnWidth(int column, int width) void QQtMPTableWidget::setColumnWidth ( int column, int width )
{ {
m_columnWidth.insert(column, width); m_columnWidth.insert ( column, width );
} }
void QQTMPTableWidget::on_btnLeft_clicked() void QQtMPTableWidget::on_btnLeft_clicked()
{ {
int index = ui->stWidgetPage->currentIndex(); int index = ui->stWidgetPage->currentIndex();
if (index > 0) if ( index > 0 )
index--; index--;
ui->stWidgetPage->setCurrentIndex(index); ui->stWidgetPage->setCurrentIndex ( index );
ui->lbPos->setText(QString("%1/%2").arg(index + 1).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( index + 1 ).arg ( m_pageNum ) );
} }
void QQTMPTableWidget::on_btnRight_clicked() void QQtMPTableWidget::on_btnRight_clicked()
{ {
int index = ui->stWidgetPage->currentIndex(); int index = ui->stWidgetPage->currentIndex();
if (index < m_pageNum - 1) if ( index < m_pageNum - 1 )
index++; index++;
ui->stWidgetPage->setCurrentIndex(index); ui->stWidgetPage->setCurrentIndex ( index );
ui->lbPos->setText(QString("%1/%2").arg(index + 1).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( index + 1 ).arg ( m_pageNum ) );
} }
void QQTMPTableWidget::on_btnJump_clicked() void QQtMPTableWidget::on_btnJump_clicked()
{ {
int num = ui->leNum->text().toInt(); int num = ui->leNum->text().toInt();
if (num <= m_pageNum && num > 0) if ( num <= m_pageNum && num > 0 )
{ {
ui->stWidgetPage->setCurrentIndex(num - 1); ui->stWidgetPage->setCurrentIndex ( num - 1 );
ui->lbPos->setText(QString("%1/%2").arg(num).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( num ).arg ( m_pageNum ) );
} }
} }
void QQTMPTableWidget::on_btnLeftHead_clicked() void QQtMPTableWidget::on_btnLeftHead_clicked()
{ {
ui->stWidgetPage->setCurrentIndex(0); ui->stWidgetPage->setCurrentIndex ( 0 );
ui->lbPos->setText(QString("%1/%2").arg(1).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( 1 ).arg ( m_pageNum ) );
} }
void QQTMPTableWidget::on_btnRightHead_clicked() void QQtMPTableWidget::on_btnRightHead_clicked()
{ {
int index = m_pageNum - 1; int index = m_pageNum - 1;
ui->stWidgetPage->setCurrentIndex(index); ui->stWidgetPage->setCurrentIndex ( index );
ui->lbPos->setText(QString("%1/%2").arg(index + 1).arg(m_pageNum)); ui->lbPos->setText ( QString ( "%1/%2" ).arg ( index + 1 ).arg ( m_pageNum ) );
} }
void QQTMPTableWidget::selectedRows(int column, QVector<QStringList>& strl) void QQtMPTableWidget::selectedRows ( int column, QVector<QStringList>& strl )
{ {
for (int i = 0; i < m_pageNum; i++) for ( int i = 0; i < m_pageNum; i++ )
{ {
QQtTableWidget* page = (QQtTableWidget*)ui->stWidgetPage->widget(i); QQtTableWidget* page = ( QQtTableWidget* ) ui->stWidgetPage->widget ( i );
QMap<int, QStringList> ids; QMap<int, QStringList> ids;
page->selectedRows(column, ids); page->selectedRows ( column, ids );
QMapIterator<int, QStringList> itor(ids); QMapIterator<int, QStringList> itor ( ids );
while (itor.hasNext()) while ( itor.hasNext() )
{ {
itor.next(); itor.next();
strl.append(itor.value()); strl.append ( itor.value() );
} }
} }
return; return;
} }
QQtTableWidget* QQTMPTableWidget::selectedRows(int column) QQtTableWidget* QQtMPTableWidget::selectedRows ( int column )
{ {
QString sectionName; QString sectionName;
QSqlQuery query(m_db); QSqlQuery query ( m_db );
query.exec(QString("select * from %1 limit 0").arg(m_table)); query.exec ( QString ( "select * from %1 limit 0" ).arg ( m_table ) );
sectionName = query.record().fieldName(column); sectionName = query.record().fieldName ( column );
query.finish(); query.finish();
pline() << sectionName; pline() << sectionName;
QVector<QStringList> lid; QVector<QStringList> lid;
selectedRows(column, lid); selectedRows ( column, lid );
static QQtTableWidget* page = new QQtTableWidget(this); static QQtTableWidget* page = new QQtTableWidget ( this );
page->setDB(m_name); page->setDB ( m_name );
page->setTable(m_table); page->setTable ( m_table );
QString excp; QString excp;
for (int i = 0; i < lid.count() - 1; i++) for ( int i = 0; i < lid.count() - 1; i++ )
excp += QString("%1 = '%2' or ").arg(sectionName).arg(lid[i].at(column)); excp += QString ( "%1 = '%2' or " ).arg ( sectionName ).arg ( lid[i].at ( column ) );
excp += QString("%1 = '%2'").arg(sectionName).arg(lid.last().at(column)); excp += QString ( "%1 = '%2'" ).arg ( sectionName ).arg ( lid.last().at ( column ) );
page->query(excp); page->query ( excp );
QAbstractItemModel* m_model = page->model(); QAbstractItemModel* m_model = page->model();
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
m_model->setHeaderData( m_model->setHeaderData (
i, Qt::Horizontal, i, Qt::Horizontal,
m_headerData.value(i, m_model->headerData(i, Qt::Horizontal).toString())); m_headerData.value ( i, m_model->headerData ( i, Qt::Horizontal ).toString() ) );
page->setSelectionMode(selectionMode); page->setSelectionMode ( selectionMode );
page->setAlternatingRowColors(altColor); page->setAlternatingRowColors ( altColor );
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
page->horizontalHeader()->setResizeMode(resizeMode); page->horizontalHeader()->setResizeMode ( resizeMode );
#else #else
page->horizontalHeader()->setSectionResizeMode(resizeMode); page->horizontalHeader()->setSectionResizeMode ( resizeMode );
#endif #endif
for (int i = 0; i < m_model->columnCount(); i++) for ( int i = 0; i < m_model->columnCount(); i++ )
{ {
page->setColumnHidden(i, m_columnHidden.value(i)); page->setColumnHidden ( i, m_columnHidden.value ( i ) );
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
page->horizontalHeader()->setResizeMode(i, m_resizeMode.value(i, resizeMode)); page->horizontalHeader()->setResizeMode ( i, m_resizeMode.value ( i, resizeMode ) );
#else #else
page->horizontalHeader()->setSectionResizeMode(i, m_resizeMode.value(i, resizeMode)); page->horizontalHeader()->setSectionResizeMode ( i, m_resizeMode.value ( i, resizeMode ) );
#endif #endif
page->setColumnWidth(i, m_columnWidth.value(i)); page->setColumnWidth ( i, m_columnWidth.value ( i ) );
} }
return page; return page;
} }
void QQTMPTableWidget::removeSelectedRows(int column) void QQtMPTableWidget::removeSelectedRows ( int column )
{ {
for (int i = 0; i < m_pageNum; i++) for ( int i = 0; i < m_pageNum; i++ )
{ {
QQtTableWidget* page = (QQtTableWidget*)ui->stWidgetPage->widget(i); QQtTableWidget* page = ( QQtTableWidget* ) ui->stWidgetPage->widget ( i );
QMap<int, QStringList> ids; QMap<int, QStringList> ids;
page->selectedRows(column, ids); page->selectedRows ( column, ids );
page->removeRows(column, ids.values()); page->removeRows ( column, ids.values() );
} }
} }

View File

@ -9,43 +9,43 @@
#include "qqt-local.h" #include "qqt-local.h"
namespace Ui { namespace Ui {
class QQTMPTableWidget; class QQtMPTableWidget;
} }
/** /**
* @brief The QQTMPTableWidget class * @brief The QQtMPTableWidget class
* *
* 使 * 使
* Table-DB关系Tree/List模型 * Table-DB关系Tree/List模型
*/ */
class QQTSHARED_EXPORT QQTMPTableWidget : public QWidget class QQTSHARED_EXPORT QQtMPTableWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QQTMPTableWidget(QWidget* parent = 0); explicit QQtMPTableWidget ( QWidget* parent = 0 );
~QQTMPTableWidget(); ~QQtMPTableWidget();
void setDB(QString db); void setDB ( QString db );
void setTable(QString table); void setTable ( QString table );
void setRelation(int column, const QSqlRelation& relation); void setRelation ( int column, const QSqlRelation& relation );
void setRecordNumPerPage(int num); void setRecordNumPerPage ( int num );
void setColumnHidden(int key, bool value = true); void setColumnHidden ( int key, bool value = true );
void setSelectionMode(QAbstractItemView::SelectionMode mode = QAbstractItemView::MultiSelection); void setSelectionMode ( QAbstractItemView::SelectionMode mode = QAbstractItemView::MultiSelection );
void setResizeMode(QHeaderView::ResizeMode mode = QHeaderView::ResizeToContents); void setResizeMode ( QHeaderView::ResizeMode mode = QHeaderView::ResizeToContents );
void setResizeMode(int column, QHeaderView::ResizeMode mode = QHeaderView::ResizeToContents); void setResizeMode ( int column, QHeaderView::ResizeMode mode = QHeaderView::ResizeToContents );
void setAlternatingRowColors(bool alt = true); void setAlternatingRowColors ( bool alt = true );
void setHeaderData(int i, Qt::Orientation, QString data); void setHeaderData ( int i, Qt::Orientation, QString data );
void setColumnWidth(int column, int width); void setColumnWidth ( int column, int width );
void query(QString filter = "1=1"); void query ( QString filter = "1=1" );
void selectedRows(int column, QVector<QStringList>& strl); void selectedRows ( int column, QVector<QStringList>& strl );
QQtTableWidget* selectedRows(int column = 0); QQtTableWidget* selectedRows ( int column = 0 );
int pageNum(); int pageNum();
int currentPage(); int currentPage();
void setCurrentPage(int page); void setCurrentPage ( int page );
public slots: public slots:
void removeSelectedRows(int column = 0); void removeSelectedRows ( int column = 0 );
private slots: private slots:
void on_btnLeft_clicked(); void on_btnLeft_clicked();
@ -59,7 +59,7 @@ private slots:
void on_btnRightHead_clicked(); void on_btnRightHead_clicked();
private: private:
Ui::QQTMPTableWidget* ui; Ui::QQtMPTableWidget* ui;
QSqlDatabase m_db; QSqlDatabase m_db;
QString m_name; QString m_name;
QString m_table; QString m_table;

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>QQTMPTableWidget</class> <class>QQtMPTableWidget</class>
<widget class="QWidget" name="QQTMPTableWidget"> <widget class="QWidget" name="QQtMPTableWidget">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>