(资料图片仅供参考)
不废话 直接上代码
private slots: void seeCardInfo(QTreeWidgetItem*,int); void on_lineEdit_fix_code_textChanged(const QString &arg1); void on_pushButton_clicked(); void dialogMenuSlot(QAction *action); void on_treeWidget_customContextMenuRequested(const QPoint &pos);private: void _init(); //override; QStringList names; QString JsonPath; QMenu *fixcard_Menu; QAction *fixcard_addAction; QAction *fixcard_delAction; QAction *fixcard_chageAction;void MainWindow::seeCardInfo(QTreeWidgetItem *item, int a){ ui->lineEdit_fix_code->clear();ui->lineEdit_lru->clear(); ui->lineEdit_fix_time->clear();ui->lineEdit_fix_item->clear(); ui->lineEdit_person_need->clear();ui->lineEdit_tool->clear(); ui->plainTextEdit_supplies->clear();ui->plainTextEdit_notice->clear(); ui->plainTextEdit_fix_standard->clear();ui->plainTextEdit_fix_step->clear(); if(item->text(a).contains("json")){ QFile loadFile(QString("./cfg/fault_diagnose_cfg/fix_card/%1/%2").arg(item->parent()->text(0)).arg(item->text(a))); JsonPath = QString("./cfg/fault_diagnose_cfg/fix_card/%1/%2").arg(item->parent()->text(0)).arg(item->text(a)); qDebug()<< JsonPath; if(!loadFile.open(QIODevice::ReadOnly)) { qDebug() << "could"t open projects json"; return; } QByteArray allData = loadFile.readAll(); loadFile.close(); QJsonParseError jsonError; QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &jsonError)); if(jsonError.error != QJsonParseError::NoError) { qDebug() << "json error!" << jsonError.errorString(); return; } QJsonObject rootObj = jsonDoc.object(); if(rootObj.contains("fix_code")){ QJsonValue value = rootObj.value("fix_code"); // 获取指定 key 对应的 value1 if(value.isString()){ QString fixCode = value.toString(); ui->lineEdit_fix_code->setText(fixCode); } } if(rootObj.contains("lru_name")){ QJsonValue value = rootObj.value("lru_name"); // 获取指定 key 对应的 value2 if(value.isString()){ QString lruName = value.toString(); ui->lineEdit_lru->setText(lruName); } } if(rootObj.contains("fix_time")){ QJsonValue value = rootObj.value("fix_time"); // 获取指定 key 对应的 value3 if(value.isString()){ QString fixTime = value.toString(); ui->lineEdit_fix_time->setText(fixTime); } } if(rootObj.contains("fix_name")){ QJsonValue value = rootObj.value("fix_name"); // 获取指定 key 对应的 value4 if(value.isString()){ QString fixName = value.toString(); ui->lineEdit_fix_item->setText(fixName); } } if(rootObj.contains("person_need")){ QJsonValue value = rootObj.value("person_need"); // 获取指定 key 对应的 value5 if(value.isString()){ QString person = value.toString(); ui->lineEdit_person_need->setText(person); } } if(rootObj.contains("tool")){ QJsonValue value = rootObj.value("tool"); // 获取指定 key 对应的 value6 if(value.isString()){ QString tool = value.toString(); ui->lineEdit_tool->setText(tool); } } if(rootObj.contains("supplies")){ QJsonValue value = rootObj.value("supplies"); // 获取指定 key 对应的 value7 if(value.isString()){ QString supplies = value.toString(); ui->plainTextEdit_supplies->appendPlainText(supplies); } } if(rootObj.contains("notice")){ QJsonValue value = rootObj.value("notice"); // 获取指定 key 对应的 value8 if(value.isString()){ QString notice = value.toString(); ui->plainTextEdit_notice->appendPlainText(notice); } } if(rootObj.contains("fix_standard")){ QJsonValue value = rootObj.value("fix_standard"); // 获取指定 key 对应的 value9 if(value.isString()){ QString fixStandard = value.toString(); ui->plainTextEdit_fix_standard->appendPlainText(fixStandard); } } if(rootObj.contains("fix_step")){ QJsonValue value = rootObj.value("fix_step"); // 获取指定 key 对应的 value10 if(value.isString()){ QString fixStep = value.toString(); ui->plainTextEdit_fix_step->appendPlainText(fixStep); } } } else{ return; }}void MainWindow::_init(){ QString path = "./cfg/fault_diagnose_cfg/fix_card"; ui->pushButton->hide(); QDir dir(path); names = dir.entryList(QDir::Dirs); names.removeOne("."); names.removeOne(".."); for(int i = 0; i < names.size(); i++){ QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget); item->setText(0, names.at(i)); QString strPath = QString("./cfg/fault_diagnose_cfg/fix_card/%1").arg(names.at(i)); QDir strDir(strPath); QStringList strNames = strDir.entryList(QDir::Files); for(int i = 0; i < strNames.size(); i++){ QTreeWidgetItem *item1 = new QTreeWidgetItem(item); item1->setText(0, strNames.at(i)); item->addChild(item1); } } QObject::connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(seeCardInfo(QTreeWidgetItem*,int))); ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); fixcard_Menu = new QMenu("Test",ui->treeWidget); fixcard_addAction = new QAction("新增工卡",fixcard_Menu); fixcard_delAction = new QAction("删除工卡",fixcard_Menu); fixcard_chageAction = new QAction("修改工卡",fixcard_Menu); fixcard_Menu->addAction(fixcard_addAction); fixcard_Menu->addAction(fixcard_delAction); fixcard_Menu->addAction(fixcard_chageAction); QObject::connect(fixcard_Menu,SIGNAL(triggered(QAction*)),this,SLOT(dialogMenuSlot(QAction*)));}void MainWindow::on_pushButton_clicked(){ QString fix_code = ui->lineEdit_fix_code->text(); QString lru_name = ui->lineEdit_lru->text(); QString fix_time = ui->lineEdit_fix_time->text(); QString fix_name = ui->lineEdit_fix_item->text(); QString person_need = ui->lineEdit_person_need->text(); QString tool= ui->lineEdit_tool->text(); QString supplies = ui->plainTextEdit_supplies->toPlainText(); QString notice = ui->plainTextEdit_notice->toPlainText(); QString fix_standard = ui->plainTextEdit_fix_standard->toPlainText(); QString fix_step = ui->plainTextEdit_fix_step->toPlainText(); QFile loadFile(JsonPath); if(!loadFile.open(QIODevice::ReadWrite)) { qDebug() << "could"t open projects json"; return; } QByteArray allData = loadFile.readAll(); QJsonParseError jsonError; QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &jsonError)); if(jsonError.error != QJsonParseError::NoError) { qDebug() << "json error!" << jsonError.errorString(); return; } QJsonObject rootObj = jsonDoc.object(); rootObj.insert("fix_code", fix_code); rootObj.insert("lru_name", lru_name); rootObj.insert("fix_time", fix_time); rootObj.insert("fix_name", fix_name); rootObj.insert("person_need", person_need); rootObj.insert("tool", tool); rootObj.insert("supplies", supplies); rootObj.insert("notice", notice); rootObj.insert("fix_standard", fix_standard); rootObj.insert("fix_step", fix_step); QJsonDocument doc(rootObj); QByteArray data=doc.toJson(); QFile file(JsonPath); file.open(QIODevice::WriteOnly); file.write(data); file.close(); loadFile.close(); QMessageBox::information(this,"提示","保存成功!");}void MainWindow::on_lineEdit_fix_code_textChanged(const QString &arg1){ ui->pushButton->show(); // ui->comboBox->currentText();}void MainWindow::on_treeWidget_customContextMenuRequested(const QPoint &pos){ fixcard_Menu->exec(QCursor::pos());}void MainWindow::dialogMenuSlot(QAction *action){ if(action == fixcard_addAction) { qDebug() <<"1111"<treeWidget->currentItem()->parent()->text(0)<treeWidget->currentItem()->text(0); } else if (action == fixcard_delAction) { } else if (action == fixcard_chageAction) { }}
【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】
点击这里:
关键词: