完成page control,完成数据的翻页功能

main
HwangKC 2024-11-11 15:28:31 +08:00
parent a920ce5b41
commit 246153fc8f
16 changed files with 328 additions and 68 deletions

View File

@ -13,9 +13,9 @@ DataFetcher::~DataFetcher()
}
bool DataFetcher::Fetch(TableData& tbl_data)
bool DataFetcher::Fetch(TableData& tbl_data,int fetch_rows)
{
int rows = MysqlUtils::getInstance()->RetrieveTableData(tbl_data);
int rows = MysqlUtils::getInstance()->RetrieveTableData(tbl_data,fetch_rows);
if (rows < 0)
{
hloge("Failed to retrieve signal data");

View File

@ -12,7 +12,7 @@ public:
virtual ~DataFetcher();
public:
virtual bool Fetch(TableData& tbl_data);
virtual bool Fetch(TableData& tbl_data,int fetch_rows = 1000);
protected:
int VerifyStatus();

View File

@ -12,6 +12,9 @@
#include "globalparameters.h"
#include "mytablemodel.h"
#define PAGES_FORMAT_STRING ("Total %1 Pages, Current No.%2 Page")
#define PER_PAGE_SIZE (30)
DevicePropertyPage::DevicePropertyPage(QWidget *parent) :
QWidget(parent),m_pTableView(nullptr),m_pButton(nullptr)
{
@ -113,6 +116,22 @@ void DevicePropertyPage::InitializeTable()
m_pTableView = new QTableView(this);
m_pButton = new QPushButton(tr("Refresh"), this);
m_pFirstButton = new QPushButton(tr("First"), this);
m_pForwardButton = new QPushButton(tr("Forward"), this);
m_pNextButton = new QPushButton(tr("Next"), this);
m_pLastButton = new QPushButton(tr("Last"), this);
m_pGotoPageNumberLineEdit = new QLineEdit(tr("1"), this);
m_pGotoPageNumberLineEdit->setFixedWidth(40);
m_pValidator = new QIntValidator(this);
m_pGotoPageNumberLineEdit->setValidator(m_pValidator);
m_pValidator->setRange(0, 100);
m_pGotoPageNumberLabel = new QLabel(tr("Page"), this);
m_pGotoPageNumberButton = new QPushButton(tr("Goto"), this);
m_pPagesLabel = new QLabel(tr("Total %d Pages, Current No.%d Page"), this);
// 创建主布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
@ -123,7 +142,26 @@ void DevicePropertyPage::InitializeTable()
// 创建一个水平布局来包含按钮
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addStretch(); // 让按钮保持在右侧
buttonLayout->addWidget(m_pPagesLabel);
QSpacerItem *spacer0 = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
buttonLayout->addItem(spacer0);
buttonLayout->addWidget(m_pFirstButton);
buttonLayout->addWidget(m_pForwardButton);
buttonLayout->addWidget(m_pNextButton);
buttonLayout->addWidget(m_pLastButton);
QSpacerItem *spacer = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
buttonLayout->addItem(spacer);
buttonLayout->addWidget(m_pGotoPageNumberLabel);
buttonLayout->addWidget(m_pGotoPageNumberLineEdit);
buttonLayout->addWidget(m_pGotoPageNumberButton);
buttonLayout->addWidget(m_pButton);
buttonLayout->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距
// 将表格视图和按钮布局添加到主布局
@ -139,18 +177,13 @@ void DevicePropertyPage::InitializeTable()
connect(m_pTableView, &QTableView::doubleClicked, this, &DevicePropertyPage::onTableViewDoubleClicked);
connect(m_pButton, &QPushButton::clicked, this, &DevicePropertyPage::onButtonClicked);
setLayout(mainLayout);
}
connect(m_pFirstButton, &QPushButton::clicked, this, &DevicePropertyPage::onFirstButtonClicked);
connect(m_pLastButton, &QPushButton::clicked, this, &DevicePropertyPage::onLastButtonClicked);
connect(m_pForwardButton, &QPushButton::clicked, this, &DevicePropertyPage::onForwardButtonClicked);
connect(m_pNextButton, &QPushButton::clicked, this, &DevicePropertyPage::onNextButtonClicked);
connect(m_pGotoPageNumberButton, &QPushButton::clicked, this, &DevicePropertyPage::onGotoButtonClicked);
void DevicePropertyPage::Refresh()
{
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
TableData tbl_data;
if (Fetch(tbl_data))
{
m_myModel->setModelData(tbl_data);
}
QGuiApplication::restoreOverrideCursor();
setLayout(mainLayout);
}
void DevicePropertyPage::setBaseType(unsigned int base, unsigned int mask)
@ -159,7 +192,6 @@ void DevicePropertyPage::setBaseType(unsigned int base, unsigned int mask)
mask_code = mask;
}
void DevicePropertyPage::onButtonClicked()
{
m_pTimer->stop();
@ -167,6 +199,86 @@ void DevicePropertyPage::onButtonClicked()
Refresh();
}
void DevicePropertyPage::Refresh()
{
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
m_TotalTableData.clear();
if (Fetch(m_TotalTableData,106))
{
PageDataProcess();
m_myModel->setModelData(m_currentPageData);
}
updatePageButtonState();
updatePageLabel();
QGuiApplication::restoreOverrideCursor();
}
int DevicePropertyPage::PageDataProcess()
{
//获取数据总数
m_totalRows = m_TotalTableData.size();
m_totalPageCount = 1;
m_currentPage = 1;
m_currentPageData.clear();
if (m_totalRows <= PER_PAGE_SIZE)
{
m_currentPageData = m_TotalTableData;
return 1;
}
m_totalPageCount = int(m_totalRows / PER_PAGE_SIZE) + 1;
m_currentPageData = m_TotalTableData.mid(0, PER_PAGE_SIZE);
return 1;
}
void DevicePropertyPage::updatePageLabel()
{
QString s = QString(PAGES_FORMAT_STRING).arg(m_totalPageCount).arg(m_currentPage);
m_pPagesLabel->setText(s);
}
void DevicePropertyPage::updatePageButtonState()
{
if ( m_currentPage == m_totalPageCount) //翻到底了,超过最大的页数
{
m_pNextButton->setEnabled(false);
m_pForwardButton->setEnabled(true);
m_pLastButton->setEnabled(false);
m_pFirstButton->setEnabled(true);
}
else if (m_currentPage == 1) //翻到头了
{
m_pNextButton->setEnabled(true);
m_pForwardButton->setEnabled(false);
m_pLastButton->setEnabled(true);
m_pFirstButton->setEnabled(false);
}
else if (m_currentPage < m_totalPageCount) //页数在中间状态
{
m_pNextButton->setEnabled(true);
m_pForwardButton->setEnabled(true);
m_pLastButton->setEnabled(true);
m_pFirstButton->setEnabled(true);
}
else
{
assert(false);
}
}
void DevicePropertyPage::pageTo(int page)
{
m_currentPageData.clear();
int numLeftElements = m_totalRows - (page - 1) * PER_PAGE_SIZE;
int numElements = numLeftElements >= PER_PAGE_SIZE ? PER_PAGE_SIZE : numLeftElements;
m_currentPageData = m_TotalTableData.mid((page-1) * PER_PAGE_SIZE, numElements);
m_myModel->setModelData(m_currentPageData);
}
void DevicePropertyPage::onTableViewDoubleClicked(const QModelIndex &index)
{
@ -176,6 +288,75 @@ void DevicePropertyPage::onTableViewDoubleClicked(const QModelIndex &index)
QVariant datatemp=model->data(mindex);
}
void DevicePropertyPage::onFirstButtonClicked()
{
m_currentPage = 1;
updatePageButtonState();
pageTo(m_currentPage);
updatePageLabel();
}
void DevicePropertyPage::onLastButtonClicked()
{
m_currentPage = m_totalPageCount;
updatePageButtonState();
pageTo(m_currentPage);
updatePageLabel();
}
void DevicePropertyPage::onForwardButtonClicked()
{
m_currentPage--;
updatePageButtonState();
m_currentPageData.clear();
pageTo(m_currentPage);
updatePageLabel();
}
void DevicePropertyPage::onNextButtonClicked()
{
m_currentPage++;
updatePageButtonState();
m_currentPageData.clear();
pageTo(m_currentPage);
updatePageLabel();
}
void DevicePropertyPage::onGotoButtonClicked()
{
int page = m_pGotoPageNumberLineEdit->text().toInt();
if (page <=0 || page>m_totalPageCount)
{
QString s = QString(tr("Page number should be large than 0 and less than %1 !")).arg(m_totalPageCount);
QMessageBox::critical(this, tr("Critical Message"),s);
return ;
}
m_currentPage = page;
updatePageButtonState();
m_currentPageData.clear();
pageTo(m_currentPage);
updatePageLabel();
}
void DevicePropertyPage::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);

