Qt实践基础-简单的登录界面的实现

主要代码的实现:

1、为了更好的实现界面的组织,采用C++直接构建UI

2、登录多次密码错误则断开“确认”按钮的链接

3、注意理解static变量的应用

4、QString类的使用更像继承了string类型,要熟悉它的使用

 1 #include "qwlogin.h"
 2 
 3 
 4 QWLogin::QWLogin(QWidget *parent)
 5     : QWidget(parent)
 6 {
 7     setUI();
 8     initialSignal();
 9     setWindowTitle("登录界面");
10 }
11 
12 QWLogin::~QWLogin()
13 {
14 }
15 
16 void QWLogin::setUI()
17 {
18     label3 = new QLabel(tr("登录"));
19     QFont font = label3->font();
20     font.setPointSize(20);
21     label3->setFont(font);
22     //登录标题
23     label1 = new QLabel(tr("用户名:"));
24     name = new QLineEdit;
25     QHBoxLayout *input_name = new QHBoxLayout;
26     input_name->addWidget(label1);
27     input_name->addWidget(name);
28     //输入用户名
29     label2 = new QLabel(tr("密码:"));
30     password = new QLineEdit;
31     QHBoxLayout *input_password = new QHBoxLayout;
32     input_password->addWidget(label2);
33     input_password->addWidget(password);
34     //输入密码
35     btnOK = new QPushButton(tr("确定"));
36     btnCLOSE = new QPushButton(tr("取消"));
37     QHBoxLayout *button = new QHBoxLayout;
38     button->addWidget(btnOK);
39     button->addStretch();
40     button->addWidget(btnCLOSE);
41     //操作按钮
42     QVBoxLayout *page = new QVBoxLayout;
43     page->addWidget(label3);
44     page->addLayout(input_name);
45     page->addLayout(input_password);
46     page->addLayout(button);
47     //构建主页面框架
48     setLayout(page);//完成布局
49 }
50 
51 void QWLogin::initialSignal()
52 {
53     connect(btnCLOSE,SIGNAL(clicked()),this,SLOT(close()));
54     connect(btnOK,SIGNAL(clicked()),this,SLOT(on_Login()));
55 }
56 
57 void QWLogin::on_Login()
58 {
59     static int cout = 0;
60     QString user = name->text();
61     QString pass = password->text();
62     QString p="";
63     int lenth = pass.count();
64     for(int i=0;i<lenth;i++)p.append("*");
65     password->setText(p);
66     if(user=="savenneer"&&pass=="cymasd")
67         p="  登录成功";
68     else {p="  登录失败";cout++;}
69     if(cout>=4)
70     {
71         p = "失败操作过多\n禁止登录";
72         disconnect(btnOK,SIGNAL(clicked),this,SLOT(on_Login()));
73     }
74     int len = user.count();
75     if(len==0||lenth==0){p="未输入内容";cout--;}
76     label3->setText(p);
77 }

头文件的构建:

 1 #ifndef QWLOGIN_H
 2 #define QWLOGIN_H
 3 
 4 #include <QWidget>
 5 #include <QPushButton>
 6 #include <QLineEdit>
 7 #include <QLabel>
 8 #include <QLayout>
 9 
10 
11 class QWLogin : public QWidget
12 {
13     Q_OBJECT
14 private:
15     QPushButton* btnOK;
16     QPushButton* btnCLOSE;
17     QLabel *label1;
18     QLabel *label2;
19     QLabel *label3;
20     QLineEdit *name;
21     QLineEdit *password;
22 
23 private slots:
24     void setUI();
25     void initialSignal();
26     void on_Login();
27 
28 public:
29     QWLogin(QWidget *parent = nullptr);
30     ~QWLogin();
31 };
32 #endif // QWLOGIN_H

OK

posted @ 2020-02-17 18:13  SavenNeer  阅读(1959)  评论(0编辑  收藏  举报