Qt入门-QLineEdit::setInputMask()

本文详细介绍了如何使用QLineEdit::setInputMask方法来限制和控制Qt编辑控件的输入格式,并结合QLineEdit::setValidator实现更复杂的输入验证,通过实例展示了IP地址、MAC地址及十六进制数的输入限制。

QLineEdit::setInputMask(const QString & inputMask)方法可以使Edit控件只允许输入自定义的格式字符串,inputMask参数设置格式化字符串的掩码。

inputMask可以是下列字符的组合

[plain] view plaincopy
  1. Character   Meaning  
  2. A  
  3.   
  4. ASCII alphabetic character required. A-Z, a-z.  
  5.   
  6. a  
  7.   
  8. ASCII alphabetic character permitted but not required.  
  9.   
  10. N  
  11.   
  12. ASCII alphanumeric character required. A-Z, a-z, 0-9.  
  13.   
  14. n  
  15.   
  16. ASCII alphanumeric character permitted but not required.  
  17.   
  18. X  
  19.   
  20. Any character required.  
  21.   
  22. x  
  23.   
  24. Any character permitted but not required.  
  25.   
  26. 9  
  27.   
  28. ASCII digit required. 0-9.  
  29.   
  30. 0  
  31.   
  32. ASCII digit permitted but not required.  
  33.   
  34. D  
  35.   
  36. ASCII digit required. 1-9.  
  37.   
  38. d  
  39.   
  40. ASCII digit permitted but not required (1-9).  
  41.   
  42. #  
  43.   
  44. ASCII digit or plus/minus sign permitted but not required.  
  45.   
  46. H  
  47.   
  48. Hexadecimal character required. A-F, a-f, 0-9.  
  49.   
  50. h  
  51.   
  52. Hexadecimal character permitted but not required.  
  53.   
  54. B  
  55.   
  56. Binary character required. 0-1.  
  57.   
  58. b  
  59.   
  60. Binary character permitted but not required.  
  61.   
  62. >  
  63.   
  64. All following alphabetic characters are uppercased.  
  65.   
  66. <  
  67.   
  68. All following alphabetic characters are lowercased.  
  69.   
  70. !  
  71.   
  72. Switch off case conversion.  
  73.   
  74. \  
  75.   
  76. Use \ to escape the special characters listed above to use them as separators.  

示例:

000.000.000.000;”是一个IP地址。

“HH:HH:HH:HH:HH:HH;0”是一个MAC地址。

“\\0\\xHHHH;0”是只允许输入一个16位的十六进制数。

 

另外,一个很有用的技巧是组合使用QLineEdit::setInputMask和QLineEdit::setValidator可实现更复杂的输入屏蔽和验证。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值