detdialog.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "detdialog.h"
  2. #include "ui_detdialog.h"
  3. DetDialog::DetDialog(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::DetDialog)
  6. {
  7. ui->setupUi(this);
  8. ui->comboType->addItem("盒条码", "case");
  9. ui->comboType->addItem("箱条码", "box");
  10. ui->comboType->setCurrentIndex(-1);
  11. ui->labFeature->clear();
  12. ui->lineFeature->hide();
  13. // 连接信号和槽
  14. connect(ui->comboType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetDialog::onComboTypeIndexChanged);
  15. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &DetDialog::validateInput);
  16. connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &DetDialog::clearFormData);
  17. }
  18. void DetDialog::onComboTypeIndexChanged(int index)
  19. {
  20. if (index != -1) {
  21. ui->lineFeature->show();
  22. QString value = ui->comboType->itemData(index).toString();
  23. if (value == "case") {
  24. ui->labFeature->setText("装盒标准");
  25. } else if (value == "box") {
  26. ui->labFeature->setText("特征码");
  27. }
  28. } else {
  29. ui->labFeature->clear();
  30. ui->lineFeature->hide();
  31. }
  32. }
  33. void DetDialog::validateInput()
  34. {
  35. QString codeType = ui->comboType->currentData().toString();
  36. QString beginCode = ui->lineCode->text().trimmed();
  37. QString featureorCaseCount = ui->lineFeature->text().trimmed();
  38. QString count = ui->lineCount->text().trimmed();
  39. if (codeType.isEmpty() || beginCode.isEmpty() || count.isEmpty()) {
  40. QMessageBox::warning(this, "输入错误", "请填写所有必填字段。");
  41. return;
  42. }
  43. QJsonObject jsonObject;
  44. jsonObject["codeType"] = codeType;
  45. jsonObject["beginCode"] = beginCode;
  46. if (codeType == "case") {
  47. jsonObject["inCaseCount"] = featureorCaseCount;
  48. } else if (codeType == "box") {
  49. jsonObject["feature"] = featureorCaseCount;
  50. }
  51. jsonObject["count"] = count;
  52. QJsonDocument jsonDoc(jsonObject);
  53. qDebug() << jsonDoc;
  54. emit validateDetNum(jsonDoc);
  55. clearFormData(); // 清除表单数据
  56. // this->accept();
  57. }
  58. void DetDialog::clearFormData()
  59. {
  60. QList<QLineEdit*> lineEdits = findChildren<QLineEdit*>();
  61. for (QLineEdit* lineEdit : lineEdits) {
  62. lineEdit->clear();
  63. }
  64. QList<QComboBox*> comboBoxes = findChildren<QComboBox*>();
  65. for (QComboBox* comboBox : comboBoxes) {
  66. comboBox->setCurrentIndex(-1);
  67. }
  68. }
  69. DetDialog::~DetDialog()
  70. {
  71. delete ui;
  72. }