MatrixTableItemDelegate.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "MatrixTableItemDelegate.h"
  2. #include <QHBoxLayout>
  3. #include <QLabel>
  4. #include <QPushButton>
  5. #include <QButtonGroup>
  6. #include <QDebug>
  7. SchemeBar::SchemeBar(const QString &lLabel, const QString &rLabel, const QStringList &vlist, QWidget *parent)
  8. : QDialog(parent), leftLabel(lLabel), rightLabel(rLabel), barValueList(vlist)
  9. {
  10. setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
  11. QHBoxLayout *hlay = new QHBoxLayout;
  12. QLabel *l = new QLabel(leftLabel);
  13. QLabel *r = new QLabel(rightLabel);
  14. QHBoxLayout *centerLay = new QHBoxLayout;
  15. QButtonGroup *btnGroup = new QButtonGroup(this);
  16. for (QString &v : barValueList) {
  17. QPushButton *btn = new QPushButton(v);
  18. centerLay->addWidget(btn);
  19. btnGroup->addButton(btn);
  20. if (v.startsWith("1/")) {
  21. btn->setText(v.split("/")[1]);
  22. } else {
  23. btn->setText(v);
  24. }
  25. btn->setProperty("number", v);
  26. }
  27. connect(btnGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this,
  28. &SchemeBar::barClicked);
  29. hlay->addWidget(l);
  30. hlay->addLayout(centerLay);
  31. hlay->addWidget(r);
  32. setLayout(hlay);
  33. }
  34. void SchemeBar::barClicked(QAbstractButton *btn)
  35. {
  36. emit setValue(btn->property("number").toString());
  37. this->close();
  38. }
  39. MatrixTableItemDelegate::MatrixTableItemDelegate(QWidget *parent) : QStyledItemDelegate(parent) { }
  40. QWidget *MatrixTableItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  41. const QModelIndex &index) const
  42. {
  43. QStringList l = { "9", "7", "5", "3", "1", "0", "1", "3", "5", "7", "9" };
  44. SchemeBar *scheme = new SchemeBar("左", "右", l, parent);
  45. return scheme;
  46. }
  47. void MatrixTableItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { }
  48. void MatrixTableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  49. {
  50. }