Qt编程指南
- ■ QTextEdit
- ■ 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->setFocus();if(!textEdit->toPlainText().isEmpty()){textEdit->selectAll();}}void MainWindow::pushButtonClearAllClicked(){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));QRegExpValidatorQRegExp rx("^(-?[0]|-?[1-9][0-9]{0,5})(?:\\.\\d{1,4})?$|(^\\t?$)");QRegExpValidator *pReg = new QRegExpValidator(rx, this);lineEdit->setValidator(pReg);QRegExp rx("(^-?180$)|(^-?1[0-7]\\d$)|(^-?[1-9]\\d$)|(^-?[1-9]$)|^0$");QRegExpValidator *pReg = new QRegExpValidator(rx, this);lineEdit->setValidator(pReg); QRegularExpressionValidator示例一:将输入限制为1到100之间的整数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("只读模式");QFile file("moc_mainwindow.cpp");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(){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){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);
■
■