SpinBoxDelegate.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "SpinBoxDelegate.h"
  2. #include <QComboBox>
  3. #include <QDebug>
  4. #include <QMessageBox>
  5. #include <QSettings>
  6. SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : QStyledItemDelegate(parent)
  7. {
  8. QSettings config("config.ini", QSettings::IniFormat);
  9. config.setIniCodec("UTF-8");
  10. QStringList techMessaureValues = config.value("USERCONFIG/TechMessaureConfig", {}).toStringList();
  11. bool empty = true;
  12. if (!techMessaureValues.isEmpty()) {
  13. bool ok = false;
  14. for (auto &v : techMessaureValues) {
  15. v.toInt(&ok);
  16. if (!ok) {
  17. empty = true;
  18. break;
  19. } else {
  20. empty = false;
  21. }
  22. }
  23. }
  24. if (empty) {
  25. QMessageBox::warning(nullptr, "配置数据无效", "技术措施重要度输入参数配置无效,恢复默认值");
  26. list << "9"
  27. << "7"
  28. << "5"
  29. << "3"
  30. << "0"
  31. << "-3"
  32. << "-5";
  33. } else {
  34. list = techMessaureValues;
  35. }
  36. }
  37. QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  38. const QModelIndex & /* index */) const
  39. {
  40. QComboBox *editor = new QComboBox(parent);
  41. editor->addItems(list);
  42. return editor;
  43. }
  44. void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  45. {
  46. QString value = index.model()->data(index, Qt::EditRole).toString();
  47. QComboBox *spinBox = static_cast<QComboBox *>(editor);
  48. spinBox->setCurrentText(value);
  49. }
  50. void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  51. {
  52. QComboBox *spinBox = static_cast<QComboBox *>(editor);
  53. QString value = spinBox->currentText();
  54. if (!list.contains(value)) {
  55. QMessageBox::warning(nullptr, "输入无效", "只可填写9、7、5、3、0、-3、-5");
  56. value = "0";
  57. }
  58. model->setData(index, value, Qt::EditRole);
  59. emit dataChanged(index);
  60. }
  61. void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  62. const QModelIndex & /* index */) const
  63. {
  64. editor->setGeometry(option.rect);
  65. }