QT基础 制作简单登录界面

作业:

1、创建一个新项目,将默认提供的程序都注释上意义

01zy.pro代码

QT       += core gui
# QT表示要引入的类库  core:核心库例如IO操作在该库中   gui:图形化界面库
# 如果要使用其他类库中的相关函数,则需要加对于的类库后,才能使用

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# QT超过版本4时,会自动加上 widgets

CONFIG += c++11
# 支持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 \
    widget.cpp

# 管理头文件
HEADERS += \
    widget.h

# 管理所有ui文件
FORMS += \
    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

widget.h代码

#ifndef WIDGET_H
#define WIDGET_H
//防止头文件重复包含

#include <QWidget>

//ui_mywnd.h中的命名空间的声明
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }  //将其他文件中的命名空间进行声明
QT_END_NAMESPACE

//自定义的类的声明,公共继承自QWidget:QWidget中封装了有关图形化界面的相关操作的具体实现
//由于继承的是系统提供的类,那么自定义的类中即使没有写任何东西,其类中也有很多成员了
class Widget : public QWidget
{
    Q_OBJECT    //信号与槽的元对象,直接写即可,没有该宏,就不能使用信号与槽

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

private:
    Ui::Widget *ui;  //成员属性,指针
};
#endif // WIDGET_H

main.cpp代码

#include "widget.h"
//文件包含,自定义的头文件,该头文件中包含了图形化界面类

#include <QApplication>
//包含应用程序的头文件

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);  //使用应用程序类实例化对象,调用有参构造
    Widget w;   //使用自定义的类调用无参构造在栈区构造一个界面对象
    w.show();   //调用对象的成员函数,将界面展示出来
    return a.exec();
    //a.exec():使用应用程序类对象,调用应用程序的成员函数,保证界面不被关闭,轮询等待界面上的时间发生
    //等待用户操作界面上的组件
    //等待界面上的信号与槽的响应
    //等待事件处理机制的实现
}

widget.cpp代码

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

//构造函数的定义
Widget::Widget(QWidget *parent)
    : QWidget(parent)    //在子类的初始化列表中显式调用父类的有参构造,来完成对子类从父类中继承下来成员的初始化
    , ui(new Ui::Widget)     //给自己的类中的指针成员实例化空间
{
    ui->setupUi(this);   //将ui界面上拖拽的组件展示到this界面上
}

//析构寒湖是的定义
Widget::~Widget()
{
    delete ui;   //释放ui界面申请的组件空间
}

2、使用代码的形式实现登录框

widget.h代码

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
};
#endif // WIDGET_H

widget.cpp代码

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1、设置窗口
    this->setFixedSize(600,400);    //设置界面尺寸
    this->setWindowTitle("QQ");    //设置窗口标题
    //设置图标,使用匿名对象完成
    this->setWindowIcon(QIcon("C:/Users/Administrator/Desktop/icon/QQ.png"));

    //设置按钮
    //使用无参构造在堆区申请一个按钮
    QPushButton *btn1 = new QPushButton;
    btn1->setParent(this);  //将当前界面设置成组件的父组件
    btn1->setText("登录");    //设置按钮文本内容
    btn1->resize(80,40);    //重新设置按钮尺寸
    btn1->move(180,320);    //移动当前组件位置
    //设置样式表
    btn1->setStyleSheet("color:white;background-color:skyblue;border-radius:10px;");

    //使用有参构造,构造一个按钮,实例化对象时,顺便给定父组件
    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("注册");
    //使用btn1的尺寸设置当前按钮的尺寸
    btn2->resize(btn1->size());
    //使用btn1的位置,确定btn2的位置
    btn2->move(btn1->x()+btn1->width()+10,btn1->y());
    //设置样式表
    btn2->setStyleSheet("color:white;background-color:skyblue;border-radius:10px;");

    //3、设置行编辑器
    //使用有参构造一个行编辑器
    QLineEdit *edit1 = new QLineEdit(this);
    edit1->setWindowIcon(QIcon("C:/Users/Administrator/Desktop/icon/zhh.png"));
    edit1->resize(250,40);  //重置尺寸
    edit1->move(150,160);
    edit1->setAlignment(Qt::AlignCenter);   //设置文本对齐方式

    edit1->setPlaceholderText("请输入QQ账号");    //设置占位文本

    //使用有参构造一个行编辑器
    QLineEdit *edit2 = new QLineEdit(this);
    edit2->setWindowIcon(QIcon("C:/Users/Administrator/Desktop/icon/pwd.png"));
    edit2->resize(edit1->size());   //重置尺寸
    edit2->move(150,220);  //移动位置
    edit2->setPlaceholderText("请输入QQ密码");    //设置占位文本
    edit2->setAlignment(Qt::AlignCenter);   //设置文本对齐方式
    edit2->setEchoMode(QLineEdit::Password);    //设置回显模式

    QLabel *lab1 = new QLabel(this);
    lab1->setText("账号");
    lab1->move(100,230);

    QLabel *lab2 = new QLabel(this);
    lab2->setText("密码");
    lab2->move(100,170);

    QLabel *lab3 = new QLabel(this);
    //lab3->setStyleSheet("background-color:skyblue;");
    lab3->move(0,0);
    lab3->setFixedSize(600,140);
    //给标签设置动图
    //创建一个movie对象
    QMovie *movie = new QMovie("C:/Users/Administrator/Desktop/icon/bj.gif");
    //将动图对象放入到标签中
    lab3->setMovie(movie);
    //让动图动起来
    movie->start();
    //让标签内容自适应大小
    lab3->setScaledContents(true);

    //给标签设置静态图
    QLabel *lab4 = new QLabel(this);
    lab4->resize(60,60);
    lab4->move(260,80);
    lab4->setPixmap(QPixmap("C:/Users/Administrator/Desktop/icon/tx.jpg"));
    lab4->setScaledContents(true);
}

Widget::~Widget()
{
}

运行结果:

知识梳理:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值