使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数
将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空
main.c
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QMovie>
#include<QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui
{class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget* parent = nullptr);~Widget();
public slots:void my_slot();void do_login();
private:Ui::Widget* ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget* parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);QMovie* mv = new QMovie(":/icon/qq2.gif");ui->label->setMovie(mv);mv->start();ui->edit_pswd->setEchoMode(QLineEdit::Password);connect(ui->btn_exit, SIGNAL(clicked()), this, SLOT(my_slot()));connect(ui->btn_login, &QPushButton::clicked, this, &Widget::do_login);}Widget::~Widget()
{delete ui;
}void Widget::my_slot()
{this->close();
}void Widget::do_login()
{if(ui->edit_user->text() != "admin") {QMessageBox::warning(this, "提示", "账户错误!");} else {if(ui->edit_pswd->text() != "123456") {QMessageBox::warning(this, "提示", "密码错误,请重新输入!");ui->edit_pswd->setText("");} else {QMessageBox::information(this, "提示", "登录成功!");this->close();}}
}