#include "loadMonitorPageDlg.h" #include "ui_loadMonitorPageDlg.h" #include "global.h" #include "tools.h" #include LoadMonitorPageDlg::LoadMonitorPageDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::loadMonitorPageDlg) ,_pModel(nullptr) { ui->setupUi(this); this->setWindowFlags(Qt::FramelessWindowHint | windowFlags()); initial(); } LoadMonitorPageDlg::~LoadMonitorPageDlg() { delete ui; } void LoadMonitorPageDlg::initial() { _pModel = new QStandardItemModel(this); ui->treeView->setModel(_pModel); ui->treeView->setHeaderHidden(true); connect(ui->btn_ok,&QPushButton::clicked,this,&LoadMonitorPageDlg::onOkClicked); connect(ui->btn_cancel,&QPushButton::clicked,this,&LoadMonitorPageDlg::onCancelClicked); } void LoadMonitorPageDlg::updateItems(QString sProj,QPair pair) { QStandardItem* topLevelItem = findTopLevelItem(sProj); if (topLevelItem) { // 如果存在,直接在该项下插入子项 QStandardItem* childItem = new QStandardItem(pair.first); childItem->setData(pair.second); topLevelItem->appendRow(childItem); } else { // 如果不存在,创建新的顶层项并插入子项 QStandardItem* newTopLevelItem = new QStandardItem(sProj); QStandardItem* childItem = new QStandardItem(pair.first); childItem->setData(pair.second); newTopLevelItem->appendRow(childItem); _pModel->appendRow(newTopLevelItem); } ui->treeView->expandAll(); } void LoadMonitorPageDlg::clearItems() { if(_pModel){ QStandardItem *root = _pModel->invisibleRootItem(); //先清空model int rowCount = root->rowCount(); if (rowCount > 0) { _pModel->removeRows(0, rowCount); } } } void LoadMonitorPageDlg::onOkClicked() { QItemSelectionModel* selectionModel = ui->treeView->selectionModel(); if (selectionModel && selectionModel->hasSelection()) { // 获取当前选中的索引 QModelIndex currentIndex = selectionModel->currentIndex(); if (currentIndex.isValid()) { QStandardItem* item = _pModel->itemFromIndex(currentIndex); int nLevel = getLevel(item); if(nLevel == 0 || nLevel == -1) //顶层不响应 return; QStandardItem* parent = item->parent(); if(item) { DiagramInfo info; info.id = item->data(Qt::UserRole+1).toUuid(); info.sName = item->text(); info.sBasePageName = parent->text(); emit monitorSelected(info); } hide(); } } } void LoadMonitorPageDlg::onCancelClicked() { hide(); } QStandardItem* LoadMonitorPageDlg::findTopLevelItem(const QString& name) { for (int i = 0; i < _pModel->rowCount(); ++i) { QStandardItem* item = _pModel->item(i); if (item && item->text() == name) { return item; } } return nullptr; }