qt signal and slot mechanism, login interface jump

Login interface jump

configuration file.pro

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    form.cpp \
    main.cpp \
    widget.cpp

HEADERS += \
    form.h \
    widget.h

FORMS += \
    form.ui \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    icon.qrc

header file.head

#ifndef FORM_H
#define FORM_H

#include <QWidget>    //父类所在的头文件
#include <QLabel>    //标签类所在的头文件

namespace Ui {
class Form;
}

class Form : public QWidget         //封装一个类,公共继承与QWidget
{
    Q_OBJECT           //信号和槽的元对象

public:
    explicit Form(QWidget *parent = nullptr);          //构造函数的声明, 默认参数nullptr
    ~Form();               //析构函数的声明

private:
    Ui::Form *ui;          //通过ui指针可以找到通过图形化界面拖拽的组件,ui->组件名
    //定义一个标签指针
    QLabel *lab1;
};

#endif // FORM_H
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QIcon>
#include <QLineEdit>
#include <QPushButton>
#include <QTextToSpeech>
#include <QtDebug>>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

signals:
    void my_signal(); //定义一个信号函数
    void jump();   //声明一个跳转的信号函数
public slots:

    void  my_login_slot();        //声明一个处理登录按钮发射的信号的槽函数
    void  my_cancle_slot();      //声明一个处理取消按钮发射的信号的槽函数

private:
    Ui::Widget *ui;
    //定义一个标签指针
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    //定义行编辑器指针
    QLineEdit *Edit1;
    QLineEdit *Edit2;

    //定义btn按钮指针
    QPushButton *btn1;
    QPushButton *btn2;

    //定义一个播报员指针
    QTextToSpeech *speecher;

};
#endif // WIDGET_H

source files.sources

#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :        //定义构造函数的实现
    QWidget(parent),
    ui(new Ui::Form)               //实例化ui指针的空间
{
    ui->setupUi(this);           //实例化通过图形化界面拖拽处理的组件的空间,让其依附此界面而存在

    //固定页面大小
    this->setFixedSize(400,400);
    //给lab1实例化空间
    lab1 = new QLabel("我爱学习", this);
    //设置大小
    lab1->resize(80,40);
    //移动位置
    lab1->move(160,180);

}

//析构函数的实现
Form::~Form()
{
    delete ui;         //释放指针成员ui的空间
}
#include "widget.h"   //自定义类Weidget所在的头文件widget
#include "form.h"     //自定义类Form所的头文件form

#include <QApplication>    //应用程序类所在的头文件

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);   //实例化一个应用程序的类对象a,调用有参构造函数
    Widget w;           //实例化一个Widget的类对象w,调用无参构造函数
    w.show();          //调用show函数,展示图形化界面
    Form f;           //实例化一个类对象f,调用无参构造函数

    //将w中发射的jump信号连接到f的show函数中
    QObject::connect(&w, &Widget::jump, &f, &Form::show);  //因为connect函数是由QObject类提供的,main函数中没有这个类,所以需要手动指定
    return a.exec();
}
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    speecher = new QTextToSpeech();

    //固定窗口大小
    this->setFixedSize(600, 560);
    //设置窗口标题
    this->setWindowTitle("hello");
    //设置窗口图标
    this->setWindowIcon(QIcon("C:/Users/asus/Desktop/icon/wodepeizhenshi.png"));
    //实例化空间,指定父组件,依附于此界面存在
    lab1 = new QLabel(this);
    //设置大小
    lab1->resize(600, 250);
    //设置图片
    lab1->setPixmap(QPixmap(":/new/prefix1/icon/logo.png"));

    //设置图片自适应
    lab1->setScaledContents(true);

    //为lab2实例化空间,指定父组件
    lab2 = new QLabel(this);
    //设置大小
    lab2->resize(30, 30);
    //设置图片
    lab2->setPixmap(QPixmap(":/new/prefix1/icon/userName.jpg"));
    //设置图片自适应
    lab2->setScaledContents(true);
    //移动位置
    lab2->move(140, 320);

    //为lab3实例化空间
    lab3 = new QLabel();
    //设置指定父组件,依附此界面存在
    lab3->setParent(this);
    //设置大小
    lab3->resize(30, 30);
    //设置图片
    lab3->setPixmap(QPixmap(":/new/prefix1/icon/passwd.jpg"));
    //设置图片自适应
    lab3->setScaledContents(true);
    //移动位置
    lab3->move(lab2->x(), lab2->y()+80);

    //无参构造一个行编辑器 QLineEdit
    Edit1 = new QLineEdit();
    //指定父组件,让其依附于此界面存在
    Edit1->setParent(this);
    //设置大小
    Edit1->resize(240, 40);
    //设置背景色
    Edit1->setStyleSheet("background-color:white; border: 1px solid gray; padding: 10px;");
    //移动位置
    Edit1->move(lab2->x()+80, lab2->y()-10);
    //设置占位文本
    Edit1->setPlaceholderText("QQ/手机/邮箱");

    //有参构造一个行编辑器 QLineEdit
    Edit2 = new QLineEdit(this);
    Edit2->resize(Edit1->size());
    //设置北京四
    Edit2->setStyleSheet("background-color:white; border: 1px solid gray; padding: 10px;");
    //移动位置
    Edit2->move(lab3->x()+80, lab3->y()-10);
    //设置文本位置
    Edit2->setPlaceholderText("密码");
    //设置回显模式
    Edit2->setEchoMode(QLineEdit::Password);

    //有参构造一个命令按钮,初始化文本内容,指定父组件 QPushButton
    QPushButton *btn1 = new QPushButton("登录", this);
    //设置大小
    btn1->resize(80, 40);
    //移动位置
    btn1->move(240, Edit2->y()+80);
    //设置图片
    btn1->setIcon(QIcon(":/new/prefix1/icon/login.png"));

    //有参构造一个命令按钮,初始化图标和文本内容,指定父组件QPushButton
    QPushButton *btn2 = new QPushButton(QIcon(":/new/prefix1/icon/cancel.png"), "取消", this);
    //设置大小
    btn2->resize(btn1->size());
    //移动位置
    btn2->move(btn1->x()+120, btn1->y());
    
    Edit1->clear();

    //qt5实现连接
    connect(btn1, &QPushButton::clicked, this, &Widget::my_login_slot);
    //参数1:信号发送者
    //参数2:要发送的信号,发送的信号函数的函数指针变量(函数名),指定是哪个类的, 该信号函数必须是该组件所在类中所具有的信号函数
    //参数3:信号的接收者
    //参数4:槽函数, 槽函数的函数指针,指定是哪个类的,该槽函数必须是该组件所在类中具有的槽函数

    //qt4实现连接
   // connect(btn2, SIGNAL(clicked()), this, SLOT(my_cancle_slot()));
    //参数1: 信号的发送者
    //参数2: 要发送的信号, 该信号函数必须是该组件中所在类中所具有的信号函数,需要使用SIGNAL宏来转换
    //参数3: 信号的接收者
    //参数4: 槽函数,该槽函数必须是该组件所在类中具有的槽函数,需要使用SLOT宏来转换
    connect(btn2, SIGNAL(clicked()), this, SLOT(close()));



}

