Linux 学习记录46(QT篇)

本文目录
- Linux 学习记录46(QT篇)
- 一、建立QT项目工程
- 二、
- 1.
- 2.
 
- 三、自动生成的文件介绍
- 1. tempprj.pro
- 2. mainwindow.h
- 3. mainwindow.cpp
- 4. main.cpp
- 5. mainwindow.ui
 
- 四、常用类的介绍
- 1. 信息调试类
- (1. qDebug
- (2. 输出当前界面尺寸
- (3. 设置当前界面尺寸
- (4. 设置当前窗口界面
 
- 1. 按钮类
- 1. 类
 
- 思维导图
- 练习
- 1. 自定义登录页面
 
一、建立QT项目工程




二、
1.
2.
三、自动生成的文件介绍

1. tempprj.pro
.pro是一个工程管理文件,依据这个文件来生成makefile文件来编译源文件
#添加相关的类库,例如进程线程库、网络编程库、数据库等待
QT  += core gui #默认提供两个类库: core包含了核心库,io相关操作等 gui库提供了图形化相关的类库
#如果qt版本超过4.0,就会多加一个widgets类库,大部分组件全部来自于该库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tempprj
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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++11以后的C++语法
CONFIG += c++11
#管理源文件
SOURCES += \
        main.cpp \
        mainwindow.cpp
#管理头文件
HEADERS += \
        mainwindow.h
#管理ui文件
FORMS += \
        mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2. mainwindow.h
#ifndef MAINWINDOW_H//防止文件重复包含
#define MAINWINDOW_H
#include <QMainWindow>//由于类中使用的QMainwindow,所以引入的头文件
//该命名空间是在ui文件对应的头文件中定义的,只是在此处进行声明
namespace Ui {
class MainWindow;
}
//自定义的类,界面类,该类继承了QMainwindow
class MainWindow : public QMainWindow
{
    Q_OBJECT//信号与槽的元对象
public:
    explicit MainWindow(QWidget *parent = nullptr);//构造函数的声明 指定该组件的父组件的
    ~MainWindow();//析构函数的声明
private:
	//ui界面类中的Mywnd实例化出来的对象指针,可以使用该指针,在后期找到ui界面上拖拽出来的组件
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
3. mainwindow.cpp
#include "mainwindow.h"//自定义头文件的引入
#include "ui_mainwindow.h"//ui界面对应头文件的引入
//构造函数的定义
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
//初始化列表,在类的初始化列表中必须显性调用父类的有参构造完成对继承下来成员的初始化工作
    ui(new Ui::MainWindow)//给自己的指针成员实例化空间
{
    ui->setupUi(this);//给ui界面上的组件申请空间,并初始化到ui界面上
}
//析构函数的定义
MainWindow::~MainWindow()
{
    delete ui;//释放ui指针空间
}
4. main.cpp
#include "mainwindow.h"//引用自定义头文件
#include <QApplication>//引用应用程序头文件
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//使用应用程序实例化了一个对象a
    MainWindow w;//使用自定义的类实例化一个对象
    w.show();//将界面展示
    return a.exec();//阻塞等待对面上的操作,不然程序结束
    //可以等待:用户操作组件,信号发送,相关的事件触发
}
5. mainwindow.ui
图形化界面操作
 
四、常用类的介绍
接下来的程序编写基本上都在以下界面下
 
1. 信息调试类
需要导入 #include <QDebug> #include <QIcon>
(1. qDebug
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug("hello word\r\n");
    qDebug("%s","hello word\r\n");
}
输出>>
(2. 输出当前界面尺寸
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug()<<this->size();//当前界面尺寸
    qDebug()<<this->frameSize();//组件实际大小不包括边框
    qDebug()<<this->rect().size();//当前界面所占用矩形框的尺寸
}
输出>>
(3. 设置当前界面尺寸
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //重新设置当前界面尺寸
    this->resize(800,600);
    //使用重载函数重新设置当前界面尺寸,参数是一个匿名对象
    this->resize(QSize(400,300));
    qDebug()<<this->size();//当前界面尺寸
}
输出>>
(4. 设置当前窗口界面
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置最大尺寸
    this->setMaximumSize(400,300);
    //设置最小尺寸
    this->setMinimumSize(300,200);
    //设置固定尺寸
//    this->setFixedSize(128,64);
    //重新设置当前界面尺寸
    this->resize(800,600);
    //设置窗口类型为纯净窗口
//    this->setWindowFlag(Qt::FramelessWindowHint);
    //设置窗口标题
    this->setWindowTitle("proj");//当前界面尺寸
    //设置窗口图标
    this->setWindowIcon(QIcon("C:\\Users\\Hao\\Desktop\\buf\\link.jpg"));//设置窗口图标
    //使用样式表更改颜色
    this->setStyleSheet("background-color:grey");
    //设置串口透明度
    this->setWindowOpacity(0.5);
    //窗口位置
    this->move(30,500);
    //获取窗口标题
    qDebug()<<this->windowTitle();//当前界面尺寸
    qDebug()<<this->size();//当前界面尺寸
}
输出>>
1. 按钮类
需要添加头文件 #include <qpushbutton.h>
1. 类
需要添加头文件 
思维导图
练习
1. 自定义登录页面
#include "mainwindow.h"
#include "public.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QFont UPfont;
    /************窗口设置*************/
    //设置固定尺寸
    this->setFixedSize(600,360);
    this->setWindowTitle("Qt window");//当前界面尺寸
    //设置窗口图标
    this->setWindowIcon(QIcon("C:\\Users\\Hao\\Desktop\\buf\\Qt_w.png"));//设置窗口图标
    this->setStyleSheet("background-color:#F5F5F5");
    /************标签设置*************/
    QLabel *theme1 = new QLabel(this);//主题
    theme1->resize(600,180);
    theme1->setPixmap(QPixmap("C:\\Users\\Hao\\Desktop\\buf\\Qt.jpg"));
//    QLabel *theme2 = new QLabel(this);//Pass word
//    theme2->resize(600,180);
    theme2->setPixmap(QPixmap("C:\\Users\\Hao\\Desktop\\buf\\green.png"));
//    theme2->move(0,180);
    lab4->setScaledContents(true);
    QLabel *lab2 = new QLabel(this);//UID
    lab2->resize(40,30);
    lab2->move(210,200);
    lab2->setPixmap(QPixmap("C:\\Users\\Hao\\Desktop\\buf\\IDcard.png"));
    lab2->setScaledContents(true);
    QLabel *lab3 = new QLabel(this);//Pass word
    lab3->resize(30,30);
    lab3->move(215,240);
    lab3->setPixmap(QPixmap("C:\\Users\\Hao\\Desktop\\buf\\OnePassword.png"));
    lab3->setScaledContents(true);
    /************按键设置*************/
    UPfont.setPointSize(15);
    UPfont.setFamily("宋体");
    QPushButton *log_on = new QPushButton(QIcon("C:\\Users\\Hao\\Desktop\\buf\\登录.png"),"登录", this);
    QPushButton *cancel = new QPushButton(QIcon("C:\\Users\\Hao\\Desktop\\buf\\取消.png"),"取消", this);
    log_on->resize(80,40);
    log_on->move(200,280);
    log_on->setStyleSheet("border:none;border-radius:10px;background-color:#87CEFA;color:#fff");
    cancel->resize(80,40);
    cancel->move(320,280);
    cancel->setStyleSheet("border:none;border-radius:10px;background-color:#87CEFA;color:#fff");
    /**********行编辑器设置*************/
    UPfont.setPointSize(12);
    UPfont.setFamily("宋体");
    QLineEdit *psrd = new QLineEdit(this);
    //重新设置大小
    psrd->resize(130,30);
    //移动
    psrd->move(260,240);
    // 设置样式表
    psrd->setStyleSheet("border:none;border-radius:10px;background-color:#696969;color:#fff");
    //设置占位文本
    psrd->setPlaceholderText("Pass word");
    psrd->setEchoMode(QLineEdit::Password);
    psrd->setFont(UPfont);
    QLineEdit *UID = new QLineEdit(this);
    //重新设置大小
    UID->resize(130,30);
    //移动
    UID->move(260,200);
    // 设置样式表
    UID->setStyleSheet("border:none;border-radius:10px;background-color:#696969;color:#fff");
    //设置占位文本
    UID->setPlaceholderText("UID");
    UID->setFont(UPfont);
}
MainWindow::~MainWindow()
{
}
效果>>
























