#include "devicepropertypage.h" #include #include #include #include #include #include #include #include "globalparameters.h" #include "mytablemodel.h" DevicePropertyPage::DevicePropertyPage(QWidget *parent) : QWidget(parent),m_pTableView(nullptr),m_pButton(nullptr) { //InitializeTable(); 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(); int nInterval = 5000; //ui->spinBox->value(); m_pTimer->start(nInterval*1000); } } 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); //AppCommon::getInstance()->InitializeTableView(m_myModel,m_pTableView); m_pTableView->setModel(m_myModel); // 自动调整列宽 connect(m_pTableView->horizontalScrollBar(), &QScrollBar::valueChanged, this, &DevicePropertyPage::adjustColumnWidths); adjustColumnWidths(); // 初次设置列宽 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); } 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); } void DevicePropertyPage::adjustColumnWidths() { int columnCount = m_pTableView->model()->columnCount(); int totalWidth = m_pTableView->viewport()->width(); int columnWidth = totalWidth / columnCount; for (int column = 0; column < columnCount; ++column) { m_pTableView->setColumnWidth(column, columnWidth); } }