#include "mainwindow.h" #include "custommodel.h" #include "ui_mainwindow.h" #include "listview_ad.h" #include "listview_delgate.h" #include #include"../sample_button_slide/CustomButton.h" #include"../test_drag/mylistwidget.h" #include #include #include #include #include #include using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //add_slide; QWidget *slide_2= ui->slide_2; // 获取ceshi_2_slide控件指针 customB2 = new CustomButton(this); // 创建自定义控件实例 connect(customB2, &CustomButton::click, this, &MainWindow::onWidgetClicked); // 将自定义控件添加到ceshi_2_slide控件中 QHBoxLayout *layout2 = new QHBoxLayout(slide_2); layout2->setContentsMargins(0, 0, 0, 0); layout2->addWidget(customB2); layout2->setSpacing(0); //add_slide; QWidget *slide_3= ui->slide_3; // 获取ceshi_2_slide控件指针 customB3 = new CustomButton(this); // 创建自定义控件实例 connect(customB3, &CustomButton::click, this, &MainWindow::onWidgetClicked); // 将自定义控件添加到ceshi_2_slide控件中 QHBoxLayout *layout3 = new QHBoxLayout(slide_3); layout3->setContentsMargins(0, 0, 0, 0); layout3->addWidget(customB3); layout3->setSpacing(0); //add_slide; QWidget *slide_4= ui->slide_4; // 获取ceshi_2_slide控件指针 customB4 = new CustomButton(this); // 创建自定义控件实例 connect(customB4, &CustomButton::click, this, &MainWindow::onWidgetClicked); // 将自定义控件添加到ceshi_2_slide控件中 QHBoxLayout *layout4 = new QHBoxLayout(slide_4); layout4->setContentsMargins(0, 0, 0, 0); layout4->addWidget(customB4); layout4->setSpacing(0); //初始化右侧u // MyListWidget* my1=new MyListWidget(); // QVBoxLayout *layout5 = new QVBoxLayout(); // layout5->addWidget(my1); // ui->test_frame->setLayout(layout5); // connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted); my1=new MyListWidget(); my1->setObjectName("listWidget"); QVBoxLayout *layout5 = new QVBoxLayout(); layout5->addWidget(my1); ui->frame->setLayout(layout5); qDebug()<<"widget_group"<widget_group1; connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted); connect(my1, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint))); my2=new MyListWidget(); my2->setObjectName("listWidget_2"); QVBoxLayout *layout6 = new QVBoxLayout(); layout6->addWidget(my2); ui->frame_4->setLayout(layout6); connect(my2, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted); connect(my2, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_2_customContextMenuRequested(QPoint))); my3=new MyListWidget(); my3->setObjectName("listWidget_3"); QVBoxLayout *layout7 = new QVBoxLayout(); layout7->addWidget(my3); ui->frame_5->setLayout(layout7); connect(my3, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted); connect(my3, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_3_customContextMenuRequested(QPoint))); my4=new MyListWidget(); my4->setObjectName("listWidget_4"); QVBoxLayout *layout8 = new QVBoxLayout(); layout8->addWidget(my4); ui->frame_6->setLayout(layout8); connect(my4, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted); connect(my4, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_4_customContextMenuRequested(QPoint))); QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2"; root_pro_path=folder; qDebug()<comboBox->currentText(); //初始化TreeView QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json"; qDebug()<treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotTreeMenu(QPoint))); //初始化treeview 和 combobox QTreeView *treeView=ui->treeView; QStandardItemModel* model=new QStandardItemModel(treeView); model->setHorizontalHeaderLabels(QStringList()<setModel(model); initTreeView(model->invisibleRootItem(),QJA); qDebug()<<"QJA"<(ui->treeView->model()); connect(tree_model, &QStandardItemModel::dataChanged, this, &MainWindow::onDataChanged); bool connected = connect(&_sender, &Dialog::_mySignal, this, &MainWindow::handleSignal); if(!connected) { // Handle the error qDebug()<<"connect failed"; } //初始化右边 // initRight(QString parentNode,QString childNode); } void QTreeView::mousePressEvent(QMouseEvent *event) { QModelIndex index = indexAt(event->pos()); if (index.isValid() && index.parent().isValid()) { // 这是一个二级列表项,你可以在这里处理点击事件 // 例如,获取二级列表项的数据并进行相应的操作 QVariant data = index.data(Qt::DisplayRole); qDebug() << "Clicked on subitem:" << data.toString(); } else { qDebug() << "Clicked on subitem false"; QTreeView::mousePressEvent(event); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::readFile(const QString& filePath)//设置打开该窗体时检查路径并加载 { // 获取文件所在路径 QString dirPath = QFileInfo(filePath).absolutePath(); qDebug() << dirPath; // 创建路径 QDir dir; if (!dir.mkpath(dirPath)) { qDebug() << "无法创建路径:" << dirPath; return; } // 创建文件 QFile file(filePath); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { qDebug() << "无法创建文件:" << file.errorString(); return; } // 使用QTextStream读取文件内容 QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); // 处理每一行的内容 // TODO: 添加您的逻辑 qDebug() << line; } // 关闭文件 file.close(); } void MainWindow::on_pushButton_clicked() { dialog=new Dialog(this); dialog->setModal(false); dialog->show(); } void MainWindow::on_pushButton_2_clicked() { dialog=new Dialog(this); dialog->setModal(false); dialog->show(); } QJsonObject MainWindow::readJsonFile(const QString& filePath) { // 打开JSON文件 QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "无法打开文件:" << file.errorString(); return QJsonObject(); } // 读取文件内容 QByteArray jsonData = file.readAll(); file.close(); // 解析JSON数据 QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); if (!jsonDoc.isObject()) { qDebug() << "无效的JSON文件"; return QJsonObject();; } // 获取JSON对象 QJsonObject jsonObj = jsonDoc.object(); return jsonObj; // 处理其他字段... } //初始化treeview 和combobox控件 void MainWindow::initTreeView(QStandardItem* parentItem,const QJsonArray& jsonArray) { QStringList pro_list; for (QJsonArray::const_iterator it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it) { QJsonValue jsonValue = *it; qDebug()<appendRow(item); // 处理字符串值 s } else if (jsonValue.isDouble()) { double doubleValue = jsonValue.toDouble(); // 处理双精度浮点数值 } else if (jsonValue.isObject()) { QJsonObject objectValue = jsonValue.toObject(); QString name = objectValue["name"].toString(); QStandardItem* item = new QStandardItem(name); parentItem->appendRow(item); QJsonArray childrenArray = objectValue["children"].toArray(); pro_list.append(name); // 处理对象值 initTreeView(item, childrenArray); } } ui->treeView->clearSelection(); ui->treeView->setCurrentIndex(QModelIndex()); } void MainWindow::slotTreeMenu(const QPoint &pos) { // QModelIndex curItem = ui->treeView->indexAt( pos ); // QModelIndex index = curItem.sibling(curItem.row(),0); // if( !index.isValid() ) // return; // QMenu *popMenu = new QMenu( this ); // QAction *deleteSeed = new QAction(tr("Delete"), this); // QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\ // QMenu::item{padding:3px 20px 3px 20px;}\ // QMenu::indicator{width:13px;height:13px;}\ // QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\ // QMenu::separator{height:1px;background:#757575;}"; // popMenu->setStyleSheet(qss); // popMenu->addAction( deleteSeed ); // connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( slotTreeMenuDelete()) ); // popMenu->exec( QCursor::pos() ); // delete popMenu; } //删除设定为假删除,在窗体退出的时候才更新json文件,并执行删除操作 /////////到这里 /// /// /// //// /// /// /// // /// // /// / /// /// // void MainWindow::slotTreeMenuDelete() { // QModelIndex curIndex = ui->treeView->currentIndex(); // QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index // qDebug()<(ui->treeView->model()); // model->removeRow(index.row(), index.parent()); } void MainWindow::initComboBox(QStringList qs){ ui->comboBox->clear(); ui->comboBox->addItems(qs); } void MainWindow::initRight(QString parentNode,QString childNode){ // QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2"; QString currentPro = ui->comboBox->currentText(); config_json_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode+"/config.json"; treeview_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode; qDebug()<dir==Qt::AlignLeft){ temb->pos_right_button(); } } if(used=="0"&&temb) { if(temb->dir==Qt::AlignRight){ temb->pos_left_button(); } } initRightListwidget(rightLw,jsonArray); } } void MainWindow::on_treeView_clicked(const QModelIndex &index) { // 获取第一级节点的信息 if(index.parent().isValid()) { QModelIndex parentIndex = index.parent(); QString parentData = parentIndex.data().toString(); qDebug() << "First-level Node: " << parentData; // 获取第二级节点的信息 QString childData = index.data().toString(); qDebug() << "Second-level Node: " << childData; initRight(parentData,childData); } } void MainWindow::initRightListwidget(QListWidget* qw,const QJsonArray& qja) { QStringList dataList; for (const QJsonValue& value : qja) { if (value.isString()) { QString stringValue = value.toString(); dataList.append(stringValue); } } qDebug()<<"initRightListwidget"; qDebug()<<"dataList"<clear(); qw->addItems(dataList); } /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件 void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos) { qDebug()<<"1111111"; QListWidgetItem* curItem = my1->itemAt( pos ); if( curItem == NULL ) return; QMenu *popMenu = new QMenu( this ); QAction *deleteSeed = new QAction(tr("Delete"), this); QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\ QMenu::item{padding:3px 20px 3px 20px;}\ QMenu::indicator{width:13px;height:13px;}\ QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\ QMenu::separator{height:1px;background:#757575;}"; popMenu->setStyleSheet(qss); popMenu->addAction( deleteSeed ); this->qlw=my1; connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) ); popMenu->exec( QCursor::pos() ); delete popMenu; delete deleteSeed; } void MainWindow::on_listWidget_2_customContextMenuRequested(const QPoint &pos) { QListWidgetItem* curItem = my2->itemAt( pos ); if( curItem == NULL ) return; QMenu *popMenu = new QMenu( this ); QAction *deleteSeed = new QAction(tr("Delete"), this); popMenu->addAction( deleteSeed ); this->qlw=my2; connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot())); popMenu->exec( QCursor::pos() ); delete popMenu; delete deleteSeed; } /////daozheer 可以删除项 还没有修改json文件 void MainWindow::deleteSeedSlot() { qDebug()<<"deleteSeedSlot"; QListWidgetItem * item = this->qlw->currentItem(); qDebug()<qlw; if( item == NULL ) return; int curIndex = this->qlw->row(item); qDebug()<qlw->takeItem(curIndex); qDebug()<text(); // updateConfig(ui->listWidget);//更新对应的config.json文件 QString deal_file_path; QString group_name; int group_index; if(qlw->objectName()=="listWidget") { group_name="group1"; group_index=0; } else if(qlw->objectName()=="listWidget_2") { group_name="group2"; group_index=1; } else if(qlw->objectName()=="listWidget_3") { group_name="group3"; group_index=2; } else { group_name="group4"; group_index=3; } deal_file_path=treeview_path+"/"+group_name+"/"+curItem->text(); QFile file(deal_file_path); file.remove(); QJsonObject groupObject = RQJA[group_index].toObject(); QJsonArray childrenArray = groupObject["children"].toArray(); childrenArray.removeAt(curIndex); groupObject["children"] = childrenArray; RQJA[group_index] = groupObject; writeBackToConfigJson(); delete item; } void MainWindow::on_listWidget_3_customContextMenuRequested(const QPoint &pos) { QListWidgetItem* curItem = my3->itemAt( pos ); if( curItem == NULL ) return; QMenu *popMenu = new QMenu( this ); QAction *deleteSeed = new QAction(tr("Delete"), this); popMenu->addAction( deleteSeed ); this->qlw=my3; connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) ); popMenu->exec( QCursor::pos() ); delete popMenu; delete deleteSeed; } void MainWindow::on_listWidget_4_customContextMenuRequested(const QPoint &pos) { QListWidgetItem* curItem = my4->itemAt( pos ); if( curItem == NULL ) return; QMenu *popMenu = new QMenu( this ); QAction *deleteSeed = new QAction(tr("Delete"), this); popMenu->addAction( deleteSeed ); this->qlw=my4; connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) ); popMenu->exec( QCursor::pos() ); delete popMenu; delete deleteSeed; } /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件 //滑块点击slot函数 void MainWindow::onWidgetClicked() { QObject *senderObj = sender(); // 获取发出信号的对象指针 if(RQJA.empty())return; if (senderObj) { QWidget *clickedWidget = qobject_cast(senderObj); // 将指针转换为QWidget指针 if (clickedWidget) { QString widgetName = clickedWidget->parentWidget()->objectName(); // 获取控件的对象名称 if(widgetName=="slide_2") { if(customB2->dir==Qt::AlignLeft) { //变为1 QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"]; selectedValueRef = "1"; qDebug()<dir==Qt::AlignLeft) { //变为1 QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"]; selectedValueRef = "1"; qDebug()<dir==Qt::AlignLeft) { //变为1 QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"]; selectedValueRef = "1"; qDebug()<comboBox->currentText(); //初始化TreeView QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json"; QJO=tool::readJsonFile(jsonPath); // 从JSON对象中提取数据 QJA = QJO.value("children").toArray(); ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu); //初始化treeview 和 combobox QTreeView *treeView=ui->treeView; QStandardItemModel* model=new QStandardItemModel(treeView); model->setHorizontalHeaderLabels(QStringList()<setModel(model); initTreeView(model->invisibleRootItem(),QJA); qDebug()<<"QJA"<(ui->treeView->model()); connect(tree_model, &QStandardItemModel::dataChanged, this, &MainWindow::onDataChanged); } void MainWindow::clearRight() { my1->clear(); my2->clear(); my3->clear(); my4->clear(); this->customB2->pos_left_button(); this->customB3->pos_left_button(); this->customB4->pos_left_button(); } void MainWindow::handleOperationCompleted(QString fromfilePath) { QStringList parts = fromfilePath.split("/"); QObject *senderObj = sender(); QListWidget *LW = qobject_cast(senderObj); //QString tofilePath1 = "/Users/lal/QT_pro/build-test_tool1-Qt_6_3_1_for_macOS-Debug/test_tool1.app/Contents/MacOS/~/Documents/RedInterstitialData/HotUpdate2/pro2/tes1/version1/group3"; QString group_name; int group_index; if(LW->objectName()=="listWidget") { group_name="group1"; group_index=0; } else if(LW->objectName()=="listWidget_2") { group_name="group2"; group_index=1; } else if(LW->objectName()=="listWidget_3") { group_name="group3"; group_index=2; } else { group_name="group4"; group_index=3; } QString tofilePath1 = treeview_path+"/"+group_name; QFile sourceFile(fromfilePath); QFile targetFile(tofilePath1 + "/" +parts[parts.size()-1]); if (sourceFile.exists() && QDir(tofilePath1).exists()) { if (sourceFile.copy(targetFile.fileName())) { qDebug() << "File copied successfully."; LW->addItem(parts[parts.size()-1]); qDebug()<parent(); QJsonObject groupObject = RQJA[group_index].toObject(); QJsonArray childrenArray = groupObject["children"].toArray(); childrenArray.append(parts[parts.size()-1]); groupObject["children"] = childrenArray; RQJA[group_index] = groupObject; writeBackToConfigJson(); } else { qDebug() << "Failed to copy file."; QMessageBox::information(this, "失败", "文件名重复"); } } else { qDebug() << "Source file or target folder does not exist."; } } void MainWindow::on_pushButton_4_clicked() { QList items1; QStandardItemModel *model1 = static_cast(ui->treeView->model()); // QStandardItem* item2 = new QStandardItem(QStringLiteral("新建测试")); QDateTime currentDateTime = QDateTime::currentDateTime(); QString currentDateTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss"); QStandardItem* item2 = new QStandardItem(currentDateTimeString); QString add_Path=root_pro_path+"/"+ui->comboBox->currentText(); QDir dir; if (!dir.exists(add_Path+"/"+currentDateTimeString)) { if (dir.mkpath(add_Path+"/"+currentDateTimeString)) { qDebug() << "Folder created successfully."; QString filePath = add_Path+"/"+"Hotupdate.json"; QFile file(filePath); if (!file.open(QIODevice::ReadOnly)) { QString errorString = file.errorString(); qDebug() << "Failed to open file: " << errorString; // 其他错误处理逻辑 } QByteArray jsonData = file.readAll(); file.close(); QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); if (jsonDoc.isNull()) { qDebug() << "Failed to parse JSON data."; return; } // 将 JSON 数据转换为对象 QJsonObject jsonObj = jsonDoc.object(); // 使用 jsonObj 进行进一步操作,例如读取特定的键值对 QJsonObject newQ; QJsonArray newA; newQ.insert("children",newA); newQ.insert("name",currentDateTimeString); QJsonArray qaa=jsonObj["children"].toArray(); qaa.append(newQ); jsonObj["children"]=qaa; QJA=qaa; tool::writeJsonFile(filePath,jsonObj); items1.append(item2); model1->appendRow(items1); } else { qDebug() << "Failed to create folder."; } } else { qDebug() << "Folder already exists."; } } void MainWindow::on_pushButton_5_clicked() { // 获取选择模型 QItemSelectionModel* selectionModel = ui->treeView->selectionModel(); if (selectionModel) { // 获取当前选中的节点的索引 QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); QModelIndex parentIndex = selectedIndexes.first().parent(); if(parentIndex.isValid()) { QMessageBox::information(this, "提示", "请选中一级节点"); return; } // 遍历选中的索引列表 foreach (const QModelIndex& index, selectedIndexes) { // 打印选中节点的数据 QVariant data = index.data(Qt::DisplayRole); qDebug() << "Selected Node Data: " << data.toString(); QString add_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+data.toString(); QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json"; //直接对新增的版本添加config.json group1 group2 group3 group4 qDebug()<(ui->treeView->model()); if (model) { QStandardItem* parentItem = model->itemFromIndex(index); if (parentItem) { QStandardItem* childItem = new QStandardItem("version"+folderCount); parentItem->appendRow(childItem); } } } } } void MainWindow::on_comboBox_customContextMenuRequested(const QPoint &pos) { } void MainWindow::on_pushButton_9_clicked() { // qDebug()<treeView->currentIndex(); // 获取被选中的节点 QModelIndex selectedIndex = ui->treeView->currentIndex(); if(!selectedIndex.isValid()) { // 如果没有选中任何节点,则弹出提示框 QMessageBox::information(this, "提示", "没有选中任何节点"); return; } // 检查选中的节点是一级节点还是二级节点 if(selectedIndex.parent().isValid()) { // 如果选中的节点有父节点,那么它是二级节点 // 在这里执行二级节点对应的代码... QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(), QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel); if (reply == QMessageBox::Yes) { qDebug() << "Yes was clicked"; qDebug() << "选中的是二级节点"; QModelIndex parentIndex = selectedIndex.parent(); QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+parentIndex.data().toString()+"/"+selectedIndex.data().toString(); QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json"; //删除文件夹 QDir dir(del_Path); dir.removeRecursively(); //修改json文件 QFile file1(modify_pro_config); if (!file1.open(QIODevice::ReadOnly)) { QString errorString = file1.errorString(); qDebug() << "Failed to open file: " << errorString; // 其他错误处理逻辑 } QByteArray jsonData = file1.readAll(); file1.close(); QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); if (jsonDoc.isNull()) { qDebug() << "Failed to parse JSON data."; return; } // 将 JSON 数据转换为对象 QJsonObject jsonObj = jsonDoc.object(); QJsonArray parentArray = jsonObj["children"].toArray(); QJsonObject childObject = parentArray[parentIndex.row()].toObject(); QJsonArray nestedArray = childObject["children"].toArray(); nestedArray.removeAt(selectedIndex.row()); childObject["children"] = nestedArray; parentArray[parentIndex.row()] = childObject; jsonObj["children"] = parentArray; qDebug() << "Modified QJsonObject:" << jsonObj; tool::writeJsonFile(modify_pro_config,jsonObj); QStandardItemModel* model = dynamic_cast(ui->treeView->model()); if (model) { QStandardItem* parentItem = model->itemFromIndex(parentIndex); if (parentItem) { parentItem->removeRow(selectedIndex.row()); } } } else if (reply == QMessageBox::No) { qDebug() << "No was clicked"; } else { qDebug() << "Cancel was clicked"; } } else { // 如果选中的节点没有父节点,那么它是一级节点 // 在这里执行一级节点对应的代码... QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(), QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel); if (reply == QMessageBox::Yes) { qDebug() << "Yes was clicked"; qDebug() << "选中的是一级节点"; QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+selectedIndex.data().toString(); QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json"; //删除文件夹 QDir dir(del_Path); dir.removeRecursively(); //修改json文件 QFile file1(modify_pro_config); if (!file1.open(QIODevice::ReadOnly)) { QString errorString = file1.errorString(); qDebug() << "Failed to open file: " << errorString; // 其他错误处理逻辑 } QByteArray jsonData = file1.readAll(); file1.close(); QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); if (jsonDoc.isNull()) { qDebug() << "Failed to parse JSON data."; return; } // 将 JSON 数据转换为对象 QJsonObject jsonObj = jsonDoc.object(); QJsonArray parentArray = jsonObj["children"].toArray(); parentArray.removeAt(selectedIndex.row()); jsonObj["children"] = parentArray; qDebug() << "Modified QJsonObject:" << jsonObj; tool::writeJsonFile(modify_pro_config,jsonObj); QStandardItemModel* model = dynamic_cast(ui->treeView->model()); model->removeRow(selectedIndex.row()); } else if (reply == QMessageBox::No) { qDebug() << "No was clicked"; } else { qDebug() << "Cancel was clicked"; } } } void MainWindow::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { Q_UNUSED(bottomRight); Q_UNUSED(roles); // 获取节点的文本 QString text = topLeft.data().toString(); qDebug()<<"datachanged"; if (!topLeft.isValid()) return; // 获取父节点 QModelIndex parentIndex = topLeft.parent(); if (!parentIndex.isValid()) { // 如果父节点无效,那么这是一个一级节点 qDebug() << "First-level node text changed: " << topLeft.data().toString(); qDebug() << "Index in tree: " << topLeft.row(); QJsonObject temqjo=QJA[topLeft.row()].toObject(); QString lastname=temqjo["name"].toString();qDebug()<comboBox->currentText()+"/"+lastname; QString newFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+topLeft.data().toString(); QDir dir; if (!dir.rename(oldFolderPath, newFolderPath)) { QMessageBox::information(this, "提示", "命名重复"); qWarning() << "Failed to rename folder from" << oldFolderPath << "to" << newFolderPath; QStandardItemModel* model = dynamic_cast(ui->treeView->model()); QStandardItem* selectedItem = model->itemFromIndex(topLeft); selectedItem->setText(lastname); return; } QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json"; qDebug()<comboBox->currentText()+"/"+parentIndex.data().toString()+"/"+lastname; QString newFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+ parentIndex.data().toString()+"/"+topLeft.data().toString(); QDir dir; QString filename=topLeft.data().toString(); if (!dir.rename(oldFolderPath, newFolderPath)) { QMessageBox::information(this, "提示", "命名重复"); qWarning() << "Failed to rename folder from" << oldFolderPath << "to" << newFolderPath; QStandardItemModel* model = dynamic_cast(ui->treeView->model()); QStandardItem* selectedItem = model->itemFromIndex(topLeft); selectedItem->setText(lastname); return; } QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json"; qDebug()<