185 lines
4.6 KiB
C++
185 lines
4.6 KiB
C++
#include <QCoreApplication>
|
|
#include <QMessageBox>
|
|
#include <QString>
|
|
#include <QAbstractTableModel>
|
|
#include <QTableView>
|
|
#include <QHeaderView>
|
|
#include <QApplication>
|
|
#include <QCursor>
|
|
|
|
#include <hv/hlog.h>
|
|
|
|
#include "globalparameters.h"
|
|
#include "mytablemodel.h"
|
|
#include "kutilities.h"
|
|
|
|
AppData::~AppData()
|
|
{
|
|
}
|
|
|
|
AppData::AppData(token)
|
|
{
|
|
lstDataTableHeaderText << ("Status")
|
|
<< ("Parameter")
|
|
<< ("Value")
|
|
<< ("Unit")
|
|
<< ("Time");
|
|
|
|
nTimeOut = 5000;
|
|
qsDestinationIp = "127.0.0.1";
|
|
|
|
qsLastErrorString = "OK";
|
|
|
|
}
|
|
|
|
AppCommon::AppCommon(token)
|
|
{
|
|
}
|
|
|
|
AppCommon::~AppCommon()
|
|
{
|
|
|
|
}
|
|
|
|
void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView)
|
|
{
|
|
//设置tableview的model
|
|
model->setHeadData(AppData::getInstance()->lstDataTableHeaderText);
|
|
tableView->setModel(model);
|
|
|
|
QHeaderView* pHeaderView = tableView->horizontalHeader();
|
|
//pHeaderView->setStyleSheet("QHeaderView::section {color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");
|
|
pHeaderView->setStyleSheet("QHeaderView::section{background:lightgray;}");
|
|
|
|
// pHeaderView->setSectionResizeMode(QHeaderView::Stretch); //Stretch
|
|
|
|
pHeaderView->setHidden(false); //false 显示行号列 true Hide
|
|
//pHeaderView->setVisible(true);
|
|
//pHeaderView->setFixedHeight(40);
|
|
|
|
//点击表时不对表头行光亮(获取焦点)
|
|
pHeaderView->setHighlightSections(false);
|
|
//pHeaderView->setDefaultSectionSize(35);
|
|
|
|
// //设置表头字体加粗
|
|
// QFont font = pHeaderView->font();
|
|
// font.setBold(true);
|
|
// pHeaderView->setFont(font);
|
|
|
|
//设置表头字体
|
|
pHeaderView->setFont(QFont("Arial", 12));
|
|
|
|
// 设置表头列宽自动调整
|
|
for (int i = 0; i < model->columnCount(); ++i)
|
|
{
|
|
pHeaderView->setSectionResizeMode(i, QHeaderView::Stretch);
|
|
}
|
|
|
|
// 允许用户通过拖动表头边缘调整列宽
|
|
pHeaderView->setSectionResizeMode(QHeaderView::Interactive);
|
|
|
|
tableView->verticalHeader()->setDefaultSectionSize(30); //行高
|
|
|
|
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
tableView->setAlternatingRowColors(true);
|
|
tableView->setTextElideMode(Qt::ElideMiddle);
|
|
|
|
|
|
//所有单元格的字体 设置成一样
|
|
tableView->setFont(QFont("Arial", 9));
|
|
|
|
//设置表格数据区内的所有单元格都不允许编辑
|
|
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
#if 0
|
|
//设置列宽
|
|
int w = tableView->width();
|
|
int base = w /(AppData::getInstance()->lstDataTableHeaderText.count()-1);
|
|
tableView->setColumnWidth(0,base-45);
|
|
tableView->setColumnWidth(1,base-20);
|
|
tableView->setColumnWidth(2,base+30);
|
|
tableView->setColumnWidth(3,base+10);
|
|
tableView->setColumnWidth(4,base);
|
|
#endif
|
|
|
|
tableView->show();
|
|
}
|
|
|
|
CWaitorCursor::CWaitorCursor()
|
|
{
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
}
|
|
|
|
CWaitorCursor::~CWaitorCursor()
|
|
{
|
|
QApplication::restoreOverrideCursor();
|
|
}
|
|
|
|
///////////////////////////////////////////////////
|
|
/// \brief AppTcpClient
|
|
///
|
|
///
|
|
|
|
AppTcpClient::~AppTcpClient()
|
|
{
|
|
if (m_tcpclient)
|
|
{
|
|
m_tcpclient->stop();
|
|
delete m_tcpclient;
|
|
}
|
|
}
|
|
|
|
AppTcpClient::AppTcpClient(token)
|
|
:m_tcpclient(nullptr), m_connfd(0)
|
|
{
|
|
}
|
|
|
|
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf);
|
|
|
|
|
|
int AppTcpClient::Initialize(QString remote_host, int remote_port)
|
|
{
|
|
using namespace hv;
|
|
|
|
if (!m_tcpclient || !m_tcpclient->isConnected())
|
|
{
|
|
if (m_tcpclient)
|
|
{
|
|
m_tcpclient->stop();
|
|
delete m_tcpclient;
|
|
}
|
|
|
|
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());
|
|
//};
|
|
|
|
reconn_setting_t reconn;
|
|
reconn_setting_init(&reconn);
|
|
reconn.min_delay = 1000;
|
|
reconn.max_delay = 10000;
|
|
reconn.delay_policy = 2;
|
|
//reconn.max_retry_cnt = 50;
|
|
m_tcpclient->setReconnect(&reconn);
|
|
|
|
m_tcpclient->start();
|
|
}
|
|
return m_connfd;
|
|
}
|
|
|
|
void onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
|
|
{
|
|
hlogi("<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
|
|
}
|