Signal function and slot function gui graphical interface

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMainWindow>
#include <QString>
#include <QDebug>
#include <QIcon>
#include <QPushButton>//按钮
#include <QLineEdit>//文本框
#include <QLabel>//标签
#include <QCheckBox>
#include <QMessageBox>
namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT
signals: //该权限下定义的都是信号函数
    void mysignal(); //信号函数
public slots:
    void myslots(); //定义了一个槽函数
    void my_slots();
public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    QLabel *l1,*l2,*l3,*l4,*l5,*l7,*l6;//文本标签
    QPushButton *b1,*b2;//按钮
    QLineEdit *le1,*le2;//input框
    QCheckBox *cb1,*cb2;//复选框
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

chat.h

#ifndef CHAT_H
#define CHAT_H

#include <QWidget>
#include <QMainWindow>
#include <QString>
#include <QDebug>
#include <QIcon>
#include <QPushButton>//按钮
#include <QLineEdit>//文本框
#include <QLabel>//标签
#include <QCheckBox>
#include <QMessageBox>
namespace Ui {
    
    
class chat;
}

class chat : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit chat(QWidget *parent = nullptr);
    ~chat();
    QPushButton *b1;
public slots:
    void chat_slot();
    void login_out();
private:
    Ui::chat *ui;
};

#endif // CHAT_H

chat.cpp

#include "chat.h"
#include "ui_chat.h"

chat::chat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::chat)
{
    
    
    //ui->setupUi(this);
    this->resize(350,600);
    this->setWindowFlag(Qt::FramelessWindowHint);
    b1 = new QPushButton(this);
    b1 -> setIcon(QIcon(":/icon_/icon/denglu_1.png"));
    b1->setStyleSheet("border-style: none;");
    b1->resize(30,30);
    b1->move(320,0);
    connect(this->b1,&QPushButton::clicked,this,&chat::login_out);
}

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

void chat::chat_slot()
{
    
    
    this->show();

}

void chat::login_out()
{
    
    
    QMessageBox::StandardButtons ret = QMessageBox::question(this,
                                                             "提示","是否退出登录",
                                                             QMessageBox::Yes|QMessageBox::Cancel,
                                                      QMessageBox::Yes);
    if(ret == QMessageBox::Yes){
    
    
        this->close();
     }
}

main.cpp

#include "widget.h"
#include <QApplication>
#include "chat.h"
int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Widget w;
    w.show();
    chat c;
    QObject::connect(&w,&Widget::mysignal,&c,&chat::chat_slot);
    return a.exec();
}

widget.h

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    
    
    //ui->setupUi(this);
    //设置窗口大小
    this->setFixedSize(430,340);
    //设置icon
    this->setWindowIcon(QIcon(":/icon_/icon/aichegujiabeifen6.png"));
    //设置名称
    this->setWindowTitle("QQ");
    this -> setStyleSheet("background-color:#ffffff;");
    //设置标签1
    l1=new QLabel(this);
    //设置大小
    l1->resize(430,130);
    //设置颜色
    l1->setStyleSheet("background-color:#00a1db;");
    //设置头像
    l2=new QLabel(this);
    l2->resize(60,60);
    l2->move(185,100);
    l2->setStyleSheet("QLabel {background-image: url(:/icon_/icon/tx.png);border-radius:30px;}");
    //登录账号标签
    l3=new QLabel(this);
    l3->resize(25,25);
    l3->setScaledContents(true);
    l3->setPixmap(QPixmap(":/icon_/icon/denglu.png"));
    l3->move(100,175);
    //登录账号下划线
    l5=new QLabel(this);
    l5->resize(230,2);
    l5 -> setStyleSheet("border-radius:1px;background-color:#00a1db;");
    l5->move(100,210);
    //登录密码标签
    l4=new QLabel(this);
    l4->resize(25,25);
    l4->setScaledContents(true);
    l4->setPixmap(QPixmap(":/icon_/icon/denglumima.png"));
    l4->move(100,218);
    //登录密码下划线
    l6=new QLabel(this);
    l6->resize(230,2);
    l6 -> setStyleSheet("border-radius:1px;background-color:#00a1db;");
    l6->move(100,250);
    //账号框
    le1 = new QLineEdit(this);
    le1 -> resize(200,25);
    le1 -> setStyleSheet("border-radius:3px;border: none;");
    le1 -> setPlaceholderText("QQ号码/手机号/邮箱");
    le1 -> move(130,175);
    //密码框
    le2 = new QLineEdit(this);
    le2 -> resize(200,25);
    le2 ->setEchoMode(QLineEdit::Password);
    le2 -> setStyleSheet("border-radius:3px;border: none;");
    le2 -> setPlaceholderText("密码");
    le2 -> move(130,218);
    //复选框
    cb1 = new QCheckBox("记住密码",this);
    cb1 -> move(100,255);
    cb2 = new QCheckBox("自动登录",this);
    cb2 -> move(190,255);
    //找回密码
    l7=new QLabel("找回密码",this);
    l7->resize(56,18);
    l7->move(280,255);
    //登录按钮
    b1=new QPushButton("登录",this);
    b1->resize(100,35);
    b1->move(100,280);
    b1 -> setStyleSheet("background-color:#00a1db;border-radius:5px;border: none;");
    connect(this->b1,&QPushButton::clicked,this,&Widget::my_slots);
    //取消按钮
    b2=new QPushButton("取消",this);
    b2->resize(100,35);
    b2->move(230,280);
    b2 -> setStyleSheet("background-color:#00a1db;border-radius:5px;border: none;");
    connect(this->b2,SIGNAL(clicked()),this,SLOT(myslots()));
}

Widget::~Widget()
{
    
    
    delete ui;
}
void Widget::my_slots()
{
    
    
    QString name = this->le1->text();
    QString pass = this->le2->text();
    if("admin"!=name || "123456"!=pass){
    
    
        QString mes;
        if("admin"!=name){
    
    
            mes = "账户名错误请重新输入";
        }else{
    
    
            mes = "密码输入错误请重新输入";
        }
        QMessageBox box(QMessageBox::Critical,
                                "提示",
                              mes,
                              QMessageBox::Ok|QMessageBox::Cancel,
                              this);
        int ret=box.exec();
        switch(ret){
    
    
        case QMessageBox::Ok:
            this->le1->setText("");
            this->le2->setText("");
            break;
        case QMessageBox::Cancel:
            this->close();
            break;
        }
    }
    if("admin"==name && "123456"==pass){
    
    
        QMessageBox::StandardButton ret = QMessageBox::information(this,
                                                                   "提示框","登录成功",
                                                                   QMessageBox::Ok,QMessageBox::Ok);
        if(ret == QMessageBox::Ok){
    
    
            emit mysignal();
            this->close();
        }
    }

}
void Widget::myslots(){
    
    
    QMessageBox sure(QMessageBox::Question,"提示","是否继续登录",QMessageBox::Yes|QMessageBox::No,this);
    int ret = sure.exec();
    if(ret == QMessageBox::Yes){
    
    
            this->close();
    }
}

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/tupkoo/article/details/131195970