s
完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMovie>
#include <QPushButton>
#include <QDebug>
#include <iostream>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void my_singal();
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
public slots:
void jump_slot();
private:
Ui::Second *ui;
};
#endif // SECOND_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::my_singal, &s, &Second::jump_slot);
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口图标
this->setWindowIcon(QIcon(":/pictrue/qqkun.jpg"));
this->setWindowFlag(Qt::FramelessWindowHint);
//窗口图标设置
QMovie *mv = new QMovie(":/pictrue/kun.gif");
ui->backgroundlabel->setMovie(mv);
mv->start();
ui->backgroundlabel->setScaledContents(true);
ui->accountlabel->setPixmap(QPixmap(":/pictrue/userName.jpg"));
ui->accountlabel->setScaledContents(true);
ui->passwordlabel->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->passwordlabel->setScaledContents(true);
ui->passwdline->setEchoMode(QLineEdit::Password);
//手动连接信号和自定义的槽函数 基于qt4版的连接 该连接是不友好的连接
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(on_btn2_clicked()));
//手动连接信号和自定义的槽函数 基于qt5版的连接 该连接是友好的连接
connect(ui->btn1, &QPushButton::clicked, this, &Widget::on_btn1_clicked);
}
void Widget::on_btn1_clicked()
{
//获取账号和密码输入框中的文本
QString inputAccout = ui->accoutline->text();
QString inputPasswd = ui->passwdline->text();
//如果账号密码匹配成功,则输出"登录成功"
if(inputAccout == "admin" && inputPasswd == "123456")
{
//弹出信息对话框,提示"登录成功"
QMessageBox msg(
QMessageBox::Information, //信息图标
"提示", //标题
"登录成功", //文本
QMessageBox::Yes, //提供的按钮
this); //指定父组件
msg.exec();
emit my_singal();
this->close();
}else
{
//弹出错误对话框,提示"错误信息"
QMessageBox msg(
QMessageBox::Information, //信息图标
"提示", //标题
"账号和密码不匹配,是否重新登录", //文本
QMessageBox::Yes | QMessageBox::No, //提供的按钮
this); //指定父组件
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
ui->passwdline->clear();
}
else
{
this->close();
}
}
}
void Widget::on_btn2_clicked()
{
int ret = QMessageBox::question(this, //父组件
"问题", //标题
"您确定要退出登录吗?", //文本
QMessageBox::Yes | QMessageBox::No); //提供的按钮
if(ret == QMessageBox::Yes)
{
this->close();
}
else
{
ui->passwdline->clear();
this->show();
}
}
Widget::~Widget()
{
delete ui;
}
second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
}
Second::~Second()
{
delete ui;
}
void Second::jump_slot()
{
this->show();
}
效果图自己脑补!!!