QTableWidget:仅通过委托的数字
QTableWidget: Only numbers via delegate
本文关键字:数字 QTableWidget 更新时间:2023-10-16
我目前正在尝试让我的QTableWidget只显示数字。我读到我需要一个QAbstractItemDelegate来这样做,所以我通读了文档,发现createEditor无效。这是我目前使用的代码:
#include "tabledelegate.h"
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* TableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QLineEdit* editor = new QLineEdit(parent);
QDoubleValidator* val = new QDoubleValidator(editor);
val->setBottom(0);
val->setNotation(QDoubleValidator::StandardNotation);
editor->setValidator(val);
return editor;
}
我试图通过在 MainWindow 的构造函数中执行此操作来调用委托:
ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget));
但它给了我这个错误:
调用 'QTableWidget::setItemDelegate(TableDelegate*)' 时没有匹配函数 ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget)); ^
为什么?
您可以通过子类化 QTableWidgetItem 并重写 setData 方法来实现此行为,而不是使用项委托:
void my_table_item::setData(int role, const QVariant& value)
{
if(role == Qt::EditRole)
{
QObject* parent = 0; // reference call needed for linux
QRegExpValidator rxv(QRegExp("[+-]?\d*\.?\d+"), parent);
int pos = 0;
QString tmp = value.toString(); // reference call needed by linux
if(rxv.validate(tmp,pos) != QValidator::Acceptable) { return; }
}
this->QTableWidgetItem::setData(role, value);
}
注意:仅当用户按 Enter 时验证输入,而不是在用户输入数据时验证。
我自己做的!我犯了一些错误:
- 我忘了在我的头文件中子类QStyledItemDelegate。
- 我忘了重新实现其他三个需要的函数setEditorData(),setModelData() 和 updateEditorGeometry()。
- 我不得不运行一个QMake。
这是我的新来源:
//HEADER
#ifndef TABLEDELEGATE_H
#define TABLEDELEGATE_H
#include <QStyledItemDelegate>
class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
TableDelegate(QObject* parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#endif // TABLEDELEGATE_H
//.CPP
#include "tabledelegate.h"
#include <QLineEdit>
#include <QDoubleValidator>
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* TableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QLineEdit* editor = new QLineEdit(parent);
QDoubleValidator* val = new QDoubleValidator(editor);
val->setBottom(0);
val->setNotation(QDoubleValidator::StandardNotation);
editor->setValidator(val);
return editor;
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
QLineEdit* line = static_cast<QLineEdit*>(editor);
line->setText(QString().setNum(value));
}
void TableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index,value);
}
void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
感谢任何帮助过的人。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- QTableWidget:仅通过委托的数字
- QTableWidget:只允许数字