1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #ifndef MATTERELEMENTITEMDELEGATE_H
- #define MATTERELEMENTITEMDELEGATE_H
- #include <QStyledItemDelegate>
- #include <QFormLayout>
- #include <QDoubleSpinBox>
- class InsideWidget : public QFrame
- {
- Q_OBJECT
- public:
- InsideWidget(QWidget *parent = nullptr) : QFrame(parent)
- {
- QFormLayout *layout = new QFormLayout;
- setStyleSheet("InsideWidget{background-color:gray} QLabel{color:white}");
- layout->setMargin(10);
- layout->setSpacing(2);
- for (int i = 0; i < 4; ++i) {
- auto s = new QDoubleSpinBox;
- // s->setMaximumWidth(40);
- s->setFrame(false);
- s->setMinimum(-10000);
- s->setMaximum(10000);
- m_spins << s;
- }
- layout->addRow("[很差,交差]中间值:", m_spins.at(0));
- layout->addRow("[较差,一般]中间值:", m_spins.at(1));
- layout->addRow("[一般,较高]中间值:", m_spins.at(2));
- layout->addRow("[较高,很高]中间值:", m_spins.at(3));
- setLayout(layout);
- }
- virtual ~InsideWidget() { }
- QString result()
- {
- this->onConfirm();
- return m_result;
- }
- void setValue(const QString &value)
- {
- if (!value.isEmpty()) {
- QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
- for (int i = 0; i < tmp.size(); ++i) {
- m_spins.at(i)->setValue(tmp[i].toDouble());
- }
- }
- }
- private slots:
- void onConfirm()
- {
- m_result = QString("[%1, %2, %3, %4]")
- .arg(m_spins.at(0)->value())
- .arg(m_spins.at(1)->value())
- .arg(m_spins.at(2)->value())
- .arg(m_spins.at(3)->value());
- }
- private:
- QList<QDoubleSpinBox *> m_spins;
- QString m_result;
- };
- class MatterElementItemSpinDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- MatterElementItemSpinDelegate(QObject *parent = 0);
- QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
- void setEditorData(QWidget *editor, const QModelIndex &index) const override;
- void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
- void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
- const QModelIndex &index) const override;
- };
- class MatterElementItemInsideDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- MatterElementItemInsideDelegate(QObject *parent = 0);
- QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
- void setEditorData(QWidget *editor, const QModelIndex &index) const override;
- void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
- void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
- const QModelIndex &index) const override;
- };
- #endif // MATTERELEMENTITEMDELEGATE_H
|