一、作業(yè)要求:
1、找出槽函數(shù)調(diào)用兩次的原因
????????在Qt中使用信號調(diào)用槽函數(shù)時,當(dāng)槽函數(shù)的函數(shù)名定義格式為 “on_對象名_信號”時,則不需要在使用connect來連接信號與槽函數(shù),系統(tǒng)會自動連接信號與槽函數(shù)。如果再次使用了connect來連接,則會調(diào)用兩次槽函數(shù)。
2、完善登錄界面,登錄按鈕對應(yīng)的槽函數(shù)中,判斷所填寫賬戶密碼是否正確,默認(rèn)賬戶“admin”,密碼“123456”,如果匹配成功,則輸出登錄成功并關(guān)閉界面,如果登錄失敗,輸出登錄失敗,并清空密碼框中的內(nèi)容,點擊取消按鈕,則關(guān)閉界面
實現(xiàn)過程:
頭文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void onclose();
void sign();
private:
QLabel *lab1; //無參構(gòu)造
QLabel *lab2; //定義標(biāo)簽,順便指定父組件
QLabel *lab3; //定義標(biāo)簽,順便指定父組件
QLineEdit *edit1; //無參構(gòu)造
QLineEdit *edit3;
QPushButton *btn; //無參構(gòu)造
QPushButton *btn1; //構(gòu)造按鈕時,指定父組件
QMessageBox *box; //彈窗
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>
//完善登錄界面,登錄按鈕對應(yīng)的槽函數(shù)中,判斷所填寫賬戶密碼是否正確,默認(rèn)賬戶“admin”,密碼“123456”,
//如果匹配成功,則輸出登錄成功并關(guān)閉界面,如果登錄失敗,輸出登錄失敗,并清空密碼框中的內(nèi)容,點擊取消按鈕,則關(guān)閉界面
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//設(shè)置窗口標(biāo)題
this->setWindowTitle(QString("QQ")); //使用匿名對象進(jìn)行傳遞
//設(shè)置窗口圖標(biāo)
this->setWindowIcon(QIcon("F:/2022_Cplus_pro/day8/01_frame/1.png"));
//設(shè)置固定尺寸
this->setFixedSize(800,600);
//定義標(biāo)簽Lable
lab1=new QLabel; //無參構(gòu)造
lab1->setParent(this); //設(shè)置父組件
lab1->setText("賬戶:"); //設(shè)置文本信息
lab1->move(230,250); //移動組件位置
lab1->resize(200,40);
lab2=new QLabel(this); //定義標(biāo)簽,順便指定父組件
lab2->setText("密碼:"); //設(shè)置文本信息
lab2->move(230,330);
lab1->resize(200,40);
lab3=new QLabel(this); //定義標(biāo)簽,順便指定父組件
lab3->resize(800,200);
lab3->setScaledContents(true);
lab3->setPixmap(QPixmap("F:/2022_Cplus_pro/day8/01_frame/2.png"));
//定義行編輯器edit
edit1=new QLineEdit; //無參構(gòu)造
edit1->setParent(this); //設(shè)置父組件
edit1->move(300,250); //移動組件位置
edit1->resize(200,40);
edit3=new QLineEdit(this);
edit3->move(300,330); //移動組件位置
edit3->setEchoMode(QLineEdit::Password);
edit3->resize(200,40);
//自定義一個按鈕button
btn=new QPushButton; //無參構(gòu)造
btn->setParent(this); //設(shè)置父組件函數(shù)
btn->resize(80,40); //重新設(shè)置按鈕框大小
btn->setText("登錄"); //設(shè)置按鈕上的文本信息
btn->move(400,400); //移動組件位置
//定義按鈕1,順便指定父組件
btn1=new QPushButton(this); //構(gòu)造按鈕時,指定父組件
btn1->setText("按鈕1"); //設(shè)置按鈕上的文本信息
btn1->resize(80,40); //重新設(shè)置按鈕框大小
btn1->setText("取消"); //設(shè)置按鈕上的文本信息
btn1->move(500,400); //移動組件位置
connect(btn1,SIGNAL(clicked()),this,SLOT(onclose())); //連接按鈕btn1關(guān)閉窗口信號和槽函數(shù)
connect(btn,SIGNAL(clicked()),this,SLOT(sign())); //連接按鈕btn登錄信號和槽函數(shù)
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::sign() //登錄槽函數(shù)
{
box=new QMessageBox; //彈窗
box->setWindowTitle("提示");
if(QString(this->edit1->text())=="admin")
if(QString(this->edit3->text())=="123456")
{
box->setText("登錄成功");
box->show();
box->exec();//等待用戶響應(yīng)
close();
}
else if(QString(this->edit3->text())==0)
{
box->setText("請輸入密碼");
box->show();//展示
box->exec();//等待用戶響應(yīng)
}
else
{
box->setText("密碼錯誤");
this->edit3->clear();
box->show();//展示
box->exec();//等待用戶響應(yīng)
}
else if(QString(this->edit1->text())==0)
{
box->setText("請輸入賬號");
box->show();//展示
box->exec();//等待用戶響應(yīng)
}
else
{
box->setText("賬號錯誤");
box->show();//展示
box->exec();//等待用戶響應(yīng)
}
}
void MainWindow::onclose() //退出槽函數(shù)
{
close();
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-599832.html
測試文件
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
實現(xiàn)效果:
?文章來源:http://www.zghlxwxcb.cn/news/detail-599832.html
?
到了這里,關(guān)于QT實現(xiàn)窗口的賬戶以及密碼判斷登錄和退出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!