1.
2.
main.cpp
#include "widget.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Form f; //实例化一个form类的对象
QObject::connect(&w,&Widget::jump,&f,&Form::jump_slot); //连接两个界面,widget中的信号函数和form中的槽函数
return a.exec();
}
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
//槽函数 实现界面的出现
void Form::jump_slot()
{
this->show();
}
widget.h
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
//以下为图形界面的操作 9-19
ui->setupUi(this);
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon(":/pictrue/qq.png"));
this->setFixedSize(520,460);
ui->lab1->setPixmap(QPixmap(":/pictrue/logo.png"));
ui->lab1->setScaledContents(true);
//let1为账号的LINEEDIT框,let2为密码的LINEEDIT框
ui->let1->setPlaceholderText("QQ账号/手机号/QQ邮箱");
ui->let2->setEchoMode(QLineEdit::Password);
//基于qt4的连接,自定义槽函数,实现登录键的操作
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(my_slot()));
//基于qt5的连接,自定义槽函数,实现退出键的操作
connect(ui->btn1,&QPushButton::clicked,this,&Widget::My_slot);
}
Widget::~Widget()
{
delete ui;
}
//退出键的槽函数
void Widget::my_slot()
{
// 问题消息对话框的创建 使用静态成员函数
int rst = QMessageBox::question(this,"问题","是否要退出登录",QMessageBox::Yes|QMessageBox::No);
//如果是Yes 退出登录界面
if(rst ==QMessageBox::Yes )
{
this->close();
}
}
//登录键的槽函数
void Widget::My_slot()
{
//判断密码是否正确
if(ui->let1->text()==QString("admin") && ui->let2->text()==QString("123456"))
{
//正确后,弹出信息对话框 基于基本版本的创建 需要使用exec函数执行
QMessageBox ifm (QMessageBox::Information,"提示","登录成功",QMessageBox::Ok);
int rst = ifm.exec();
if (rst ==QMessageBox::Ok )
{
this->close(); //退出登录界面
emit jump(); //触发jump信号,准备跳转窗口
}
}
else
{
//错误后,弹出错误对话框 基于基本版本的创建 需要使用exec函数执行
QMessageBox crl (QMessageBox::Critical,"错误","账号密码不正确,是否重新登录",QMessageBox::Yes|QMessageBox::No);
int rst=crl.exec();
if (rst==QMessageBox::Yes)
{
ui->let2->clear(); //清空密码
}
else
{
this->close(); //退出登录界面
}
}
}