QLineEdit使用Regex小剖析

來源:互聯網
上載者:User

標籤:使用   io   strong   代碼   sp   line   new   on   ad   

1.使用Regex檢驗QLineEdit的輸入範圍(代碼如下):

#include <qvalidator.h>

#include <qlineedit.h>

 

QLineEdit *lineEdit = new QLineEdit(this);

QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");   //^[1-9][0-9]*$ 和 ^[1-9]{1}[/d]*$    

                                            //上面的Regex表示只能輸入大於0的正整數

lineEdit->setValidator(new QRegExpValidator(regExp, this));

 

#####或者用這個

QLineEdit只輸入字母和數字 收藏

        QRegExp regx("[a-zA-Z0-9]+$");

        QValidator *validator = new QRegExpValidator(regx, lined );

        ui->lined->setValidator( validator );

 

2.使用 QIntValidator 整型數驗證控制項控制QLineEdit的輸入範圍(代碼如下):

  #include <qvalidator.h>

  #include <qlineedit.h>

  QValidator* validator = new QIntValidator( 100, 999, this );

  QLineEdit* edit = new QLineEdit( this );

      // 只允許 edit 輸入整數 100 到 999

  edit->setValidator( validator );

  

lineedit = QLineEdit() 執行個體化一個輸入框

 

lineedit =setReadOnly(True) #設定為唯讀

 

lineedit.setDragEnabled(True) #設定能接受拖放

 

lineedit.setMaxLength(5) #設定最大長度

 

lineedit.selectAll() #全選

 

lineedit.setFocus() #得到焦點

 

lineedit.setInputMask("dx") #設定修飾 該輸入框必須輸入兩個字元

 

punctuationRe = QRegExp(r"[ ,;:.]") #得到一個regexp對象 可用下面的驗證

 

lineedit.setValidator(QRegExpValidator(QRegExp(r"[0-9]+")),self) #設定驗證 檢驗使用者輸入內容

 

lineedit.emit(SIGNAL(‘textChanged(QString)‘)) 發出 訊號 (設定為唯讀時貌似發不出 沒有具體測試)

 

lineedit.emit(SIGNAL(textEdited(QString)‘)) 發出 訊號 如果設定了驗證 該訊號在通過驗證才能發出 (設定為唯讀時貌似發不出 沒有具體測試)

QLineEdit使用Regex小剖析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.