View File

@ -4,6 +4,10 @@
#pragma execution_character_set("utf-8")
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QIntValidator>
#include "datafetcher.h"
class MyTableModel;
@ -37,18 +41,55 @@ protected:
// 重写resizeEvent当QTableView窗口大小变化时按比例调整列宽
void resizeEvent(QResizeEvent *event) override;
//处理分页数据
int PageDataProcess();
//更新页数显示
void updatePageLabel();
//更新翻页按钮状态
void updatePageButtonState();
//翻倒第i页
void pageTo(int page);
private slots:
void onButtonClicked();
void onTableViewDoubleClicked(const QModelIndex &index);
void onFirstButtonClicked();
void onLastButtonClicked();
void onForwardButtonClicked();
void onNextButtonClicked();
void onGotoButtonClicked();
private:
unsigned int filterBaseType;
unsigned int mask_code;
QTableView* m_pTableView;
QPushButton* m_pButton;
QPushButton* m_pForwardButton;
QPushButton* m_pNextButton;
QPushButton* m_pFirstButton;
QPushButton* m_pLastButton;
QIntValidator* m_pValidator;
QLabel* m_pGotoPageNumberLabel;
QLineEdit* m_pGotoPageNumberLineEdit;
QPushButton* m_pGotoPageNumberButton;
QLabel* m_pPagesLabel;
QVector<int> columnWidths; // 存储初始列宽
private:
TableData m_TotalTableData;
TableData m_currentPageData;
int m_totalPageCount; //总页数
int m_currentPage; //当前页
int m_totalRows; //数据总行数
};
#endif // DEVICEPROPERTYPAGE_H

