#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 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_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_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_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, &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"<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; // 处理其他字段... } void MainWindow::init() { QString filePath = "~/Documents/RedInterstitialData/Hotupdate.tool/Hotupdate1.json"; QJA=tool::getJsanArra(filePath); // 从JSON对象中提取数据 int num=QJA.count(); qDebug() << num; QStringList pro_list; for(int i=1;i<=num;i++) { pro_list.append(QString::number(i)); } ui->comboBox->clear(); ui->comboBox->addItems(pro_list); ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu); //初始化当前窗口 int index=ui->comboBox->currentData().toString().toInt(); inittreeview(QJA[index]); } void MainWindow::init2() { // QString filePath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json"; // QString folder = "~/Documents/RedInterstitialData/Hotupdate2"; // tool::generateFolderJsonFile(folder,filePath); // QString jsonPath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json"; // QJO=tool::readJsonFile(jsonPath); // // 从JSON对象中提取数据 // QJA = QJO.value("children").toArray(); // ui->comboBox->clear(); // 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); // QStringList pro_list; // ui->comboBox->clear(); // pro_list=initializeTreeView_conboBox(model->invisibleRootItem(),QJA); // ui->comboBox->addItems(pro_list); } //初始化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); } } } void MainWindow::inittreeview(QJsonValueRef ref) { //1,构造Model,这里示例具有3层关系的model构造过程 QStandardItemModel* model = new QStandardItemModel(ui->treeView); model->setHorizontalHeaderLabels(QStringList()< items1; QStandardItem* item1 = new QStandardItem(QString::number(i)); QStandardItem* item2 = new QStandardItem(QStringLiteral("一级节点")); items1.append(item1); items1.append(item2); model->appendRow(items1); for(int j=0;j<5;j++) { //二级节点,加入第1个一级节点 QList items2; QStandardItem* item3 = new QStandardItem(QString::number(j)); QStandardItem* item4 = new QStandardItem(QStringLiteral("二级节点")); items2.append(item3); items2.append(item4); item1->appendRow(items2); } } //2,给QTreeView应用model ui->treeView->setModel(model); } void MainWindow::on_pushButton_4_clicked() { QList items1; QStandardItemModel *model1 = static_cast(ui->treeView->model()); QStandardItem* item2 = new QStandardItem(QStringLiteral("新建测试")); items1.append(item2); model1->appendRow(items1); } void MainWindow::slotTreeMenu(const QPoint &pos) { 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;}"; QMenu menu; menu.setStyleSheet(qss); //可以给菜单设置样式 QModelIndex curIndex = ui->treeView->indexAt(pos); //当前点击的元素的index QModelIndex index = curIndex.sibling(curIndex.row(),0); //该行的第1列元素的index if (index.isValid()) { //可获取元素的文本、data,进行其他判断处理 //QStandardItem* item = mModel->itemFromIndex(index); //QString text = item->text(); //QVariant data = item->data(Qt::UserRole + 1); //... //添加一行菜单,进行展开 menu.addAction(QStringLiteral("删除"), this, SLOT(slotTreeMenuDelete(bool))); } menu.exec(QCursor::pos()); //显示菜单 } void MainWindow::slotTreeMenuExpand(bool checked) { QModelIndex curIndex = ui->treeView->currentIndex(); QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index if(index.isValid()) { ui->treeView->expand(index); } } void MainWindow::slotTreeMenuCollapse(bool checked) { QModelIndex curIndex = ui->treeView->currentIndex(); QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index if(index.isValid()) { ui->treeView->collapse(index); } } //删除设定为假删除,在窗体退出的时候才更新json文件,并执行删除操作 /////////到这里 /// /// /// //// /// /// /// // /// // /// / /// /// // void MainWindow::slotTreeMenuDelete(bool checked) { QModelIndex curIndex = ui->treeView->currentIndex(); QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index qDebug()<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"<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."; } }