模拟qq登录界面功能代码

1、继续完善登录框,当登录成功时,关闭登录界面,跳转到新的界面

1、头文件

1.1user.h

#ifndef USER_H
#define USER_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
namespace Ui {
class user;
}

class user : public QWidget
{
    Q_OBJECT

    QLabel *ulab1 ;    //登录成功后标签
    QPushButton *ubtn1;

public:
    explicit user(QWidget *parent = nullptr);
    ~user();
public slots:
    void log_slot();
private slots:
public slots:
    void uslot();

private:
    Ui::user *ui;
};

#endif // USER_H

2.2Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT
    QLabel *lab1;        //中间图片
    QLabel *lab2;        //账号图标
    QLabel *lab3;       //密码图标
    QLabel *lab4;       //登录图标
    QLabel *lab5;       //取消图标

    QLineEdit *nameedit1     = new QLineEdit;     //账号行编辑
    QLineEdit *passwdedit2   = new QLineEdit;     //密码行编辑
    QPushButton *btn1 = new QPushButton;           //登录按钮
    QPushButton *btn2 = new QPushButton;            //取消按钮


public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
signals:
    void my_signal();
public slots:
    void my_slot();
    void jump_clicked();



};

#endif // WIDGET_H

 2、main.cpp

#include "widget.h"
#include "user.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    user u;

    QObject::connect(&w, &Widget::my_signal, &u, &user::log_slot);
    return a.exec();
}

2.2 user.cpp

#include "user.h"
#include "ui_user.h"

user::user(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::user)
{
    ui->setupUi(this);
    qDebug()<<"user size ="<size();
    this->resize(350,300);
    qDebug()<windowTitle();
    this->setWindowTitle("qq");
    this->setWindowIcon(QIcon(":/icon/logo.png"));

    this->setWindowOpacity(0.8);

    ulab1 = new QLabel(this);
    ulab1->resize(this->size());
    ulab1->setPixmap(QPixmap(":/icon/backgroundhehua.jpg"));
    ulab1->setScaledContents(true);

    //登录成功点击关闭
    ubtn1 = new QPushButton("关闭",this);
    ubtn1->move(175,200);
    connect(this->ubtn1,&QPushButton::clicked,this,&user::uslot);

}

user::~user()
{
    delete ui;
}

void user::log_slot()
{
    this->show();

}
void user::uslot()
{
    this->close();
}

 2.3Widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    qDebug()<size();     //求出当前界面大小
    this->setFixedSize(430,300);  //固定尺寸为430,300
    this->setWindowOpacity(0.95);
    //2、设置窗口标题
    this->setWindowTitle("华清远见");
    qDebug()<windowTitle();    //获取窗口标题

    //3、设置窗口图标
    this->setWindowIcon(QIcon(":/icon/logo.png"));

    //4、设置一个Qtabel标签,内容为图片
    lab1 = new QLabel(
                this);
    lab1->resize(430,100);
    lab1->move(0,0);
    lab1->setPixmap(QPixmap(":/icon/background.jpg"));
    lab1->setScaledContents(true);

    //5、行编辑器类
    //name
    nameedit1 = new QLineEdit(this);
    nameedit1->resize(220,30);
    nameedit1->move(125,130);
    nameedit1->setPlaceholderText("qq号码/手机号码/邮箱");
    nameedit1->setStyleSheet("border:none;"
                             "border-bottom : 2px solid blue;");
    //password
    passwdedit2= new QLineEdit(this);
    passwdedit2->resize(220,30);
    passwdedit2->move(125,180);
    passwdedit2->setPlaceholderText("密码");
    passwdedit2->setEchoMode(QLineEdit::Password);
    passwdedit2->setStyleSheet("border:none;"
                               "border-bottom : 2px solid blue;");

    //6、设置两个标签,用来展示账户和密码

    lab2 = new QLabel(this);
    lab2->resize(25,25);
    lab2->move(100,130);
    lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
    lab2->setAlignment(Qt::AlignCenter);
    lab2->setScaledContents(true);
    //第二个标签password
    lab3 = new QLabel(this);
    lab3->resize(25,25);
    lab3->move(100,180);
    lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
    lab3->setAlignment(Qt::AlignCenter);
    lab3->setScaledContents(true);

    //7、设置两个按钮button来存放登录与取消界面
    btn1 = new QPushButton(this);
    btn1->setText("  登录");
    btn1->resize(70,35);
    btn1->move(260,250);

    lab4 = new QLabel(this);
    lab4->resize(25,35);
    lab4->move(260,250);
    lab4->setPixmap(QPixmap(":/icon/denglu.png"));
    lab4->setScaledContents(true);

    //cancel
    btn2 = new QPushButton(this);
    btn2->setText("  取消");
    btn2->resize(70,35);
    btn2->move(350,250);

    lab5 = new QLabel(this);
    lab5->resize(25,35);
    lab5->move(350,250);
    lab5->setPixmap(QPixmap(":/icon/cancel.png"));
    lab5->setScaledContents(true);

    //将取消按钮与信号和槽绑定
    connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot()));   //4.0版本

    //将登录按钮使用5.0版本链接
    connect(btn1,&QPushButton::clicked,[&](){

        if("admin" ==nameedit1->text() && "123456" == passwdedit2->text())
        {
            qDebug()<<"登录成功"<clear();
        }
    });
    //将登录后跳转界面与信号绑定


//    connect(this,&Widget::my_signal,[=](){
//        btn1->resize(btn1->width()+1,btn1->height()+1);
//    });

}

Widget::~Widget()
{
}
void Widget::my_slot()
{
   // btn1->setEnabled(false);
    this->close();     //取消登录则关闭界面
}
void Widget::jump_clicked()
{
    emit my_signal();
    this->close();
}


//void Widget::my_signal(QString)
//{
//    emit my_signal("my hello");
//}

2、新建一个工程文件,将默认的代码加上注释信息

3、完成思维导图模拟qq登录界面功能代码_第1张图片

 

你可能感兴趣的:(c++,c++,qt,c语言)