7.26 作业

1. 完善登录界面

main.c

#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::to_second,&s,&Second::show);//连接主界面的登录按钮和second的show函数
    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

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

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private:
    QLabel *logo;//logo
    QLabel *ulabel;//用户图标
    QLabel *plabel;//密码图标
    QLineEdit *uline;//用户输入行
    QLineEdit *pline;//密码输入行
    QPushButton *loginbutton;//登录按钮
    QPushButton *cancelbutton;//取消按钮
public slots:
    void cancel_slot();//取消槽函数
    void login_slot();//登录槽函数
signals:
    void to_second();//转跳到second
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(800,600);//固定大小
    setWindowOpacity(0.9);//设置透明的
    setWindowTitle("登录");//设置标题
    setWindowIcon(QIcon(":/icon/QQ.png"));//设置图片

    logo = new QLabel(this);
    logo->resize(800,300);//设置大小
    logo->setPixmap(QPixmap(":/icon/112.bmp"));//放入图片
    logo->setScaledContents(true);//自适应大小

    ulabel = new QLabel(this);
    ulabel->move(200,320);//移动位置
    ulabel->resize(50,50);
    ulabel->setPixmap(QPixmap(":/icon/denglu.png"));
    ulabel->setScaledContents(true);

    uline = new QLineEdit(this);
    uline->resize(300,50);
    uline->move(300,320);
    uline->setPlaceholderText("输入账号");//设置占位

    plabel = new QLabel(this);
    plabel->move(200,400);
    plabel->resize(50,50);
    plabel->setPixmap(QPixmap(":/icon/denglumima.png"));
    plabel->setScaledContents(true);

    pline = new QLineEdit(this);
    pline->resize(300,50);
    pline->move(300,400);
    pline->setPlaceholderText("输入密码");
    pline->setEchoMode(QLineEdit::Password);//设置为密码模式

    loginbutton = new QPushButton(QIcon(":/icon/denglu_1.png"),
                                  "登录",this);
    loginbutton->resize(100,30);
    loginbutton->move(280,500);
    connect(loginbutton,&QPushButton::clicked,this,&Widget::login_slot);//触发登录点击事件执行槽函数

    cancelbutton = new QPushButton(QIcon(":/icon/quxiao.png"),
                                   "取消",this);
    cancelbutton->resize(100,30);
    cancelbutton->move(420,500);
    connect(cancelbutton,SIGNAL(clicked()),this,SLOT(cancel_slot()));//触发取消点击事件执行槽函数
}

Widget::~Widget()
{
}

void Widget::cancel_slot()
{
    close();
}

void Widget::login_slot()
{
    if(uline->text()=="admin"&&pline->text()=="123456")
    {
        qDebug()<<"登录成功";
        emit to_second();//发射跳转信号
        close();
    }
    else
    {
        qDebug()<<"登录失败";
        uline->clear();
        pline->clear();
    }
}

second.h

#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Second *ui;
};

#endif // SECOND_H

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;
}

 

 2.给默认代码加上注释信息

QT       += core gui
#表示引入qt所需的类库,如核心库、图形化界面库


greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#如果超过4.0版本,系统会自动加上widgets库


CONFIG += c++11
#该版本的qt支持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 += \
    main.cpp \
    mywnd.cpp
#管理头文件
HEADERS += \
    mywnd.h
#管理ui文件
FORMS += \
    mywnd.ui


# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MYWND_H
#define MYWND_H     //防止文件重复包含


#include <QWidget>  //所需头文件
#include<QCheckBox>


QT_BEGIN_NAMESPACE
namespace Ui { class MyWnd; }            //将ui界面对应的头文件中的命名空间进行声明
QT_END_NAMESPACE


//自定义类名
class MyWnd : public QWidget        //自定义的类,继承自QWidget
{
    Q_OBJECT              //信号与槽的元对象,后期讲


public:
    MyWnd(QWidget *parent = nullptr);       //构造函数的声明
    ~MyWnd();                          //析构函数


private:
    Ui::MyWnd *ui;           //使用ui界面对应头文件中的命名空间中的类定义的指针
                              //后期,如果想要使用ui界面中拖拽出来的组件,可以通过该指针找到
    
    QCheckBox *checkBox;          //自己类中定义的组件,后期找到该组件需要使用this指针找到
};
#endif // MYWND_H
#include "mywnd.h"              //引入自定义的头文件
#include "ui_mywnd.h"             //引入ui界面的头文件


//构造函数的定义
MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)         //调用父类的有参构造
    , ui(new Ui::MyWnd)       //构造出ui界面拖拽的成员,并且讲地址赋值给ui指针
{
    ui->setupUi(this);      //调用设置界面函数,给ui界面上的组件申请空间
}


//析构函数的定义
MyWnd::~MyWnd()
{
    delete ui;       //释放ui界面上的组件空间
}
#include "mywnd.h"        //将自定义的头文件导入

#include <QApplication>    //引入应用程序的头文件

int main(int argc, char *argv[])    //主函数
{
    QApplication a(argc, argv);    //使用应用程序类,实例化一个类对象,调用有参构造

    //使用自定义的类实例化的对象(栈区)
    MyWnd w;         //调用无参够函数,实例化一个界面,该界面没有父组件,独立存在,别的组件依附于该界面而存在
    w.show();        //将图形化界面展示出来

//    int  b = a.exec();

//    printf("b = %d\n", b);


    return a.exec();               //阻塞等待界面的相关相应工作:用户再界面上的操作、信号与槽、事件处理
}

3.思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值