先上一个漂亮的搜索框效果图, 输入搜索文本效果, 点击搜索图标效果:
//实现代码 void MainWindow::iniLineEdit() {
ui->lineEdit->setPlaceHolderText(QObject::tr("搜索文本)); QAction * contentAct = new QAction(this); contentAct->setIcon(QIcon(":/image/文本.png")); ui->lineEdit->addAction(contentAct, QLineEdit::LeadingPosition); QAction * operateAct = ui->lineEdit->addAction(QIcon(":/image/搜索.png"), QLineEdit::TrailingPosition); connect(operateAct, SIGNAL(triggered(bool)), this, SLOT(slotOperateActTrigged())); } void MainWindow::slotOperateActTrigged() { ui->label->setText(QObject::tr("you search content is %1").arg(ui->lineEdit->text())); }
图片资源:
密码模式, 类似mysql输入密码不显示模式,输入显示为小黑圆圈模式效果,焦点移动到别的部件后显示为小黑圆圈模式效果:
//类似于mysql输入密码效果 ui->lineEdit->setEchoMode(QLineEdit::NoEcho); //ui->label->setText(QObject::tr("you have input %1").arg(ui->lineEdit->text())); 这种方式取不到lineEdit里面的内容 connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotLineEditTextChanged(QString))); void MainWindow::slotLineEditTextChanged(QString text) { ui->label->setText(QObject::tr("you have input %1").arg(text)); } //密码小黑圆圈效果 ui->lineEdit->setEchoMode(QLineEdit::Password); connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(slotLineEditEditingFinished())); void MainWindow::slotLineEditEditingFinished() { ui->label->setText(QObject::tr("you have input %1").arg(ui->lineEdit->text())); } //输入时正常显示,失去焦点时,显示为小黑圆圈 ui->lineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
文字提示效果:
ui->lineEdit->setPlaceholderText(QObject::tr("你好..."));
格式化输入效果, 输入ip地址效果,MAC地址效果,日期输入效果,license输入效果:
ui->lineEdit->setInputMask("000.000.000.000"); ui->lineEdit->setInputMask("000.000.000.000;$");
清空全部输入, 选择内容并移动(被选择的内容移动到新的地方后,原来的地方任然还有该内容)
ui->lineEdit->setPlaceholderText(QObject::tr("搜索文本")); ui->lineEdit->setClearButtonEnabled(true); //清除全部输入 ui->lineEdit->setDragEnabled(true);//移动选择内容