操作方法
打开Qt->新建文件或项目->Application->Qt Widgets Application 其余默认,选择Choose
项目名称自拟,编译器采用默认的编译器。 注意:Linux系统和我的编译器应该是一样的,Windows系统默认应该是MinGW编译器,若非必要尽量不要使用vs的编译器。
基类选择Qwidget,类名自拟。 这里采用默认的Widget类名。 这里要取消创建界面的小勾,点击下一步完成就可以了。
因为我们创建界面要用到三个部件,QLabel,QLineEdit,QPushButton。 所以我们打开头文件,在里面添加如下头文件: #include <QLabel> #include <QLineEdit> #include <QPushButton>
在class中添加如下部件和槽函数。 private: QLabel *userNameLabel; QLabel *passWordLabel; QLineEdit *userNameLineEdit; QLineEdit *passWordLineEdit; QPushButton *login; private slots: void slotLogin();
打开widget.cpp 添加头文件: #include <QGridLayout> #include <QHBoxLayout> #include <QMessageBox> #include <QDebug> 添加界面程序:
添加槽函数: void Widget::slotLogin(){ qDebug() << "输入用户名:" << userNameLineEdit->text(); //输出调试信息 qDebug() << "输入密码:" << passWordLineEdit->text(); if (userNameLineEdit->text().operator ==("user") && passWordLineEdit->text().operator ==("1234567890")) { qDebug() << "登录成功!"; QMessageBox::information(this,tr("登录提示"),tr("登录成功")); } else { qDebug() << "用户名或密码错误!"; QMessageBox::information(this,tr("登录提示"),tr("用户名或密码错误!")); } }
完整程序:
widget.h程序如下:
main.cpp不变 widget.cpp如下:
每条代码都用详细的注释,请仔细阅读。不懂的可以评论提问。 学习程序,最好一条代码一条代码的手打,不要复制。