找回密码
 立即注册
收起左侧

请问tableWidget设置lineEdit代理后怎么清空呢?

0
回复
4905
查看
[复制链接]
累计签到:27 天
连续签到:1 天
来源: 2019-4-23 14:22:11 显示全部楼层 |阅读模式
1Qter豆
使用tableWidget做电子表格,为了防止用户输入不合适的数据,就给tableWidget的item设置了lineEdit代理并添加浮点型验证器,把输入内容限制为“数字”;
目前这个功能实现了,不过还有一个需求就是“在某些情况下,电子表格的内容需要被清空”,但是设置代理后,单元格内容不能清空了,请问怎么解决呢?
相关代码如下,能不能给doubleValidator添加“清空”的验证呢?十分感谢!
  1. QWidget *centralDelegateMotionAxes::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3.     if (index.isValid()) {
  4.         QLineEdit *lineedit = new QLineEdit(parent);
  5.         // 居中显示
  6.         lineedit->setAlignment(Qt::AlignCenter);

  7. lineedit->setStyleSheet(QString(":focus{background-color: lightGray;}")); // TODO: 分析此句代码

  8.         QDoubleValidator *doubleValidator = new QDoubleValidator(-360.00, 360.00, 2); // 无效
  9.         doubleValidator->setBottom(-360.00);
  10.         doubleValidator->setTop(360.00); // 无效
  11.         lineedit->setValidator(doubleValidator);

  12.         lineedit->installEventFilter(const_cast<centralDelegateMotionAxes *>(this)); // TODO: ???
  13.         return lineedit;
  14.     } else {
  15.         return QItemDelegate::createEditor(parent, option, index);
  16.     }
  17. }

  18. void centralDelegateMotionAxes::setEditorData(QWidget *editor, const QModelIndex &index) const
  19. {
  20.     if (index.isValid()) {
  21.         QString value = index.model()->data(index, Qt::DisplayRole).toString();
  22.         QLineEdit *lineedit = static_cast<QLineEdit *>(editor);
  23.         lineedit->setText(value);
  24.     } else {
  25.         QItemDelegate::setEditorData(editor, index);
  26.     }
  27. }

  28. void centralDelegateMotionAxes::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  29. {
  30.     if (index.isValid()) {
  31.         QLineEdit *lineedit = static_cast<QLineEdit *>(editor);
  32.         model->setData(index, lineedit->text());
  33.     } else {
  34.         QItemDelegate::setModelData(editor, model, index);
  35.     }
  36. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了