widget.h:登录界面头文件(组件、信号、槽)
public:
Widget(QWidget *parent = nullptr);
~Widget();
QLabel *lb1;//背景图
QLineEdit *le1;//账号
QLineEdit *le2;//密码
QPushButton *bt1;//登录按钮
QPushButton *bt2;//退出按钮
signals:
void jump();//登录跳转信号
public slots:
void login();//登录按钮槽
void cancel();//退出按钮槽
form.h:文本框界面(组件、信号、槽)
public:
QTextEdit *td;
QPushButton *bt1;
QPushButton *bt2;
QPushButton *bt3;
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void jump_here();//登录成功跳转
void click_bt1();//字体
void click_bt2();//颜色
void click_bt3();//打开文件
widget.cpp(登录界面)
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
//组件属性
ui->setupUi(this);
this->setFixedSize(600,400);
lb1 = new QLabel(this);
lb1->resize(600,200);
lb1->setPixmap(QPixmap(":/pictrue/qq2.gif"));
lb1->setScaledContents(true);
le1 = new QLineEdit(this);
le1->move(150,220);
le1->resize(300,40);
le1->setPlaceholderText("账号");
le2 = new QLineEdit(this);
le2->move(150,270);
le2->resize(300,40);
le2->setPlaceholderText("密码");
bt1 = new QPushButton("登录",this);
bt1->move(100,320);
bt1->resize(140,60);
bt1->setStyleSheet("background-color:skyblue;color:white;border-radius:5px");
bt2 = new QPushButton("退出",this);
bt2->move(350,320);
bt2->resize(140,60);
bt2->setStyleSheet("background-color:pink;color:white;border-radius:5px");
//按钮信号和槽连接
connect(bt1,&QPushButton::clicked,this,&Widget::login);
connect(bt2,&QPushButton::clicked,this,&Widget::cancel);
}
//登录槽函数
void Widget::login()
{
if(le1->text()=="Admin"&&le2->text()=="12345")
{
int res = QMessageBox::information(this,
"提示",
"登录成功",
QMessageBox::Ok);
if(res == QMessageBox::Ok)
{
emit jump();//发射信号
this->close();
}
}
else
{
int res = QMessageBox::information(this,
"提示",
"账号或密码错误",
QMessageBox::Ok);
if(res == QMessageBox::Ok)
le2->clear();
}
}
//退出按钮槽函数
void Widget::cancel()
{
int res = QMessageBox::information(this,
"提示",
"确认退出吗",
QMessageBox::Yes|QMessageBox::No);
if(res == QMessageBox::Yes)
this->close();
}
Widget::~Widget()
{
delete ui;
}
form.cpp(文本框界面)
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
//组件属性
ui->setupUi(this);
this->resize(800,600);
td = new QTextEdit(this);
td->resize(800,400);
bt1 = new QPushButton("字体",this);
bt1->resize(100,70);
bt1->move(20,450);
bt2 = new QPushButton("颜色",this);
bt2->resize(100,70);
bt2->move(200,450);
bt3 = new QPushButton("打开文件",this);
bt3->resize(100,70);
bt3->move(380,450);
//三个按钮信号与槽连接
connect(bt1,&QPushButton::clicked,this,&Form::click_bt1);
connect(bt2,&QPushButton::clicked,this,&Form::click_bt2);
connect(bt3,&QPushButton::clicked,this,&Form::click_bt3);
}
//槽函数,与登录界面信号连接,当做登录成功跳转展示的界面
void Form::jump_here()
{
this->show();
}
//字体槽函数
void Form::click_bt1()
{
bool ok;
QFont f = QFontDialog::getFont(
&ok,
QFont("宋体",10),
this,
"字体");
if(ok)
{
td->setFont(f);
}
}
//颜色槽函数
void Form::click_bt2()
{
QColor c = QColorDialog::getColor(
QColor("black"),
this,
"颜色");
if(c.isValid()==true)
{
td->setTextColor(c);
}
}
//选择文件槽函数
void Form::click_bt3()
{
QString fi = QFileDialog::getOpenFileName(
this,
"选择文件",
"./",
"all(*.*);;images(*.png *.jpg)");
QFile file(fi);
if(file.open(QFile::ReadOnly)==false)
{
qDebug("打开失败");
}
QString text = file.readAll();
td->setText(text);
file.close();
}
Form::~Form()
{
delete ui;
}
main.cpp(main函数,连接两个界面)
#include "widget.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
Form f;
w.show();
//登录界面的信号与文本界面的槽连接,用于登录成功后的界面跳转
QObject::connect(&w,&Widget::jump,&f,&Form::jump_here);
return a.exec();
}