widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QLineEdit>
#include <QtDebug>
class Widget : public QWidget
{
Q_OBJECT
signals:
void login_success();
public slots: //该权限下要定义公共的槽函数
void my_slot(); //qt4版本,自定义的槽函数
void loginBtn_clicked(); //qt5版本
public:
Widget(QWidget *parent = nullptr) ;
~Widget();
QLabel *lb1;
QLabel *lb2;
QTimer timer;
QLabel *lb3;
QLabel *lb4;
QLineEdit *account_edit;
QLineEdit *pwd_edit;
QPushButton *login_btn;
QPushButton *cancel_btn;
};
#endif // WIDGET_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QLabel>
#include <QWidget>
#include <QIcon>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
QLabel *lb1;
public slots:
void jump_slot();
private:
Ui::Second *ui;
};
#endif // SECOND_H
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//窗口的设置
this->setFixedSize(500, 400); //设置固定大小
this->setWindowOpacity(0.95); //设置透明度
this->setWindowTitle("鹏哥快聊");
// this->setWindowFlag(Qt::FramelessWindowHint); //设置纯净窗口
this->setWindowIcon(QIcon(":/icon/QQ.png"));
//设置logo图
lb1 = new QLabel(this);
lb1->setPixmap(QPixmap(":/icon/icon.png"));
lb1->resize(50, 50);
lb1->setScaledContents(true);
lb2 = new QLabel("五天带你上手C++", this);
lb2->resize(300, 50);
lb2->move(50, 0);
lb2->setScaledContents(true);
lb2->setStyleSheet("background-color:red; color:white;");
QFont font; //设置字体
font.setPointSize(20);
lb2->setFont(font);
int interval = 200;
bool visible = true;
QObject::connect(&timer, &QTimer::timeout, [&]()
{
if (visible)
{
lb2->hide();
}
else
{
lb2->show();
}
visible = !visible;
});
timer.start(interval);
//登录框与密码框
//图像准备
lb3 = new QLabel(this);
lb3->setPixmap(QPixmap(":/icon/user.png"));
lb3->resize(40, 40);
lb3->setScaledContents(true);
lb3->move(100,100);
lb4 = new QLabel(this);
lb4->setPixmap(QPixmap(":/icon/pwd.png"));
lb4->resize(50, 50);
lb4->setScaledContents(true);
lb4->move(95,200);
//输入框
account_edit = new QLineEdit(this);
account_edit->setPlaceholderText("QQ号码/手机/邮箱");
account_edit->resize(200, 35);
account_edit->move(170, 105);
pwd_edit = new QLineEdit(this);
pwd_edit->resize(200, 35);
pwd_edit->move(170, 210);
pwd_edit->setPlaceholderText("密码");
pwd_edit->setEchoMode(QLineEdit::Password);
//登陆取消按钮
login_btn = new QPushButton(this);
login_btn->setIcon(QIcon(":/icon/login.png"));
login_btn->setText("登录");
login_btn->resize(80, 45);
login_btn->move(200,300);
font.setPointSize(15);
login_btn->setFont(font);
connect(login_btn, &QPushButton::clicked, this, &Widget::loginBtn_clicked); //qt5
cancel_btn = new QPushButton(this);
cancel_btn->setText("取消");
cancel_btn->setIcon(QIcon(":/icon/cancel.png"));
cancel_btn->resize(80, 45);
cancel_btn->move(350,300);
cancel_btn->setFont(font);
connect(cancel_btn, SIGNAL(clicked()), this, SLOT(my_slot())); //qt4
}
Widget::~Widget()
{
}
//自定义槽函数的实现部分
void Widget::my_slot()
{
this->close();
}
//qt5版本自定义槽函数
void Widget::loginBtn_clicked()
{
QString name = account_edit->text();
QString pwd = pwd_edit->text();
if (name == "admin" && pwd == "123456")
{
qDebug() << "登录成功";
emit login_success();
this->close();
}
else
{
qDebug() << "登陆失败";
pwd_edit->clear();
}
}
second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
//窗口的设置
this->setFixedSize(500, 400); //设置固定大小
this->setWindowOpacity(0.95); //设置透明度
this->setWindowTitle("鹏哥快聊");
// this->setWindowFlag(Qt::FramelessWindowHint); //设置纯净窗口
this->setWindowIcon(QIcon(":/icon/QQ.png"));
}
Second::~Second()
{
delete ui;
}
void Second::jump_slot()
{
this->show();
}
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Second s;
//连接两个界面的信号与槽
QObject::connect(&w, &Widget::login_success, &s, &Second::jump_slot);
return a.exec();
}