1.思维导图
2.继续完善登录框,当登录成功时,关闭登录界面,跳转到新的界面中
Second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
#include <QtCore/QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
private slots:
// void jump_slot(); //跳转对应槽函数
public slots:
void slot1();
//void slot2();
private:
Ui::Second *ui;
};
#endif // SECOND_H
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCore/QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
QLabel *lab1=new QLabel;
QLabel *lab2=new QLabel;
QLabel *lab3=new QLabel;
QLineEdit *edit1=new QLineEdit;
QLineEdit *edit2=new QLineEdit;
QPushButton *btn1=new QPushButton;
QPushButton *btn2=new QPushButton;
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void jump(); //跳转信号
public slots:
void on_jumpButton_clicked();
void my_slot();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
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::jump,&s,&Second::slot1);//将跳转槽函数与信号连接
return a.exec();
}
Second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
qDebug()<<"second size="<<this->size();
this->resize(350,300);
qDebug()<<this->windowTitle();
this->setWindowTitle("进门有惊喜");
this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\logo.bmp"));
// this->setScaledContents(true);
this->setWindowOpacity(0.8);
}
Second::~Second()
{
delete ui;
}
void Second::slot1()
{
this->show();
}
Widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qDebug()<<this->size();
qDebug()<<this->rect().size();
qDebug()<<this->geometry().size();
qDebug()<<this->frameGeometry().size();
qDebug()<<"width:"<<this->width()<<" height:"<<this->height();
qDebug()<<"width:"<<this->size().width()<<" height:"<<this->size().height();
//设置固定窗口尺寸
this->setFixedSize(500,400);
//窗口标题
qDebug()<<this->windowTitle(); //获取窗口标题
this->setWindowTitle("牛马聚集地");
//设置窗口图标
this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\logo.bmp"));
//设置透明度
this->setWindowOpacity(0.8);
//设置标签
QLabel *lab1=new QLabel(this);
lab1->resize(500,100);
lab1->setAlignment(Qt::AlignCenter); //垂直和水平全部居中
lab1->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\2023725191221.bmp")); //设置标签位图标
lab1->setScaledContents(true); //自适应
QLabel *lab2=new QLabel(this);//账号图标
lab2->resize(50,50);
lab2->move(100,130);
lab2->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\username.bmp")); //设置标签位图标
lab2->setScaledContents(true); //自适应
QLabel *lab3=new QLabel(this); //密码图标
lab3->resize(50,50);
lab3->move(100,200);
lab3->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\passwrd.bmp")); //设置标签位图标
lab3->setScaledContents(true); //自适应
QLineEdit *edit1=new QLineEdit;
edit1->setParent(this); //设置父组件
edit1->resize(230,50); //重新设置尺寸
edit1->move(150,130); //移动
edit1->setPlaceholderText(":用户名/账号"); //设置占位符
edit1->setStyleSheet("border:none;"
"border-bottom:2px solid black;");
QLineEdit *edit2=new QLineEdit(this);
//edit2->setParent;
edit2->resize(230,50);
edit2->move(150,200);
edit2->setEchoMode(QLineEdit::Password); //设置密文模式
edit2->setPlaceholderText(":密码"); //设置占位符
edit2->setStyleSheet("border:none;"
"border-bottom:2px solid black;");
QPushButton *btn1=new QPushButton(this);
btn1->setText("登录"); //设置按钮上的文本内容
qDebug()<<btn1->text(); //获得按钮上的文本内容
btn1->resize(80,50); //重新设置按钮的大小
qDebug()<<btn1->size();
btn1->move(150,300); //移动组件
btn1->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\login.bmp")); //设置按钮图标
QPushButton *btn2=new QPushButton(this);
btn2->setText("取消"); //设置按钮上的文本内容
qDebug()<<btn2->text(); //获得按钮上的文本内容
btn2->resize(80,50); //重新设置按钮的大小
qDebug()<<btn2->size();
btn2->move(300,300);
btn2->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\cancel.bmp")); //设置按钮图标
connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot()));//连接取消按钮
//连接登录按钮
connect(btn1,&QPushButton::clicked,[&](){
if("admin"==edit1->text()&&"123456"==edit2->text())
{
qDebug()<<"登录成功"<<endl;
on_jumpButton_clicked();
}else
{
qDebug()<<"登录失败"<<endl;
edit2->clear();
}
});
}
Widget::~Widget()
{
}
void Widget::my_slot(){
this->close();
}
void Widget::on_jumpButton_clicked(){
emit jump();
this->close();
}