完善對話框,點擊登錄對話框,如果賬號和密碼匹配,則彈出信息對話框,給出提示登錄成功,提供一個Ok按鈕,用戶點擊Ok后,關閉登錄界面,跳轉到其他界面
如果賬號和密碼不匹配,彈出錯誤對話框,給出信息"賬號和密碼不匹配,是否重新登錄";并提供兩個按鈕Yes/No,用戶點擊Yes后,清除密碼框中的內容,繼續讓用戶進行登錄,如果用戶點擊No按鈕,則直接關閉登錄界面
如果用戶點擊取消按鈕,則彈出一個問題對話框,給出信息"您是否確定要退出登錄?“,并給出兩個按鈕Yes/No,用戶迪納基Yes后,關閉登錄界面,用戶點擊No后,關閉對話框,繼續執行登錄功能
要求:基于屬性版和基于靜態成員函數版至少各用一個
second.h
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class second;
}class second : public QWidget
{Q_OBJECTpublic:explicit second(QWidget *parent = nullptr);~second();
public:void my_slot();private:Ui::second *ui;
};#endif // SECOND_H
Widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
signals:void my_signal();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_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_signal,&s,&second::my_slot);return a.exec();
}
second.cpp
#include "second.h"
#include "ui_second.h"second::second(QWidget *parent) :QWidget(parent),ui(new Ui::second)
{ui->setupUi(this);this->setWindowFlag(Qt::FramelessWindowHint);//設置純凈窗口this->setAttribute(Qt::WA_TranslucentBackground);//將窗口其他部分不顯示
}second::~second()
{delete ui;
}void second::my_slot()
{this->show();
}
Widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->setWindowFlag(Qt::FramelessWindowHint); //設置純凈窗口this->setAttribute(Qt::WA_TranslucentBackground); //將窗口其他部分不顯示this->setWindowIcon(QIcon(":/Logo/qq.jpg")); //設置軟件圖標
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{//當賬號行編輯器內容和密碼行編輯器內容都匹配時,登陸成功,否則失敗if(ui->lineEdit->text() == "114514" && ui->lineEdit_2->text() == "1919810"){//基于屬性版對話框,信息對話框,只顯示Ok按鈕QMessageBox msg(QMessageBox::Information,"登錄","登錄成功",QMessageBox::Ok,this);int ret = msg.exec(); //exec彈出對話框if(ret == QMessageBox::Ok){emit my_signal(); //發出自定義信號this->close(); //關閉窗口}}else{//基于靜態成員函數版對話框,生成兩個按鈕int ret = QMessageBox::question(this,"錯誤","賬號和密碼不匹配,是否重新登錄?",QMessageBox::Yes | QMessageBox::No);if(ret == QMessageBox::Yes){ui->lineEdit_2->clear(); //將密碼行編輯器內容清空}else if(ret == QMessageBox::No){this->close(); //關閉窗口}}
}void Widget::on_pushButton_2_clicked()
{//基于靜態成員函數版對話框,生成兩個按鈕int ret = QMessageBox::question(this,"退出","您是否確認要退出登錄?",QMessageBox::Yes | QMessageBox::No);if(ret == QMessageBox::Yes){this->close(); //關閉窗口}
}
思維導圖