QLineEdit::setValidator ( const QValidator * v )

本文介绍如何使用 Qt 的 QRegExpValidator 控制 QLineEdit 输入仅限数字的方法。通过设置正则表达式 ^[0-9]*$ 实现了对输入框输入内容的有效验证。
QRegExpValidator *validator = new QRegExpValidator(QRegExp("^[0-9]*$"), this);
this->ui->lineEdit->setValidator(validator);


#ifndef ITEMVIEW_H #define ITEMVIEW_H class ItemView : public QWidget { Q_OBJECT public: explicit ItemView(const QString &key, QWidget *parent = nullptr); explicit ItemView(const QString &key, const QString &rightText, QWidget *parent = nullptr); explicit ItemView(const QString &key, const QString &leftText, const QString &rightText, QWidget *parent = nullptr); QVariant getValue() const; void setValue(const QVariant &newValue); void setSpacing(int spacing); void setMargins(int left, int up, int right, int button); void setValidator(const QValidator *v); signals: void valueChanged(); private: QHBoxLayout *layout; QLabel *keyLabel; QLabel *leftLabel; QLabel *rightLabel; QLineEdit *lineEdit; QVariant value; }; #endif // ITEMVIEW_H #include “itemview.h” ItemView::ItemView(const QString &key, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(lineEdit); this->setLayout(layout); } ItemView::ItemView(const QString &key, const QString &rightText, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); rightLabel = new QLabel(rightText, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(rightLabel); layout->addWidget(lineEdit); this->setLayout(layout); } ItemView::ItemView(const QString &key, const QString &leftText, const QString &rightText, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); leftLabel = new QLabel(leftText, this); rightLabel = new QLabel(rightText, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(leftLabel); layout->addWidget(rightLabel); layout->addWidget(lineEdit); this->setLayout(layout); } QVariant ItemView::getValue() const { value = QVariant(lineEdit->text()); return value; } void ItemView::setValue(const QVariant &newValue) { if (value == newValue) return; value = newValue; lineEdit->setText(value.toString()); emit valueChanged(); } void ItemView::setSpacing(int spacing) { layout->setSpacing(spacing); } void ItemView::setMargins(int left, int up, int right, int button) { layout->setContentsMargins(left, up, right, button); } void ItemView::setValidator(const QValidator *v) { lineEdit->setValidator(v); } 上述代码报错,itemview.cpp:89:11: No viable overloaded ‘=’ qvariant.h:332:15: candidate function not viable: ‘this’ argument has type ‘const QVariant’, but method is not marked const :335:5: note: candidate function not viable: ‘this’ argument has type ‘const QVariant’, but method is not marked const如何修改
03-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值