QT注册界面练习(信号与槽实现页面跳转)

一、注册界面练习思路以及具体代码

        在完成注册页面搭建的前提下,通过信号与槽机制实现多组件之间的相互通信,实现页面跳转。

基本步骤:

        首先,将注册页面的登录按钮与成功登陆信号绑定,当用户名与密码均匹配时,关闭注册页面,发送跳转信号。

        其次,在成功登陆页面中设置槽函数,展示成功登陆界面。

        最后,将跳转信号与成功登陆页面槽函数进行绑定,当触发跳转信号后,调用相应槽函数。具体代码如下:

注册页面头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

signals:
    void login_success();

public slots:
    void my_slot();

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

private:
    Ui::Widget *ui;
    QLabel *lab_logo;
    QLabel *lab_name;
    QLabel *lab_passwd;
    QLineEdit *edit_name;
    QLineEdit *edit_passwd;
    QPushButton *btn_login;
    QPushButton *btn_cancel;
    QPushButton *btn3;
};
#endif // WIDGET_H

 注册页面核心代码

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置固定窗口尺寸
    this->setFixedSize(540,410);
    //设置窗口标题
    this->setWindowTitle("My_QQ");
    //设置窗口图标
    this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));

    //设置logo
    lab_logo = new QLabel("LOGO",this);
    lab_logo->resize(540,205);
    lab_logo->setPixmap(QPixmap(":/icon/logo.png"));
    lab_logo->setScaledContents(true);

    lab_name = new QLabel("账号",this);
    lab_name->resize(35,35);
    lab_name->move(115,230);
    lab_name->setPixmap(QPixmap(":/icon/userName.jpg"));
    lab_name->setScaledContents(true);

    lab_passwd = new QLabel("密码",this);
    lab_passwd->resize(35,35);
    lab_passwd->move(lab_name->x(),lab_name->y()+65);
    lab_passwd->setPixmap(QPixmap(":/icon/passwd.jpg"));
    lab_passwd->setScaledContents(true);

    //2、构造行编辑器,构造时给定父组件
    edit_name = new QLineEdit(this);
    edit_name->setPlaceholderText("QQ/手机/邮箱");           //设置编辑器的占位文本
    edit_name->resize(230,40);                              //设置尺寸
    edit_name->move(lab_name->x()+80,lab_name->y());                //移动位置
    //edit1->setStyleSheet("broder-color:black");         //设置样式表

    //3、构造行编辑器,构造时给定父组件以及文本内容
    edit_passwd = new QLineEdit(this);
    edit_passwd->setPlaceholderText("密码");                   //设置编辑器的占位文本
    edit_passwd->resize(230,40);                              //设置尺寸
    edit_passwd->move(lab_passwd->x()+80,lab_passwd->y());                //移动位置
    edit_passwd->resize(edit_name->size());
    edit_passwd->move(edit_name->x(),edit_name->y()+60);
    edit_passwd->setEchoMode(QLineEdit::Password);             //设置回显模式

    //4、使用无参构造添加一个按钮
    btn_login = new QPushButton;   //无参构造
    btn_login->setParent(this);         //给组件指定父组件,让其依附于界面而存在
    btn_login->setText("登录");         //给组件设置文本内容
    btn_login->resize(QSize(90,35));   //设置按钮组件的大小
    btn_login->move(195,360);          //移动组件位置
    btn_login->setIcon(QIcon(":/icon/login.png"));

    //5、构造按钮时,指定父组件
    btn_cancel = new QPushButton(this);      //将当前界面设置成父组件
    btn_cancel->setText("取消");
    btn_cancel->resize(btn_login->size());                     //使用其他按钮的大小设置该组件的大小
    btn_cancel->move(btn_login->x()+140,btn_login->y());
    btn_cancel->setIcon(QIcon(":/icon/cancel.png"));    //设置图标

    connect(btn_login,&QPushButton::clicked,this,&Widget::my_slot);

    connect(btn_login,SIGNAL(clicked()),this,SLOT(close()));
}

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

void Widget::my_slot()
{
    if(edit_name->text()=="admin" &&edit_passwd->text()=="123456")
    {
        qDebug() << "登陆成功";
        this->close();
        emit login_success();
    }
    else
    {
        qDebug() << "登录失败";
        this->edit_passwd->clear();
    }
}

 成功登陆页面头文件

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QLabel>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public slots:
    void login_success();


public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();

private:
    Ui::Form *ui;
    QLabel *lab_title;
};

#endif // FORM_H

成功登陆页面核心代码

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


Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    lab_title = new QLabel(this);
    this->lab_title->setText("登陆成功");
}

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

void Form::login_success()
{
    this->show();
}

主函数

#include "widget.h"
#include "form.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    Form f;

    w.connect(&w,&Widget::login_success,&f,&Form::login_success);
    return a.exec();
}

项目管理文件

QT       += core gui

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

 二、面试题简述

1、多态、虚函数、纯虚函数?

答:多态分为静态多态动态多态,静态多态有函数重载、运算符重载,函数重载表示在同一作用域下,相同函数名,不同参数列表实现不同的功能,运算符重载表示为对运算符的重新构建从而实现更多复杂的功能。动态多态表示为父类的指针或引用,指向或初始化子类的对象,调用子类对父类重写的函数,进而展开子类的功能。

        首先动态多态的运行存在许多前提条件,第一点是必须有继承关系,因为动态多态发生在父子类之间。第二点是子类和父类有他们同名同类型的函数,只有子类中有与父类同名同类型的函数时才能实现对函数的重写,第三点是父类被重写的函数必须为虚函数。

        虚函数,用virtual修饰的函数即为虚函数,当类中存在虚函数,那么该类中就会有一个虚指针,虚指针指向虚函数表,虚函数表中记录了所有虚函数以及子类对父类重写的函数。

        当父类中的虚函数没有实际意义时,可将该虚函数设置为纯虚函数,含有纯虚函数的类被称为抽象类,抽象类不能实例化对象,当子类没有对父类的纯虚函数进行重新时,子类也被称为抽象类。

2、将“引用”作为函数参数有哪些特点?

答:函数参数是程序间数据交互的桥梁,一般分为值传递和地址传递。值传递,传递的是值,不改变值原本的大小。地址传递,传递的是地址,当通过地址访问到其地址所指向的内容时,其内容可以发生改变。引用的实质为取别名,一旦确定指向不能更改。使用引用作为函数参数时,不需要重新开辟空间,效率高,通过引用可直接改变其对应的内容。当引用不想被改变的变量时,可使用const修饰,此时为常引用,常引用不能修改值的大小。

3、结构体和联合体有何区别?

答:结构体与联合体都是构造数据类型,都是由相同或不同的数据类型构造而成。但是结构体每个成员地址连续,结构体大小由每个成员的字节大小字节对齐原则决定。而联合体大小由其成员中字节最大的决定,所有成员共用一片空间。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值