QT编程day 1

1.为新的qt工程打上注释

------------------------------------------------------------------------
.pro文件:
------------------------------------------------------------------------

QT       += core gui    #添加库文件 core为核心库文件 gui为图形化库文件

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#在4.0版本后会自动添加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

#c++文件管理
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


------------------------------------------------------------------------
.h文件:
------------------------------------------------------------------------
#ifndef WIDGET_H    //防止内容重复包含
#define WIDGET_H

#include   //QWidget类所在头文件

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }  //在命名空间中实例化一个名字为Widget的类
QT_END_NAMESPACE

class Widget : public QWidget   //实例化一个名字为Widget的类,继承于QWidget(与命名空间中的Widget不是同一个类)
{
    Q_OBJECT    //信号与槽

public:
    Widget(QWidget *parent = nullptr);  //有参构造函数,有一个默认值
    ~Widget();  //析构函数

private:
    Ui::Widget *ui; //实例化一个命名空间中的Widget类的指针,可以访问ui界面上的组件
};
#endif // WIDGET_H

------------------------------------------------------------------------
main.cpp文件:
------------------------------------------------------------------------
#include "widget.h" //包含当前文件夹中的图形化头文件

#include  //包含应用程序头文件

int main(int argc, char *argv[])
{
    QApplication a(argc, argv); //实例化一个应用程序,为有参构造
    Widget w;   //在栈区实例化一个Widget类的w
    w.show();   //调用Widget中QWidget中的show函数,显示ui界面的内容
    return a.exec();    //等待程序事件
}

------------------------------------------------------------------------
widget.cpp文件:
------------------------------------------------------------------------
#include "widget.h" //调用自己的头文件
#include "ui_widget.h"  //调用图形化头文件

Widget::Widget(QWidget *parent) //定义widget的有参构造函数
    : QWidget(parent)   //调用父类的有参构造函数,把参数传给父类
    , ui(new Ui::Widget)    //为自己类中的ui指针开辟空间
{
    ui->setupUi(this);  //为ui界面拖拽的组件实例化对象
}

Widget::~Widget()   //定义widget的析构函数
{
    delete ui;  //释放ui指针指向的空间
}




2.创建一个简易的登录界面(不用实现功能)

QT编程day 1_第1张图片

------------------------------------------------------------------------
.pro文件:
------------------------------------------------------------------------
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 += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

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

#include 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
};
#endif // WIDGET_H
------------------------------------------------------------------------
main.cpp文件:
------------------------------------------------------------------------
#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
------------------------------------------------------------------------
widget.cpp文件:
------------------------------------------------------------------------
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //---------------------------------窗口设置---------------------------------
    //设置窗口为固定大小
    this->setFixedSize(540,410);
    //设置窗口icon
    this->setWindowIcon(QIcon("D:/B.work/6qt/day1/icon/wodepeizhenshi.png"));
    //设置窗口名
    this->setWindowTitle("鹏哥快聊");

    //---------------------------------标签设置---------------------------------
    //=====创建界面标签=====
    QLabel *main_lab = new QLabel(this);
    //修改标签大小
    main_lab->resize(540,200);
    //在标签中插入图片
    main_lab->setPixmap(QPixmap("D:/B.work/6qt/day1/icon/logo.png"));
    //设置为自适应
    main_lab->setScaledContents(1);

    //=====创建账号标签=====
    QLabel *account_lab = new QLabel(this);
    //修改标签大小
    account_lab->resize(50,40);
    //在标签中插入图片
    account_lab->setPixmap(QPixmap("D:/B.work/6qt/day1/icon/userName.jpg"));
    //移动到合适位置
    account_lab->move(90,220);
    //设置为自适应
    account_lab->setScaledContents(1);

    //=====创建密码标签=====
    QLabel *pwd_lab = new QLabel(this);
    //修改标签大小
    pwd_lab->resize(account_lab->size());
    //在标签中插入图片
    pwd_lab->setPixmap(QPixmap("D:/B.work/6qt/day1/icon/passwd.jpg"));
    //移动到合适位置
    pwd_lab->move(account_lab->x(),account_lab->y()+80);
    //设置为自适应
    pwd_lab->setScaledContents(1);


    //---------------------------------按钮设置---------------------------------
    //=====创建登录按钮=====
    QPushButton *login_btn = new QPushButton(this);
    //设置按钮大小
    login_btn->resize(100,40);
    //在按钮上插入图片
    login_btn->setIcon(QIcon("D:/B.work/6qt/day1/icon/login.png"));
    //在按钮上输入文本
    login_btn->setText("登录");
    //移动到合适位置
    login_btn->move(270,360);
    //=====创建取消按钮=====
    QPushButton *cancel_btn = new QPushButton(this);
    //设置按钮大小
    cancel_btn->resize(100,40);
    //在按钮上插入图片
    cancel_btn->setIcon(QIcon("D:/B.work/6qt/day1/icon/cancel.png"));
    //在按钮上输入文本
    cancel_btn->setText("取消");
    //移动到合适位置
    cancel_btn->move(login_btn->x()+120,login_btn->y());

    //---------------------------------输入设置---------------------------------
    //=====创建账号输入=====
    QLineEdit *account_edit = new QLineEdit(this);
    //设置占位文本
    account_edit->setPlaceholderText("QQ号码/手机/邮箱");
    //设置大小
    account_edit->resize(200,40);
    //移动到适合位置
    account_edit->move(account_lab->x()+100,account_lab->y());

    //=====创建密码输入=====
    QLineEdit *pwd_edit = new QLineEdit(this);
    //设置占位文本
    pwd_edit->setPlaceholderText("密码");
    //设置大小
    pwd_edit->resize(account_edit->size());
    //移动到合适位置
    pwd_edit->move(pwd_lab->x()+100,pwd_lab->y());
    //设置回文显示模式
    pwd_edit->setEchoMode(QLineEdit::Password);

}

Widget::~Widget()
{
}

你可能感兴趣的:(qt,c++)