1> 创建一个新项目,将默认提供的程序都注释上意义
01Demo.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 \
mywindow.cpp
# 管理头文件
HEADERS += \
mywindow.h
# 管理所有ui文件
FORMS += \
mywindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
mywindow.h
#ifndef MYWINDOW_H
#define MYWINDOW_H
//防止头文件重复包含
#include <QWidget>
//ui_mywnd.h中的命名空间的声明
QT_BEGIN_NAMESPACE
namespace Ui { class MyWindow; } //将其他文件中的命名空间进行声明
QT_END_NAMESPACE
//自定义的类的声明,公共继承自QWidget:QWidget中封装了有关图形化界面的相关操作的具体实现
//由于继承的是系统提供的类,那么自定义的类中即使没有写任何东西,其类中也有很多成员了
class MyWindow : public QWidget
{
Q_OBJECT //信号与槽的元对象,直接写,没有该宏,就不能使用信号与槽
public:
MyWindow(QWidget *parent = nullptr); //构造函数的声明
~MyWindow(); //析构函数的声明 虚析构函数
private:
Ui::MyWindow *ui; //成员属性,指针
};
#endif // MYWINDOW_H
mywindow.cpp
#include "mywindow.h"
#include "ui_mywindow.h"
//构造函数的定义
MyWindow::MyWindow(QWidget *parent)
: QWidget(parent) //在子类的初始化列表中显式调用父类的有参构造,来完成对子类从父类中继承下来成员的初始化
, ui(new Ui::MyWindow) //给自己的类中的指针成员实例化空间
{
ui->setupUi(this); //将ui界面上拖拽的组件展示到this界面上
}
//析构寒湖是的定义
MyWindow::~MyWindow()
{
delete ui; //释放ui界面申请的组件空间
}
main.cpp
#include "mywindow.h"
//文件包含,自定义的头文件,该头文件中包含了图形化界面类
#include <QApplication>
//包含应用程序的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //使用应用程序类实例化对象,调用有参构造
MyWindow w; //使用自定义的类调用无参构造在栈区构造一个界面对象
w.show(); //调用对象的成员函数,将界面展示出来
return a.exec();
//a.exec():使用应用程序类对象,调用应用程序的成员函数,保证界面不被关闭,轮询等待界面上的时间发生
//等待用户操作界面上的组件
//等待界面上的信号与槽的响应
//等待事件处理机制的实现
}
2> 使用代码的形式实现登录框
01text.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
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
main.c
#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"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(900,800); //重置界面尺寸
this->setWindowTitle("登录"); //设置界面名称为:登录
this->setWindowIcon(QIcon("C:\\Users\\86183\\Desktop\\icon_dj40cz9xze4\\bianji-nan.png"));
//设置界面图标
QLabel *lab1 = new QLabel(this); //定义文本
lab1->resize(300,200); //设置文本框大小
lab1->move(300,200); //移动文本位置
lab1->setPixmap(QPixmap("C:\\Users\\86183\\OneDrive\\图片\\jiguang.jpg"));
//设置文本为图片
lab1->setScaledContents(true); //设置图片自动缩放
QLabel *lab2 = new QLabel("账号:",this); //定义文本
lab2->resize(50,40); //重置文本框大小
lab2->move(lab1->x(),lab1->y()+lab1->height()+10); //移动文本位置
QLabel *lab3 = new QLabel("密码:",this); //定义文本
lab3->resize(50,40); //重置文本框大小
lab3->move(lab2->x(),lab2->y()+lab2->height()); //移动文本位置
QLineEdit *edit1 = new QLineEdit(this);
edit1->resize(250,30); //重置尺寸
edit1->move(lab2->x()+lab2->width(),lab2->y());
edit1->setStyleSheet("color:black"); //设置字体颜色
edit1->setAlignment(Qt::AlignCenter); //设置文本对齐方式:居中
edit1->setPlaceholderText("输入QQ账号"); //设置占位文本
QLineEdit *edit2 = new QLineEdit(this);
edit2->resize(250,30); //重置尺寸
edit2->move(lab3->x()+lab3->width(),lab3->y());
edit2->setStyleSheet("color:black"); //设置字体颜色
edit2->setAlignment(Qt::AlignCenter); //设置文本对齐方式:居中
edit2->setEchoMode(QLineEdit::Password); //设置密文模式
edit2->setPlaceholderText("输入QQ密码"); //设置占位文本
QPushButton *btn1 = new QPushButton("登录",this); //定义按键
btn1->resize(110,40); //重置按键大小
btn1->move(lab3->x(),lab3->y()+lab3->height()); //移动按键位置
QPushButton *btn2 = new QPushButton("取消",this); //定义按键
btn2->resize(110,40); //重置按键大小
btn2->move(btn1->x()+btn1->width()+80,btn1->y()); //移动按键位置
}
Widget::~Widget()
{
}
结果:
3> 思维导图