作业:
使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数
将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QThread>
#include <QTextToSpeech>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void login_slot();
signals:
void my_signal();
private:
Ui::Widget *ui;
QTextToSpeech *speech;
QLabel *lab;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
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);
speech = new QTextToSpeech(this);
//窗口界面
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon(":/pictrue/qq.png"));
//第一个label
ui->logoLab->setPixmap(QPixmap(":/pictrue/logo.png"));
ui->logoLab->setScaledContents(true);
//账户标签
ui->userNameLab->setPixmap(QPixmap(":/pictrue/userName.jpg"));
ui->userNameLab->setScaledContents(true);
//密码标签
ui->passwdLab->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->passwdLab->setScaledContents(true);
//密码输入
ui->passwdEdit->setEchoMode(QLineEdit::Password);
//登录
connect(ui->loginbtn,&QPushButton::clicked,this, &Widget::login_slot);
//取消
connect(ui->cancelbtn,SIGNAL(clicked()),this,SLOT(close()));
}
void Widget::login_slot()
{
if(ui->userNameEdit->text() == "zxr" && ui->passwdEdit->text() == "123456")
{
speech->say("登陆成功");
QThread::sleep(3);
this->close();
}
else
{
ui->loginbtn->setText("登录失败");
}
}
Widget::~Widget()
{
delete ui;
}
运行结果: