QT day01

自定义实现登录界面:

#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>

Widget::Widget(QWidget *parent) //定义有参构造函数
    : QWidget(parent)
    , ui(new Ui::Widget)        //给自己类中的指针实例化空间
{
    //ui->setupUi(this);        //将ui界面上拖拽的组件展示到this界面上
    //按钮组件
    this->setWindowTitle("QQ登录");
    this->resize(520,520);
    this->setStyleSheet("background-color:lightblue");
    QPushButton *btn1 = new QPushButton;
    btn1->setParent(this);
    btn1->setText("登录");
    btn1->resize(60,40);
    btn1->move(150,300);
    btn1->setStyleSheet("background-color:lightgreen");
    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("注册");
    btn2->resize(btn1->size());
    btn2->move(300,300);
    btn2->setStyleSheet("background-color:lightgreen");
    //行编辑器
    QLineEdit *edit1 = new QLineEdit;
    edit1->setParent(this);
    edit1->resize(250,30);
    edit1->move(150,150);
    edit1->setPlaceholderText("账号");
    edit1->setEchoMode(QLineEdit::Password);
    QLineEdit *edit2 = new QLineEdit(this);
    edit2->resize(edit1->size());
    edit2->move(150,200);
    edit2->setPlaceholderText("密码");
    edit2->setEchoMode(QLineEdit::Password);
    //创建标签
    QLabel *lab1 = new QLabel;
    lab1->setParent(this);
    lab1->setText("账号:");
    lab1->move(edit1->x()-60,edit1->y());
    QLabel *lab2 = new QLabel("密码:",this);
    lab2->move(edit2->x()-60,edit2->y());

}

Widget::~Widget()               //定义析构函数
{
    delete ui;                  //释放ui界面组件的空间
}

效果图:

02Demo.pro

#QT:引入的类库  core:核心库  gui:图形化界面库
QT       += core gui
#当QT超过版本4时会自动加上widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#支持C++11新特性
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 += \
    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

widgets.h

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

#include <QWidget>
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();    //使用应用程序类对象并调用成员函数保证页面不被关闭
}

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界面组件的空间
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值