View File

@ -1,4 +1,4 @@
#include "formserialportsettingdialog.h"
#include "formserialportsettingdialog.h"
#include "ui_formserialportsettingdialog.h"
#include <QListView>

View File

@ -136,8 +136,10 @@ AppTcpClient::AppTcpClient(token)
{
}
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf);
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
hlogd("TCP Client<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
}
int AppTcpClient::Initialize(QString remote_host, int remote_port)
{
@ -152,18 +154,14 @@ int AppTcpClient::Initialize(QString remote_host, int remote_port)
}
m_tcpclient = new hv::TcpClient();
m_connfd = m_tcpclient->createsocket(remote_port, remote_host.toStdString().c_str());
if (m_connfd < 0)
{
return -20;
}
m_tcpclient->onMessage = onMessage;
//m_tcpclient.onMessage = [](const SocketChannelPtr& channel, Buffer* buf)
//{
// hlogi("<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
//};
//m_tcpclient->onMessage = onMessage;
reconn_setting_t reconn;
reconn_setting_init(&reconn);
@ -178,7 +176,8 @@ int AppTcpClient::Initialize(QString remote_host, int remote_port)
return m_connfd;
}
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
void AppTcpClient::resetOnMessageCallbackFunction(onMessageCallbackFunc &func)
{
hlogi("<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
m_tcpclient->onMessage = func;
}

View File

@ -134,6 +134,7 @@ public:
void InitializeTableView(MyTableModel* model,QTableView* tableView);
};
typedef std::function<void(const hv::SocketChannelPtr&, hv::Buffer*)> onMessageCallbackFunc;
class AppTcpClient:public Singleton<AppTcpClient>
{
@ -145,6 +146,7 @@ public:
AppTcpClient& operator =(const AppTcpClient&)= delete;
int Initialize(QString remote_host,int remote_port=DEVICE_SERVER_PORT);
void resetOnMessageCallbackFunction(onMessageCallbackFunc& func);
public:
hv::TcpClient* m_tcpclient;

View File

@ -226,14 +226,17 @@ public:
bool isConnected()
{
if (channel == NULL) return false;
if (channel == NULL)
return false;
return channel->isConnected();
}
// send thread-safe
int send(const void* data, int size)
{
if (!isConnected()) return -1;
if (!isConnected())
return -1;
return channel->write(data, size);
}
int send(Buffer* buf)

View File

@ -1,4 +1,4 @@
#include "kutilities.h"
#include "kutilities.h"
#include <iomanip>
#include <string>
#include <sstream>

View File

@ -14,6 +14,9 @@
#include <QTableView>
#include <QStandardItemModel>
#include <hv/hlog.h>
#include "kutilities.h"
#include "devicepropertypage.h"
#include "formserialportsettingdialog.h"
@ -29,6 +32,10 @@ MainDialog::MainDialog(QWidget *parent) :
// Load the window state
loadWindowState();
//设置返回消息回调
onMessageCallbackFunc f = std::bind(&MainDialog::onMessage, this, std::placeholders::_1, std::placeholders::_2);
AppTcpClient::getInstance()->resetOnMessageCallbackFunction(f);
}
MainDialog::~MainDialog()
@ -240,3 +247,8 @@ void MainDialog::onToolRefreshClicked()
{
QMessageBox::information(this, "Refresh Button Clicked", "Refresh");
}
void MainDialog::onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
hlogd("MainDialog <==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
}

