Qt编程指南

  • ■ QTextEdit
  • ■ QLineEdit
    • ■ QLineEdit 设置正则表达式
  • ■ QPlainTextEdit
  • ■ QKeySequenceEdit
  • ■ QList edits

■ QTextEdit

/* 实例和对象,设置位置和显示大小 */textEdit = new QTextEdit(this); textEdit->setGeometry(0, 0, 800, 400);/* 实例和对象,设置位置和显示大小,设置文本 */pushButtonSelectAll = new QPushButton(this);pushButtonSelectAll->setGeometry(200, 420, 50, 20);pushButtonSelectAll->setText("全选");/* 实例和对象,设置位置和显示大小,设置文本 */pushButtonClearAll = new QPushButton(this);pushButtonClearAll->setGeometry(500, 420, 50, 20);pushButtonClearAll->setText("清除");/* 信号槽连接 */connect(pushButtonSelectAll, SIGNAL(clicked()), this,SLOT(pushButtonSelectAllClicked()));connect(pushButtonClearAll, SIGNAL(clicked()), this,SLOT(pushButtonClearAllClicked()));MainWindow::~MainWindow(){}void MainWindow::pushButtonSelectAllClicked(){/* 设置焦点为textEdit */textEdit->setFocus();/* 判断文本编辑框内容是否为空,不为空则全选 */if(!textEdit->toPlainText().isEmpty()){/* 全选 */textEdit->selectAll();}}void MainWindow::pushButtonClearAllClicked(){/* 清空textEdit里的文本内容 */textEdit->clear();}/* 设置当前程序的工作目录为可执行程序的工作目录 */QDir::setCurrent(QCoreApplication::applicationDirPath());this->setGeometry(0, 0, 800, 480);

■ QLineEdit

switch (index) {case 0://默认,输入什么即显示什么echoLineEdit->setEchoMode(QLineEdit::Normal);break;case 1://密码,一般是用小黑点覆盖你所输入的字符echoLineEdit->setEchoMode(QLineEdit::Password);break;case 2://编辑时输入字符显示输入内容,否则用小黑点代替echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);break;case 3://任何输入都看不见(只是看不见,不是不能输入)echoLineEdit->setEchoMode(QLineEdit::NoEcho);示例一: QLineEdit限制输入只能输入Ip地址,且未输入时要显示4个点展位。// 正在表达式限制输入ui->lineEdit_ip->setValidator(new QRegExpValidator(QRegExp("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")));// 用于占位ui->lineEdit_ip->setInputMask("000.000.000.000; ");

■ QLineEdit 设置正则表达式

一共4种限制器:QDoubleValidator, QIntValidator, QRegExpValidator, 和QRegularExpressionValidatorQDoubleValidatorlineEdit->setValidator(new QIntValidator(0, 1000, this)); QIntValidatorlineEdit->setValidator(new QDoubleValidator(-180.0,180.0,6,this));QRegExpValidator//限制浮点数输入范围为[-999999.9999,999999.9999] QRegExp rx("^(-?[0]|-?[1-9][0-9]{0,5})(?:\\.\\d{1,4})?$|(^\\t?$)");QRegExpValidator *pReg = new QRegExpValidator(rx, this);lineEdit->setValidator(pReg);//限制浮点数输入范围为[-180,180]QRegExp rx("(^-?180$)|(^-?1[0-7]\\d$)|(^-?[1-9]\\d$)|(^-?[1-9]$)|^0$");QRegExpValidator *pReg = new QRegExpValidator(rx, this);lineEdit->setValidator(pReg); QRegularExpressionValidator示例一:将输入限制为1100之间的整数QRegularExpressionValidator* validator = new QRegularExpressionValidator(QRegularExpression("([1-9][0-9]?|100)"), this);ui->lineEdit_BMI->setValidator(validator);ui->lineEdit_height->setValidator(validator);
#include QRegularExpressionValidator* m_validator1_10;m_validator1_10 = new QRegularExpressionValidator(QRegularExpression("^(?:[1-9]|[1-4][0-9]|50)$"), this);ui->lineEdit_7_MagAbsorptionRate->setValidator(m_validator1_10);

■ QPlainTextEdit

plainTextEdit = new QPlainTextEdit(this);plainTextEdit->setGeometry(0, 50, 800, 430);radioButton = new QRadioButton(this);radioButton->setGeometry(650, 20, 100, 20);radioButton->setText("只读模式");/* 打开可执行程序目录里的moc_mainwindow.cpp,注意如果是Windows下moc_mainwindow.cpp并不在当前目录,而在上一级目录"../moc_mainwindow.cpp"*/QFile file("moc_mainwindow.cpp");/* 以只读模式打开,但是可以在plainTextEdit里编辑 */file.open((QFile::ReadOnly | QFile::Text));/* 加载到文件流 */QTextStream in(&file);/* 从文本流中读取全部 */plainTextEdit->insertPlainText(in.readAll());/* 信号槽连接 */connect(radioButton, SIGNAL(clicked()), this,SLOT(radioButtonClicked()));MainWindow::~MainWindow(){}void MainWindow::radioButtonClicked(){/* 检查radioButton是否选中 */if(radioButton->isChecked()) {/* 设置为只读模式 */plainTextEdit->setReadOnly(true);} else {/* 设置为非只读模式 */plainTextEdit->setReadOnly(false);}}

■ QKeySequenceEdit

/* 实例化 */keySequenceEdit = new QKeySequenceEdit(this);/* 设置位置与大小 */keySequenceEdit->setGeometry(350, 200, 150, 30);/* 信号槽连接 */connect(keySequenceEdit,SIGNAL(keySequenceChanged(const QKeySequence &)),this,SLOT(KSEKeySequenceChanged(const QKeySequence &)));}MainWindow::~MainWindow(){}void MainWindow::KSEKeySequenceChanged(const QKeySequence &keySequence){/* 判断输入的组合键是否为Ctrl + Q,如果是则退出程序 */if(keySequence == QKeySequence(tr("Ctrl+Q"))) {/* 结束程序 */this->close();}else {/* 打印出按下的组合键 */qDebug()<<keySequence.toString()<<endl;}}使用资源里的文件时格式是:+前缀+文件路径*/QPixmap pixmap(":images/openedv.png");

■ QList edits

//将文本框拿出来QList<QLineEdit *> edits = this->findChildren<QLineEdit *>();edit = edits.first();edit->setFrame(true);edit->setAlignment(Qt::AlignCenter);edit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);