2024-09-12 16:23:44 +08:00
|
|
|
|
#include "devicepropertypage.h"
|
|
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QTableView>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QScrollBar>
|
2024-09-13 12:53:02 +08:00
|
|
|
|
#include <QHeaderView>
|
2024-09-12 16:23:44 +08:00
|
|
|
|
|
|
|
|
|
#include "globalparameters.h"
|
|
|
|
|
#include "mytablemodel.h"
|
|
|
|
|
|
|
|
|
|
DevicePropertyPage::DevicePropertyPage(QWidget *parent) :
|
|
|
|
|
QWidget(parent),m_pTableView(nullptr),m_pButton(nullptr)
|
|
|
|
|
{
|
2024-09-13 12:53:02 +08:00
|
|
|
|
InitializeTable();
|
2024-09-12 16:23:44 +08:00
|
|
|
|
|
|
|
|
|
m_pTimer = new QTimer(this);
|
|
|
|
|
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
|
|
|
|
|
//m_pTimer->start(AppData::getInstance()->nTimeOut);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevicePropertyPage::~DevicePropertyPage()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePropertyPage::handleTimeout()
|
|
|
|
|
{
|
|
|
|
|
if(m_pTimer->isActive())
|
|
|
|
|
{
|
|
|
|
|
m_pTimer->stop();
|
|
|
|
|
|
|
|
|
|
Refresh();
|
|
|
|
|
|
2024-09-13 12:53:02 +08:00
|
|
|
|
int nInterval = 5000;
|
2024-09-12 16:23:44 +08:00
|
|
|
|
m_pTimer->start(nInterval*1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 13:01:48 +08:00
|
|
|
|
void DevicePropertyPage::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(200);
|
|
|
|
|
|
|
|
|
|
// //设置表头字体加粗
|
|
|
|
|
// 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::Interactive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 允许用户通过拖动表头边缘调整列宽
|
|
|
|
|
//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 1
|
|
|
|
|
//设置列宽
|
|
|
|
|
int base = 120; //w / (model->columnCount()-1);
|
|
|
|
|
tableView->setColumnWidth(0,base-30);
|
|
|
|
|
tableView->setColumnWidth(1,base*3+40);
|
|
|
|
|
tableView->setColumnWidth(2,base);
|
|
|
|
|
tableView->setColumnWidth(3,base-40);
|
|
|
|
|
tableView->setColumnWidth(4,base+50);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
tableView->show();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 16:23:44 +08:00
|
|
|
|
void DevicePropertyPage::InitializeTable()
|
|
|
|
|
{
|
|
|
|
|
m_myModel = new MyTableModel(this);
|
|
|
|
|
m_myModel->setHeadData(AppData::getInstance()->lstDataTableHeaderText);
|
|
|
|
|
|
|
|
|
|
m_pTableView = new QTableView(this);
|
|
|
|
|
m_pButton = new QPushButton(tr("Refresh"), this);
|
|
|
|
|
|
|
|
|
|
// 创建主布局
|
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
|
|
|
|
|
2024-09-14 13:01:48 +08:00
|
|
|
|
InitializeTableView(m_myModel,m_pTableView);
|
2024-09-12 16:23:44 +08:00
|
|
|
|
|
|
|
|
|
mainLayout->addWidget(m_pTableView);
|
|
|
|
|
|
|
|
|
|
// 创建一个水平布局来包含按钮
|
|
|
|
|
QHBoxLayout *buttonLayout = new QHBoxLayout();
|
|
|
|
|
buttonLayout->addStretch(); // 让按钮保持在右侧
|
|
|
|
|
buttonLayout->addWidget(m_pButton);
|
|
|
|
|
buttonLayout->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距
|
|
|
|
|
|
|
|
|
|
// 将表格视图和按钮布局添加到主布局
|
|
|
|
|
mainLayout->addLayout(buttonLayout);
|
|
|
|
|
|
|
|
|
|
mainLayout->setContentsMargins(0, 0, 0, 0); // 取消主布局的边距
|
|
|
|
|
mainLayout->setSpacing(10); // 取消布局间的间距
|
|
|
|
|
|
|
|
|
|
// 设置固定大小和位置的按钮
|
|
|
|
|
m_pButton->setFixedSize(100, 30); // 设置按钮的固定大小
|
|
|
|
|
|
|
|
|
|
// 连接信号和槽
|
|
|
|
|
connect(m_pTableView, &QTableView::doubleClicked, this, &DevicePropertyPage::onTableViewDoubleClicked);
|
|
|
|
|
connect(m_pButton, &QPushButton::clicked, this, &DevicePropertyPage::onButtonClicked);
|
2024-09-13 12:53:02 +08:00
|
|
|
|
|
|
|
|
|
setLayout(mainLayout);
|
2024-09-12 16:23:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePropertyPage::Refresh()
|
|
|
|
|
{
|
|
|
|
|
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
|
|
|
TableData tbl_data;
|
|
|
|
|
if (Fetch(tbl_data))
|
|
|
|
|
{
|
|
|
|
|
m_myModel->setModelData(tbl_data);
|
|
|
|
|
}
|
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePropertyPage::setBaseType(unsigned int base, unsigned int mask)
|
|
|
|
|
{
|
|
|
|
|
filterBaseType = base;
|
|
|
|
|
mask_code = mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DevicePropertyPage::onButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
m_pTimer->stop();
|
|
|
|
|
|
|
|
|
|
Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DevicePropertyPage::onTableViewDoubleClicked(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
//QAbstractItemModel *model=ui->tableView->model();
|
|
|
|
|
MyTableModel *model = (MyTableModel *)m_pTableView->model();
|
|
|
|
|
QModelIndex mindex = model->index(index.row(),7); //index.row()为算选择的行号。7为所选中行的第8列。。
|
|
|
|
|
QVariant datatemp=model->data(mindex);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 12:53:02 +08:00
|
|
|
|
void DevicePropertyPage::resizeEvent(QResizeEvent *event)
|
2024-09-12 16:23:44 +08:00
|
|
|
|
{
|
2024-09-13 12:53:02 +08:00
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
|
#if 0
|
|
|
|
|
// 获取当前QTableView的总宽度
|
|
|
|
|
int currentTotalWidth = m_pTableView->viewport()->width();
|
|
|
|
|
|
|
|
|
|
// 计算总的初始宽度
|
|
|
|
|
int initialTotalWidth = 0;
|
|
|
|
|
for (int width : columnWidths) {
|
|
|
|
|
initialTotalWidth += width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算比例因子
|
|
|
|
|
double scaleFactor = static_cast<double>(currentTotalWidth) / initialTotalWidth;
|
|
|
|
|
|
|
|
|
|
// 重新设置列宽,按比例调整
|
|
|
|
|
for (int i = 0; i < columnWidths.size(); ++i) {
|
|
|
|
|
int newWidth = static_cast<int>(columnWidths[i] * scaleFactor);
|
|
|
|
|
m_pTableView->setColumnWidth(i, newWidth);
|
2024-09-12 16:23:44 +08:00
|
|
|
|
}
|
2024-09-13 12:53:02 +08:00
|
|
|
|
#endif
|
2024-09-12 16:23:44 +08:00
|
|
|
|
}
|
|
|
|
|
|