View File

@ -8,6 +8,7 @@
#include <QIcon>
#include <QStackedWidget>
#include <QToolBar>
#include <hv_tcpclient.h>
namespace Ui
{
@ -29,6 +30,8 @@ public:
public slots:
void changePage(QListWidgetItem *current, QListWidgetItem *previous);
//接收服务器返回消息的函数
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf);
private slots:
void onToolButton1Clicked();

View File

@ -8,7 +8,7 @@
#include <hv/hlog.h>
#include "MainDialog.h"
#include "mysqlutils.h"
#include "kutilities.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@ -69,24 +69,37 @@ bool MainWindow::testDatabase()
return true;
}
void MainWindow::onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
hlogd("MainWindow<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
}
bool MainWindow::testServerEcho()
{
using namespace hv;
QString svr = ui->serverIp->text();
int connfd = AppTcpClient::getInstance()->Initialize("127.0.0.1",DEVICE_SERVER_PORT);
int connfd = AppTcpClient::getInstance()->Initialize(svr,DEVICE_SERVER_PORT);
if (connfd <= 0)
{
hloge("failed to connect to device data service ...");
hloge("failed to create socket for connecting to device data service ...");
return false;
}
hlogi("connect to device data service port %d, connfd=%d ...", DEVICE_SERVER_PORT, connfd);
hlogi("create socket for connecting to device data service port %d, connfd=%d ...", DEVICE_SERVER_PORT, connfd);
CWaitorCursor wait1;
hv_msleep(500);
//设置返回消息回调
onMessageCallbackFunc f = std::bind(&MainWindow::onMessage, this, std::placeholders::_1, std::placeholders::_2);
AppTcpClient::getInstance()->resetOnMessageCallbackFunction(f);
unsigned char frame[10] = { 0x02, 0x01,0x00,0x00,0x00, 0x00,0xEE,0xFF,0xEE,0xFF};
int cnt = AppTcpClient::getInstance()->m_tcpclient->send((void*)frame,10);
hlogd("send %d bytes, connfd=%d ...", cnt, connfd);
hlogd("send %d bytes test echo signal, connfd=%d ...", cnt, connfd);
return cnt > 0 ? true : false;
}

View File

@ -3,6 +3,7 @@
#pragma execution_character_set("utf-8")
#include <QMainWindow>
#include <hv_tcpclient.h>
class MainDialog;
@ -21,6 +22,9 @@ public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public:
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf);
protected:
void setIp(const QString &ip);
bool testDatabase();

