#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLineEdit> #include <QLabel> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); QLineEdit* edit[10]; QLabel* label ; public slots: void textChanged(const QString& text); }; #endif // MAINWINDOW_H
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->resize(400,400); this->centralWidget(); int ypos = 30 ; for(int i=0;i<5;i++) { edit[i] = new QLineEdit("世界,不好",this); edit[i]->setGeometry(10,ypos,200,40); ypos += 50 ; } // 设置显示模式 edit[1]->setEchoMode(QLineEdit::Normal); // 与默认类型一致 edit[2]->setEchoMode(QLineEdit::NoEcho); // 隐藏输入文本,不改变光标位置 edit[3]->setEchoMode(QLineEdit::Password); // 用*表示文本 edit[4]->setEchoMode(QLineEdit::PasswordEchoOnEdit); // 若编辑文本与默认相同,失去焦点显示* label = new QLabel("文本框内容为:",this); label->setGeometry(10,300,350,30); connect(edit[0],SIGNAL(textChanged(QString)),this,SLOT(textChanged(QString))); } MainWindow::~MainWindow() { } void MainWindow::textChanged(const QString& text) { label->setText("文本框内容为:"+text); }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }