QLineEdit 用法总结

  • 文字提示
  • 密码模式
  • 禁止输入即使用代码赋值也无法输入
  • 验证器
    • 验证器类型
    • 验证器 使用方法
  • 掩码
  • 待选项

文字提示

pLineEdit->setPlaceholderText("Password");

密码模式

// 直接隐藏
pPasswordLineEdit->setEchoMode(QLineEdit::Password);
// 输入完成再隐藏
pPasswordEchoOnEditLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);

禁止输入,即使用代码赋值也无法输入

pNoEchoLineEdit->setEchoMode(QLineEdit::NoEcho);

验证器

验证器类型

//整型验证器
QIntValidator
// 浮点型验证器 
QDoubleValidator
// 正则验证器
QRegExpValidator 

验证器 使用方法

QLineEdit *pIntLineEdit = new QLineEdit(this);
QLineEdit *pDoubleLineEdit = new QLineEdit(this);
QLineEdit *pValidatorLineEdit = new QLineEdit(this);

pIntLineEdit->setPlaceholderText(QString::fromLocal8Bit("整形"));
pDoubleLineEdit->setPlaceholderText(QString::fromLocal8Bit("浮点型"));
pValidatorLineEdit->setPlaceholderText(QString::fromLocal8Bit("字母和数字"));

// 整形 范围:[1, 99]
QIntValidator *pIntValidator = new QIntValidator(this);
pIntValidator->setRange(1, 99);

// 浮点型 范围:[-360, 360] 精度:小数点后2位
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
pDoubleValidator->setRange(-360, 360);
pDoubleValidator->setNotation(QDoubleValidator::StandardNotation);
pDoubleValidator->setDecimals(2);

// 字符和数字
QRegExp reg("[a-zA-Z0-9]+$");
QRegExpValidator *pValidator = new QRegExpValidator(this);
pValidator->setRegExp(reg);

pIntLineEdit->setValidator(pIntValidator);
pDoubleLineEdit->setValidator(pDoubleValidator);
pValidatorLineEdit->setValidator(pValidator);

掩码

    QLineEdit *pIPLineEdit = new QLineEdit(this);
    QLineEdit *pMACLineEdit = new QLineEdit(this);
    QLineEdit *pDateLineEdit = new QLineEdit(this);
    QLineEdit *pLicenseLineEdit = new QLineEdit(this);

    QString str = QStringLiteral("0000年00月00日");
    pIPLineEdit->setInputMask(str);//"000.000.000.000;_"
    pIPLineEdit->setFixedWidth(400);
// 末尾的 _ 意思是待填写部分用 _ 字符填充
    pMACLineEdit->setInputMask("HH:HH:HH:HH:HH:HH;_");
    pDateLineEdit->setInputMask("0000-00-00");
    pLicenseLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");

待选项

重写这个虚函数

  void LineEdit::contextMenuEvent(QContextMenuEvent *event)
  {
      QMenu *menu = createStandardContextMenu();
      menu->addAction(tr("My Menu Item"));
      //...
      menu->exec(event->globalPos());
      delete menu;
  }

你可能感兴趣的:(Qt,Qt图像视图框架)