View File

@ -39,17 +39,16 @@
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>160</x>
<x>100</x>
<y>50</y>
<width>240</width>
<width>391</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<family>宋体</family>
<italic>false</italic>
<bold>false</bold>
<pointsize>28</pointsize>
<bold>true</bold>
</font>
</property>
<property name="text">
@ -83,9 +82,8 @@
</property>
<property name="font">
<font>
<family>宋体</family>
<italic>false</italic>
<bold>false</bold>
<pointsize>12</pointsize>
<bold>true</bold>
</font>
</property>
<property name="text">

View File

@ -1,4 +1,4 @@
#include "mysqlutils.h"
#include "mysqlutils.h"
#include <QDebug>
#include <hv/hlog.h>
@ -148,13 +148,15 @@ void MysqlUtils::CloseDatabase()
#endif
}
int MysqlUtils::RetrieveTableData(TableData& tbl_data)
int MysqlUtils::RetrieveTableData(TableData& tbl_data,int retrieve_rows)
{
#if 1
MYSQL_RES* res;
MYSQL_ROW row;
//执行SQL 查询
if (mysql_query(m_DbConnection, "SELECT * FROM `hjems`.`tbl_data` ORDER BY data_timestamp LIMIT 0,100"))
QString sql = QString("SELECT * FROM `hjems`.`tbl_data` ORDER BY data_timestamp LIMIT 0,%1").arg(retrieve_rows);
if (mysql_query(m_DbConnection, sql.toStdString().c_str()))
{
hloge( "Error: %s", mysql_error(m_DbConnection) ) ;
return -1;
@ -162,33 +164,35 @@ int MysqlUtils::RetrieveTableData(TableData& tbl_data)
res = mysql_use_result(m_DbConnection);
int j = 0;
while ((row = mysql_fetch_row(res)) != NULL)
{
ModelItem item;
// item.status = STATUS_WARN;
// item.ParameterName= "Visual Leak Detector detected 1 memory leak (84 bytes)";
// item.sampleTime = "2024-9-10 11:22:33.444";
// item.unit = "C";
// item.value = "100";
// tbl_data.append(item);
item.status = STATUS_WARN;
item.ParameterName= "Visual Leak Detector detected 1 memory leak (84 bytes)";
item.sampleTime = "2024-9-10 11:22:33.444";
item.unit = "C";
item.value = QString("%1").arg(j++).toStdString();
tbl_data.append(item);
std::string buf;
for (unsigned int i = 0; i < mysql_num_fields(res); ++i)
{
qDebug() << mysql_fetch_field_direct(res,i)->name ;
QString field_name (mysql_fetch_field_direct(res,i)->name);
if (field_name == "data_timestamp")
{
item.sampleTime = row[i];
qDebug() << row[i];
}
else if (field_name == "data_content")
{
std::string v(row[i]);
qDebug() << Kutilities::printHex(v.c_str(),v.length()).c_str();
ZipUtils::DecompressString(v.c_str(),v.length(),buf);
qDebug() << buf.c_str();
}
// qDebug() << mysql_fetch_field_direct(res,i)->name ;
// QString field_name (mysql_fetch_field_direct(res,i)->name);
// if (field_name == "data_timestamp")
// {
// item.sampleTime = row[i];
// qDebug() << row[i];
// }
// else if (field_name == "data_content")
// {
// std::string v(row[i]);
// qDebug() << Kutilities::printHex(v.c_str(),v.length()).c_str();
// ZipUtils::DecompressString(v.c_str(),v.length(),buf);
// qDebug() << buf.c_str();
// }
}
qDebug() << "\r\n";
}

View File

@ -31,7 +31,7 @@ public:
void CloseDatabase();
bool OpenDatabase(const std::string& server = "127.0.0.1", int dbport = 3306, const std::string& dbuser = "root", const std::string& dbpasswd = "Hj57471000", const std::string& database="hjems");
int RetrieveTableData(TableData& tbl_data);
int RetrieveTableData(TableData& tbl_data,int retrieve_rows=100);
private:
#ifdef _USING_QT_MYSQL_CONNECTOR_