使用手动连接,将登录框中的取消按钮使用第二中连接方式,右击转到槽,在该槽函数中,调用关闭函数,将登录按钮使用qt4版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空。
//头文件 #ifndef MYWIDGET_H #define MYWIDGET_H #include #include #include #include #include #include QT_BEGIN_NAMESPACE namespace Ui { class MyWidget; } QT_END_NAMESPACE class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr); ~MyWidget(); public slots: void my_slots(); private slots: void on_LogoutpushButton_clicked(); private: Ui::MyWidget *ui; }; #endif // MYWIDGET_H //源程序 #include "mywidget.h" #include "ui_mywidget.h" MyWidget::MyWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::MyWidget) { ui->setupUi(this); //设置窗口标题 this->setWindowTitle("QQ"); //设置窗口图标 this->setWindowIcon(QIcon("C:/Users/EDY/Desktop/QT_items/QQ_01.jpeg")); //设置固定窗口大小 this->setFixedSize(320,448); //设置背景图片 QMovie *mv = new QMovie(":/QQ_111.gif"); ui->Background->setMovie(mv); ui->Background->setScaledContents(1); mv->start(); //设置Logo ui->Logo->setPixmap(QPixmap(":/QQ_222.png")); ui->Logo->setScaledContents(1); //设置账号密码小图标 ui->accountLabel->setPixmap(QPixmap(":/OIP-C (1).jfif")); ui->accountLabel->setScaledContents(1); ui->passwordLabel->setPixmap(QPixmap(":/kKh3eZiksQ.jpg")); ui->passwordLabel->setScaledContents(1); //设置账号密码编辑栏 ui->accountEdit->setPlaceholderText("请输入QQ账号"); ui->passwordEdit->setPlaceholderText("请输入密码"); ui->passwordEdit->setEchoMode(QLineEdit::Password); //设置登录退出按钮背景色 ui->LoginpushButton->setStyleSheet("background-color:rgb(154,216,255)"); ui->LogoutpushButton->setStyleSheet("background-color:rgb(154,216,255)"); //点击退出,关闭窗口 connect(ui->LogoutpushButton,SIGNAL(clicked()),this,SLOT(on_LogoutpushButton_clicked())); //点击登录,如果成功关闭该界面,失败则提示账号或密码不正确 connect(ui->LoginpushButton,SIGNAL(clicked()),this,SLOT(my_slots())); } MyWidget::~MyWidget() { delete ui; } void MyWidget::my_slots() { if(ui->accountEdit->text()!="admin" || ui->passwordEdit->text()!="123456") { ui->accountEdit->setText("账号或密码不正确!"); ui->passwordEdit->setText("账号或密码不正确!"); ui->passwordEdit->setEchoMode(QLineEdit::Normal); } else { this->close(); } } void MyWidget::on_LogoutpushButton_clicked() { this->close(); }