天天看點

Qt 中QLineEdit控件限制輸入類型:整數/浮點數

在MFC程式設計中,我們可以通過設定輸入框的屬性,讓使用者隻能輸入數字。

在QT中的輸入框(QLineEdit)可以通過綁定

QIntValidator :隻能輸入整數

QDoubleValidator:隻能輸入浮點數

QRegExpValidator:隻讓使用者按照正規表達式定義好的樣式進行輸入

對象來控制使用者的輸入。

整數例子

QLineEdit* aEdit = new QLineEdit;
QIntValidator* aIntValidator = new QIntValidator;
aIntValidator->setRange(-50, 50);
aEdit->setValidator(aIntValidator);
           

轉自http://www.07net01.com/program/482396.html

感謝~

浮點數例子

QLineEdit* bEdit = new QLineEdit;
QDoubleValidator* adoubleValidator = new QDoubleValidator(0,500,2,this);
aEdit->setValidator(adoubleValidator);
           

四個參數的意義分别是最小值,最大值,小數位數,繼承于

正規表達式例子

https://blog.csdn.net/qq_40086556/article/details/90722042

繼續閱讀