做外貿(mào)網(wǎng)站要花多少錢怎么制作網(wǎng)頁教程
作業(yè)
完善登錄框
點擊登錄按鈕后,判斷賬號(admin)和密碼(123456)是否一致,如果匹配失敗,則彈出錯誤對話框,文本內(nèi)容“賬號密碼不匹配,是否重新登錄”,給定兩個按鈕ok和cancel,點擊ok后,會清除密碼框中的內(nèi)容,繼續(xù)進(jìn)行登錄;如果點擊cancel按鈕,則關(guān)閉界面。
如果賬號和密碼匹配,則彈出信息對話框,給出提示信息為“登錄成功”,給出一個按鈕ok,點擊ok后,關(guān)閉整個登錄界面,跳轉(zhuǎn)到其他界面
點擊取消按鈕后,彈出問題對話框,詢問是否確定要退出登錄,給出兩個按鈕,yes|no,點擊yes,則直接關(guān)閉整個登錄界面,如果點擊no則進(jìn)行進(jìn)行登錄
要求:消息對話框,對象版和靜態(tài)成員函數(shù)版至少各實現(xiàn)一個
【免費】2023/9/18-C++/QT實現(xiàn)簡易登錄資源-CSDN文庫https://download.csdn.net/download/weixin_54147737/88354416
一、信號與槽機(jī)制
1.1 簡介
1> 信號與槽機(jī)制,是qt引以為傲的、能夠立于編程界而不倒的核心機(jī)制
2> 作用:實現(xiàn)多個組件之間的互相通信,一個組件發(fā)信號,另一個組件接收信號后,做相應(yīng)的響應(yīng)
3> 原理類似于IO中的兩個進(jìn)程間通過信號通信,但是,這里的信號,是可以傳遞信息的
1.2 信號與槽
1> 信號:信號函數(shù),定義在類體內(nèi)signals權(quán)限下的特殊成員函數(shù),該函數(shù)是不完整的函數(shù),只有聲明,沒有定義,返回值為void,參數(shù)就是傳遞的信息
2> 槽:槽函數(shù),定義在類體內(nèi)slots權(quán)限下的成員函數(shù),該函數(shù)是完整的函數(shù),既有聲明也有定義,返回值為void類型,參數(shù)就是接收信號的信息
3> 槽函數(shù)可以被當(dāng)作普通函數(shù)一樣調(diào)用,但是普通函數(shù)不能當(dāng)作槽函數(shù)一樣連接信號
4> 信號函數(shù)和槽函數(shù),不是標(biāo)準(zhǔn)的C++代碼,通過moc.exe工具將其自動轉(zhuǎn)化為標(biāo)準(zhǔn)的C++代碼
5> 信號函數(shù)和槽函數(shù)可以是組件自帶的,也可以由用戶自定義,系統(tǒng)提供的每個組件都會自帶信號與槽
6> 包含信號與槽的類體定義
class Widget : public QWidget
{Q_OBJECT //有關(guān)信號與槽的元對象signals: //該權(quán)限下定義屬于自己的信號void my_signal(); //自定義一個無參無返回值的信號函數(shù)private slots:void my_slot(); //自定義無參無返回值的槽函數(shù)public:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H
1.3 信號與槽的連接
1> 基于ui界面的,手動選中信號與槽的連接,該連接只適用于系統(tǒng)提供的信號與槽的連接,連接函數(shù)也是由系統(tǒng)實現(xiàn)
2> 也是基于ui界面,在組件上右鍵轉(zhuǎn)到槽,手動實現(xiàn)槽函數(shù)的內(nèi)容,該方法只能更改槽函數(shù)邏輯,信號、連接都是由系統(tǒng)實現(xiàn)
3> qt4版本手動連接信號與槽,該連接是不友好的連接,因為兩個宏的原因,它會將所有傳遞的數(shù)據(jù)都改成字符串類型
[static] QMetaObject::Connection //返回值:是建立的信號與槽的連接,是一個靜態(tài)成員函數(shù)QObject::connect( //函數(shù)名const QObject *sender, //信號發(fā)射者,是組件的指針const char *signal, //要發(fā)射的信號函數(shù),函數(shù)是函數(shù)指針,要的是字符串類型,所以需要使用SIGNAL()宏進(jìn)行轉(zhuǎn)換后使用const QObject *receiver, //信號接收者,是組件的指針const char *method) //接收信號后,接收者要處理的槽函數(shù),函數(shù)是函數(shù)指針,要的是字符串類型,所以需要使用SLOT()宏進(jìn)行轉(zhuǎn)換后使用
舉個例子:QLabel *label = new QLabel;QScrollBar *scrollBar = new QScrollBar;QObject::connect(scrollBar, SIGNAL(valueChanged(int)),label, SLOT(setNum(int)));
4> qt5版本的信號與槽的連接
[static] QMetaObject::Connection //返回值:是建立的信號與槽的連接,是一個靜態(tài)成員函數(shù)QObject::connect( //函數(shù)名const QObject *sender, //信號發(fā)射者,是組件的指針PointerToMemberFunction signal, //信號函數(shù)的指針,要指定是哪個類中的哪個成員函數(shù)const QObject *receiver, //信號接收者,是組件的指針PointerToMemberFunction method) //槽函數(shù)的指針,要指定是哪個類中的哪個成員函數(shù)
舉個例子:QLabel *label = new QLabel;QLineEdit *lineEdit = new QLineEdit;QObject::connect(lineEdit, &QLineEdit::textChanged,label, &QLabel::setText);
5> 信號連接功能函數(shù)
[static] QMetaObject::Connection //返回值:是建立的信號與槽的連接,是一個靜態(tài)成員函數(shù)QObject::connect( //函數(shù)名const QObject *sender, //信號發(fā)射者,是組件的指針PointerToMemberFunction signal, //信號函數(shù)的指針,要指定是哪個類中的哪個成員函數(shù)Functor functor) //功能函數(shù):全局函數(shù)作為功能函數(shù)、仿函數(shù)作為功能函數(shù)、lambda表達(dá)式作為功能函數(shù)
舉個例子1,全局函數(shù)作為功能函數(shù):void someFunction();QPushButton *button = new QPushButton;QObject::connect(button, &QPushButton::clicked, someFunction);舉個例子2:lambda作為功能函數(shù)QByteArray page = ...;QTcpSocket *socket = new QTcpSocket;socket->connectToHost("qt-project.org", 80);QObject::connect(socket, &QTcpSocket::connected, [=] () {socket->write("GET " + page + "\r\n");});
6> 信號與槽的斷開
只需將之前的連接函數(shù)的connect改成disconnect即可,參數(shù)不變
1.4 信號的定義與發(fā)射
1、在類體的signals權(quán)限下,定義屬于自己的信號
2、信號函數(shù)只有聲明,沒有定義,返回值為void,可用有參數(shù)
3、在程序所需處,使用關(guān)鍵字emit發(fā)射自定義的信號,格式:emit 信號名(實參);
4、信號函數(shù)可以連接到槽函數(shù)中,也可以連接另一個信號函數(shù),表明發(fā)射第一個信號時,第二個信號跟著發(fā)射
1.5 信號與槽的總結(jié)
1> 一個信號可以連接多個槽函數(shù)
2> 一個槽函數(shù)可以連接多個信號函數(shù)
3> 一個信號函數(shù)可以連接到槽函數(shù)中,也可以連接另一個信號
4> 信號函數(shù)和槽函數(shù)參數(shù)個數(shù)
1、信號函數(shù)和槽函數(shù)進(jìn)行鏈接時,一般要求信號函數(shù)和槽函數(shù)的參數(shù)保持一致connect(信號發(fā)送者, SIGNAL(signalFun()),信號接收者, SLOT(slotFun())); //Okconnect(信號發(fā)送者, SIGNAL(signalFun(int)),信號接收者, SLOT(slotFun(int))); //Okconnect(信號發(fā)送者, SIGNAL(signalFun(int, char)),信號接收者, SLOT(slotFun(int, char))); //Okconnect(信號發(fā)送者, SIGNAL(signalFun(char, int)),信號接收者, SLOT(slotFun(int, char))); //Falseconnect(信號發(fā)送者, SIGNAL(signalFun(int)),信號接收者, SLOT(slotFun(char))); //False2、當(dāng)信號函數(shù)的參數(shù)大于槽函數(shù)的參數(shù)時connect(信號發(fā)送者, SIGNAL(signalFun(int, char)),信號接收者, SLOT(slotFun())); //Okconnect(信號發(fā)送者, SIGNAL(signalFun(int, char)),信號接收者, SLOT(slotFun(int))); //Okconnect(信號發(fā)送者, SIGNAL(signalFun(int, char)),信號接收者, SLOT(slotFun(char))); //False3、當(dāng)信號函數(shù)的參數(shù)小于槽函數(shù)的參數(shù)時connect(信號發(fā)送者, SIGNAL(signalFun(int)),信號接收者, SLOT(slotFun(int, char))); //Falseconnect(信號發(fā)送者, SIGNAL(signalFun(int)),信號接收者, SLOT(slotFun(int, char=0))); //Ok
1.6 使用信號與槽實現(xiàn)簡單的頁面跳轉(zhuǎn)
1> 準(zhǔn)備兩個界面
2> 在第一個界面中,定義跳轉(zhuǎn)的信號函數(shù),第二個界面定義槽函數(shù)
3> 可以在主程序中實例化兩個界面,第一個界面調(diào)用show函數(shù),第二個不調(diào)用,并且在主調(diào)函數(shù)中將第一個界面的信號與第二個界面的槽函數(shù)連接
1) 頭文件
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include"second.h"QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();signals:void jump(); //自定義跳轉(zhuǎn)信號函數(shù)private slots:void on_loginBtn_clicked();private:Ui::Widget *ui;Second *s1; //定義另一個界面的指針
};
#endif // WIDGET_H
second.h
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class Second;
}class Second : public QWidget
{Q_OBJECTpublic slots:void jump_slot(); //接收跳轉(zhuǎn)信號的槽函數(shù)public:explicit Second(QWidget *parent = nullptr);~Second();private:Ui::Second *ui;
};#endif // SECOND_H
2) 源文件
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);s1 = new Second; //給另一個界面實例化空間//將當(dāng)前界面的信號,與s1界面的槽函數(shù)進(jìn)行連接connect(this,&Widget::jump, s1, &Second::jump_slot);
}Widget::~Widget()
{delete ui;
}void Widget::on_loginBtn_clicked()
{emit jump();this->hide(); //將當(dāng)前界面隱藏
}
second.cpp
#include "second.h"
#include "ui_second.h"Second::Second(QWidget *parent) :QWidget(parent),ui(new Ui::Second)
{ui->setupUi(this);
}Second::~Second()
{delete ui;
}//接收跳轉(zhuǎn)信號對應(yīng)的槽函數(shù)
void Second::jump_slot()
{this->show(); //將自己界面進(jìn)行展示
}
3) 主程序
#include "widget.h"
#include"second.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();//Second s; //定義第二個界面//連接w的信號與s的槽//QObject::connect(&w, &Widget::jump, &s, &Second::jump_slot);return a.exec();
}
二、對話框
2.1 消息對話框(QMessageBox)
1> 該類提供的消息對話框,用于提高用戶的交互性,可以提供問題對話框、信息對話框、警告對話框、錯誤對話框。。。
2> 實現(xiàn)該對話框的方式有兩種:基于屬性版本、靜態(tài)成員函數(shù)版本
3> 基于屬性版本實現(xiàn)消息對話框:公共該類實例化對象,調(diào)用exec函數(shù)進(jìn)入執(zhí)行態(tài),該函數(shù)返回值時用戶點擊的按鈕
QMessageBox::QMessageBox( //構(gòu)造函數(shù)函數(shù)名QMessageBox::Icon icon, //圖標(biāo)const QString &title, //對話框標(biāo)題const QString &text, //對話框提示文本內(nèi)容QMessageBox::StandardButtons buttons = NoButton, //對話框提供的按鈕,如果有多個按鈕,則使用位或隔開QWidget *parent = nullptr) //父組件參數(shù)1介紹,對話框提供的圖標(biāo),該類中的枚舉值ConstantValueDescription
QMessageBox::NoIcon0the message box does not have any icon.//不提供任何圖標(biāo)
QMessageBox::Question4an icon indicating that the message is asking a question.//提供一個問號圖標(biāo)
QMessageBox::Information1an icon indicating that the message is nothing out of the ordinary.//提供一個i符號圖標(biāo)
QMessageBox::Warning2an icon indicating that the message is a warning, but can be dealt with.//提供一個感嘆號圖標(biāo)
QMessageBox::Critical3an icon indicating that the message represents a critical problem. //提供一個叉號圖標(biāo)參數(shù)4介紹,對話框上提供的標(biāo)準(zhǔn)按鈕,時類中的枚舉值,如果有多個按鈕,中間有位或隔開
ConstantValueDescription
QMessageBox::Ok0x00000400An "OK" button defined with the AcceptRole. //提供一個ok按鈕
QMessageBox::Open0x00002000An "Open" button defined with the AcceptRole. //提供一個open按鈕
QMessageBox::Save0x00000800A "Save" button defined with the AcceptRole. //提供一個保存按鈕
QMessageBox::Cancel0x00400000A "Cancel" button defined with the RejectRole. //提供一個取消按鈕
QMessageBox::Close0x00200000A "Close" button defined with the RejectRole. //提供一個關(guān)閉按鈕
QMessageBox::Discard0x00800000A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.//不保存
舉個例子:QMessageBox msgBox; //實例化對象msgBox.setText("The document has been modified.");msgBox.setInformativeText("Do you want to save your changes?");msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);msgBox.setDefaultButton(QMessageBox::Save);int ret = msgBox.exec(); //展示對話框//判斷用戶點擊的結(jié)果switch (ret) {case QMessageBox::Save:// Save was clickedbreak;case QMessageBox::Discard:// Don't Save was clickedbreak;case QMessageBox::Cancel:// Cancel was clickedbreak;default:// should never be reachedbreak;}
4> 基于靜態(tài)成員函數(shù)版本:無需實例化對象,但是只能設(shè)置有限的信息
[static] QMessageBox::StandardButton //函數(shù)返回值類型,是用戶點擊的標(biāo)準(zhǔn)按鈕,該函數(shù)是靜態(tài)成員函數(shù)QMessageBox::warning( //函數(shù)名QWidget *parent, //父組件const QString &title, //對話框標(biāo)題const QString &text, //對話框文本內(nèi)容QMessageBox::StandardButtons buttons = Ok, //提供的標(biāo)準(zhǔn)按鈕,多個按鈕用位或隔開QMessageBox::StandardButton defaultButton = NoButton) //默認(rèn)選中的按鈕舉個例子:int ret = QMessageBox::warning(this, tr("My Application"),tr("The document has been modified.\n""Do you want to save your changes?"),QMessageBox::Save | QMessageBox::Discard| QMessageBox::Cancel,QMessageBox::Save);其余的靜態(tài)成員函數(shù)QMessageBox::StandardButton
critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
QMessageBox::StandardButton
information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
QMessageBox::StandardButton
question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
5> 案例
1) 頭文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QMessageBox> //消息對話框類
#include<QDebug>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_quesBtn_clicked();void on_warnBtn_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
2) 源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}//問題按鈕對應(yīng)的槽函數(shù)
void Widget::on_quesBtn_clicked()
{//1、調(diào)用構(gòu)造函數(shù)實例化對象QMessageBox box(QMessageBox::Question, //圖標(biāo)"問題", //對話框標(biāo)題"晚上約不?", //對話框文本內(nèi)容QMessageBox::Yes | QMessageBox::No, //提供的按鈕this); //父組件box.setDetailedText("有上好的飛天茅臺"); //提供詳細(xì)文本內(nèi)容box.setDefaultButton(QMessageBox::No); //將no設(shè)置成默認(rèn)按鈕box.setButtonText(QMessageBox::Yes, "好的");box.setButtonText(QMessageBox::No, "沒空");//2、調(diào)用exec函數(shù)運行對話框int ret = box.exec();//3、對結(jié)果進(jìn)行判斷if(ret == QMessageBox::Yes){qDebug()<<"木的問題,老地方見";}else if(ret == QMessageBox::No){qDebug()<<"下次一定";}
}//警告按鈕對應(yīng)的槽函數(shù)
void Widget::on_warnBtn_clicked()
{//直接調(diào)用靜態(tài)成員函數(shù)完成對話框的實現(xiàn)int ret = QMessageBox::warning(this, //父組件"警告", //對話框標(biāo)題"放學(xué)等著,別走", //對話框文本內(nèi)容QMessageBox::Yes|QMessageBox::No, //對話題提供的按鈕QMessageBox::No); //默認(rèn)選中的按鈕//對用戶選中的按鈕進(jìn)行判斷if(ret == QMessageBox::Yes){qDebug()<<"行,葷的還是素的,文的還是武的任你挑";}else if(ret == QMessageBox::No){qDebug()<<"你永遠(yuǎn)是我大哥";}
}