MatterElementItemDelegate.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef MATTERELEMENTITEMDELEGATE_H
  2. #define MATTERELEMENTITEMDELEGATE_H
  3. #include <QStyledItemDelegate>
  4. #include <QFormLayout>
  5. #include <QDoubleSpinBox>
  6. class InsideWidget : public QFrame
  7. {
  8. Q_OBJECT
  9. public:
  10. InsideWidget(QWidget *parent = nullptr) : QFrame(parent)
  11. {
  12. QFormLayout *layout = new QFormLayout;
  13. setStyleSheet("InsideWidget{background-color:gray} QLabel{color:white}");
  14. layout->setMargin(10);
  15. layout->setSpacing(2);
  16. for (int i = 0; i < 4; ++i) {
  17. auto s = new QDoubleSpinBox;
  18. // s->setMaximumWidth(40);
  19. s->setFrame(false);
  20. s->setMinimum(-10000);
  21. s->setMaximum(10000);
  22. m_spins << s;
  23. }
  24. layout->addRow("[很差,交差]中间值:", m_spins.at(0));
  25. layout->addRow("[较差,一般]中间值:", m_spins.at(1));
  26. layout->addRow("[一般,较高]中间值:", m_spins.at(2));
  27. layout->addRow("[较高,很高]中间值:", m_spins.at(3));
  28. setLayout(layout);
  29. }
  30. virtual ~InsideWidget() { }
  31. QString result()
  32. {
  33. this->onConfirm();
  34. return m_result;
  35. }
  36. void setValue(const QString &value)
  37. {
  38. if (!value.isEmpty()) {
  39. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  40. for (int i = 0; i < tmp.size(); ++i) {
  41. m_spins.at(i)->setValue(tmp[i].toDouble());
  42. }
  43. }
  44. }
  45. private slots:
  46. void onConfirm()
  47. {
  48. m_result = QString("[%1, %2, %3, %4]")
  49. .arg(m_spins.at(0)->value())
  50. .arg(m_spins.at(1)->value())
  51. .arg(m_spins.at(2)->value())
  52. .arg(m_spins.at(3)->value());
  53. }
  54. private:
  55. QList<QDoubleSpinBox *> m_spins;
  56. QString m_result;
  57. };
  58. class MatterElementItemSpinDelegate : public QStyledItemDelegate
  59. {
  60. Q_OBJECT
  61. public:
  62. MatterElementItemSpinDelegate(QObject *parent = 0);
  63. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  64. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  65. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  66. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  67. const QModelIndex &index) const override;
  68. };
  69. class MatterElementItemInsideDelegate : public QStyledItemDelegate
  70. {
  71. Q_OBJECT
  72. public:
  73. MatterElementItemInsideDelegate(QObject *parent = 0);
  74. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  75. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  76. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  77. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  78. const QModelIndex &index) const override;
  79. };
  80. #endif // MATTERELEMENTITEMDELEGATE_H