Widget::~Widget()
{
    delete ui;
}

//自定义槽函数的实现
void  Widget::my_login_slot()
{
    //获取ui界面上的用户名和密码
    //Qstring username = Edit1->text();
    //Qstring passwd = Edit2->text();
    if(Edit1->text() == "admin" && Edit2->text() == "123456")
    {
        emit jump();
        qDebug() << "登录成功";
        speecher->say("登录成功");
        this->close();
    }
    else
    {
        qDebug() << "登录失败";
        speecher->say("登录失败");
        Edit2->clear();
    }
}
void Widget::my_cancle_slot()
{
    this->close();
}

1. Polymorphism, virtual function, pure virtual function?
Answer: Polymorphism is divided into static polymorphism and dynamic polymorphism. Static polymorphism includes function overloading and operator overloading. Function overloading means that in the same scope, the same function name and different parameter lists implement different functions. Operator overloading means that To restructure operators to achieve more complex functions. Dynamic polymorphism is represented as a pointer or reference of the parent class, which points to or initializes the object of the subclass, calls the function that the subclass overrides the parent class, and then expands the functions of the subclass.

        First of all, there are many prerequisites for the operation of dynamic polymorphism. The first point is that there must be an inheritance relationship, because dynamic polymorphism occurs between parent and child classes. The second point is that the subclass and the parent class have functions of the same name and type. Only when the subclass has a function of the same name and type as the parent class can the function be rewritten. The third point is the rewritten function of the parent class. Must be a virtual function.

        Virtual function, a function modified with virtual is a virtual function. When there is a virtual function in a class, there will be a virtual pointer in the class. The virtual pointer points to the virtual function table. The virtual function table records all virtual functions and subclasses. A function overridden by the parent class.

        When the virtual function in the parent class has no practical meaning, the virtual function can be set to a pure virtual function. A class containing a pure virtual function is called an abstract class. Abstract classes cannot instantiate objects. When a subclass has no access to the parent class, When pure virtual functions are recreated, the subclass is also called an abstract class.

2. What are the characteristics of using "reference" as a function parameter?
Answer: Function parameters are the bridge for data interaction between programs, and are generally divided into value transfer and address transfer. Passing by value means passing the value without changing the original size of the value. Address transfer transfers the address. When the content pointed to by the address is accessed through the address, its content can be changed. The essence of the reference is to use an alias, and once the pointer is determined, it cannot be changed. When using references as function parameters, there is no need to re-open space, which is highly efficient. The corresponding content can be directly changed through references. When referencing a variable that you do not want to change, you can use const modification. In this case, it is a constant reference. A constant reference cannot modify the size of the value.

3. What is the difference between a structure and a union?
Answer: Structures and unions are both constructed data types, and are constructed from the same or different data types. However, the address of each member of the structure is continuous, and the size of the structure is determined by the byte alignment principle of the byte size of each member. The size of the union is determined by the largest byte among its members, and all members share a space.
 

Guess you like

Origin blog.csdn.net/qq_46766479/article/details/132612553