12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "SpinBoxDelegate.h"
- #include <QComboBox>
- #include <QDebug>
- #include <QMessageBox>
- #include <QSettings>
- SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : QStyledItemDelegate(parent)
- {
- QSettings config("config.ini", QSettings::IniFormat);
- config.setIniCodec("UTF-8");
- QStringList techMessaureValues = config.value("USERCONFIG/TechMessaureConfig", {}).toStringList();
- bool empty = true;
- if (!techMessaureValues.isEmpty()) {
- bool ok = false;
- for (auto &v : techMessaureValues) {
- v.toInt(&ok);
- if (!ok) {
- empty = true;
- break;
- } else {
- empty = false;
- }
- }
- }
- if (empty) {
- QMessageBox::warning(nullptr, "配置数据无效", "技术措施重要度输入参数配置无效,恢复默认值");
- list << "9"
- << "7"
- << "5"
- << "3"
- << "0"
- << "-3"
- << "-5";
- } else {
- list = techMessaureValues;
- }
- }
- QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
- const QModelIndex & /* index */) const
- {
- QComboBox *editor = new QComboBox(parent);
- editor->addItems(list);
- return editor;
- }
- void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
- {
- QString value = index.model()->data(index, Qt::EditRole).toString();
- QComboBox *spinBox = static_cast<QComboBox *>(editor);
- spinBox->setCurrentText(value);
- }
- void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
- {
- QComboBox *spinBox = static_cast<QComboBox *>(editor);
- QString value = spinBox->currentText();
- if (!list.contains(value)) {
- QMessageBox::warning(nullptr, "输入无效", "只可填写9、7、5、3、0、-3、-5");
- value = "0";
- }
- model->setData(index, value, Qt::EditRole);
- emit dataChanged(index);
- }
- void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
- const QModelIndex & /* index */) const
- {
- editor->setGeometry(option.rect);
- }
|