Qt零散知识点

Qt零散知识点

Qt优点

  • 跨平台
  • 接口简单,易上手
  • 一定程度上简化了内存的回收

Qt创建新项目

第一个窗口类默认的三个基类

  • QWidget
  • QMainWindow
  • QDialog

其中QWidget是QMainWindow和QDialog的基类

一个Qt项目默认创建的文件

  • main.cpp 入口函数
  • pro文件:工程文件

pro文件介绍

powershell 复制代码
QT       += core gui                                # QT包含的模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets     # 4版本以上,加入widget模块
TARGET = AppDemo                                    # 目标,生成的可执行文件
TEMPLATE = app                                      # 模板 Application 应用程序

SOURCES += \                                       # 源文件
    main.cpp \
    MainWindow.cpp

HEADERS += \                                        # 头文件
    MainWindow.h

FORMS += \                                          # UI文件
    MainWindow.ui

main函数(入口函数)介绍

cpp 复制代码
#include "MainWindow.h"

#include <QApplication>             // 应用程序类

/**
 *@brief argc:命令行参数的数量 argv:命令行参数数组
 */
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);     // 应用程序对象,Qt中有且只有一个应用程序对象

    MainWindow w;                   // 创建一个自定义窗口对象
    w.show();                       // 窗口对象默认是不会弹出的,需要调用show函数进行显示

    return a.exec();                // a.exec()进入消息循环机制,阻塞代码
}

宏:Q_OBJECT

提供Qt中信号和槽的机制

Qt Creator

快捷键介绍

运行:CTRL + R

编译:CTRL + B

帮助文档:F1

注释:CTRL + /

查找:CTRL + F

自动对齐:CTRL + I

同名.h和.cpp之间切换:CTRL +F4

将当前代码复制到下一行:CTRL + ALT + ↓

帮助文档位置

D:\Qt\Qt5.13.2\5.13.2\mingw73_64\bin

设置文件编码格式

有时候可能会遇到中文字符显示乱码,此时我们可以在Qt Creator的:
工具->选项->文本编辑器->行为 里面设置文件的编码格式:UTF-8

Qt控件

QPushButton

使用Qt的帮助手册

cpp 复制代码
QPushButton Class
The QPushButton widget provides a command button. More...
Header:
#include <QPushButton> 			// 包含头文件
qmake:
QT += widgets					// 所属模块,如果pro文件里面没有,需要手动加上去
Inherits:
QAbstractButton
Inherited By:
QCommandLinkButton
List of all members, including inherited members 

按钮的一些基本操作

cpp 复制代码
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPushButton* pBtn = new QPushButton();

    // 如果是show函数,默认采用的是顶层方式弹出窗口
    // pBtn->show();

    // 预期想让按钮依附于父窗口中,需要设置所属的父窗口对象
    pBtn->setParent(this);
    pBtn->setText("孙悟空");

    // 移动按钮
    pBtn->move(100, 100);

    // 设置按钮的大小
    pBtn->resize(120, 40);

    // 设置主窗口大小
    this->resize(800, 600);

    // 设置窗口标题
    this->setWindowTitle("First App");

    // 设置窗口固定大小(进行此设置后窗口无法拉伸,收缩)
    this->setFixedSize(800, 600);
}

Qt内存模型

编写一个自定义按钮类继承自QPushButton,然后在其析构函数内输出打印消息,将其配置在父窗口MainWindow上面,同时MainWindow析构函数也输出打印消息。

代码:

cpp 复制代码
// NewButton.h
#ifndef NEWBUTTON_H
#define NEWBUTTON_H

#include <QPushButton>

class NewButton : public QPushButton
{
    Q_OBJECT
public:
    explicit NewButton(QWidget *parent = nullptr);
    ~NewButton();

signals:

public slots:
};

#endif // NEWBUTTON_H

// NewButton.cpp
#include "NewButton.h"
#include <QDebug>

NewButton::NewButton(QWidget *parent) : QPushButton(parent)
{

}

NewButton::~NewButton()
{
    qDebug() << "New Button Destruction Call";
}

// MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

// MainWindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPushButton>
#include "NewButton.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPushButton* pBtn = new QPushButton();

    // 如果是show函数,默认采用的是顶层方式弹出窗口
    // pBtn->show();

    // 预期想让按钮依附于父窗口中,需要设置所属的父窗口对象
    pBtn->setParent(this);
    pBtn->setText("孙悟空");

    // 移动按钮
    pBtn->move(100, 100);

    // 设置按钮的大小
    pBtn->resize(120, 40);

    // 设置主窗口大小
    this->resize(800, 600);

    // 设置窗口标题
    this->setWindowTitle("First App");

    // 设置窗口固定大小(进行此设置后窗口无法拉伸,收缩)
    this->setFixedSize(800, 600);

    NewButton* pBtnA = new NewButton(this);
}

MainWindow::~MainWindow()
{
    qDebug() << "MainWindow Destruction Call";
    delete ui;
}

输出:

cpp 复制代码
MainWindow Destruction Call
New Button Destruction Call
14:13:53: Debugging has finished
相关推荐
小声读源码7 分钟前
【技巧】使用UV创建python项目的开发环境
开发语言·python·uv·dify
yxc_inspire14 分钟前
基于Qt的app开发第七天
开发语言·c++·qt·app
zm-v-1593043398616 分钟前
解锁遥感数据密码:DeepSeek、Python 与 OpenCV 的协同之力
开发语言·python·opencv
周Echo周28 分钟前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
明天更新33 分钟前
Java处理压缩文件的两种方式!!!!
java·开发语言·7-zip
老胖闲聊39 分钟前
C# 注册表操作类
开发语言·c#
勘察加熊人41 分钟前
Python+Streamlit实现登录页
开发语言·python
理智的煎蛋1 小时前
keepalived+lvs
java·开发语言·集成测试·可用性测试
Bl_a_ck1 小时前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
编程有点难2 小时前
Python训练打卡Day23
开发语言·python