当前位置: 首页 > article >正文

Qt开发第一讲

一、Qt项目里面有什么?

对各个文件的解释:

Empty.pro文件

QT       += core gui   # 要引入的Qt模块,后面学习到一些内容的时候可能会修改这里
#这个文件相当于Linux里面的makefile文件。makefile其实是一个非常古老的技术了。
#qmake搭配.pro起到作用和makefile是类似的。
#Qt creator 把这个过程中编译的细节都封装好了,不需要过多的关注,只需要点击运行按钮,就直接编译运行了
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 \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui
#描述了当前项目中,参与构建的文件都有啥(编译器要编译哪些文件)
#这个地方不需要手动修改,Qt Creator帮我们自动维护好

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

# 项目的工程文件,也是qmake构建时候的重要依据。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//要想使用这个类,就需要包含对应的头文件。Qt
//的设定,使用Qt中内置的类,包含的头文件的名字就是和类名一致的
#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
//创建项目时,选择的父类。
class MainWindow : public QMainWindow
{
    Q_OBJECT  //是一个Qt内置的宏,宏本质上是文本替换
    //Qt中有一个非常核心的机制,信号和槽,如果某个类想使用信号和槽,就需要引入
    //Q_OBJECT这个宏

public:
    //Qt中引入了对象树机制,创建的Qt的对象,就可以把这个对象给挂到对象树上,往树上挂的时候
    //就需要指定父节点。
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui; //和form file密切相关。
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"

#include <QApplication>
//入口函数,命令行参数的个数,argv命令行参数。
int main(int argc, char *argv[])
{
    //编写一个Qt的图形化界面程序,一定需要有QApplication对象!
    QApplication a(argc, argv);
    //创建一个控件对象,并显示出来。它的父类是QWidget,都是QWidget提供的。
    MainWindow w;
    //show方法让空间显示出来
    w.show();
    //让空间隐藏
    //w.hide();
    return a.exec(); //让程序执行起来。在linux中我们学过exec*系列函数,把可执行文件中的代码和数据,替换到当前进程中
    //但在这里,只是名字恰好一样
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
//构造和析构的实现
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow) //把form file生成的界面和当mainwindow关联起来
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>  //xml格式的文件,和html相似。
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

二、Qt Hello World 程序

两种方式实现hello world

1、通过图形化的方式,在界面上创建出一个空间,显示hello world

Qt Designer右上角,通过树形结构,显示出了当前界面上都有哪些控件。

2、通过纯代码的方式,通过编写代码,在界面上创建控件,显示hello world

#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QLabel* label = new QLabel(this);
    label->setText("hello world");
}

Widget::~Widget()
{
    delete ui;
}


http://www.kler.cn/news/326410.html

相关文章:

  • ip池子的大小与什么相关?
  • echarts 导出pdf空白原因
  • Spring系统学习(五)——Spring数据库编程
  • YOLOv8改进 | 主干改进篇,华为的轻量化架构GhostNetV2改进特征提取网络
  • 前端项目依赖包中的依赖包漏洞解决方案
  • springboot实战学习(10)(ThreadLoacl优化获取用户详细信息接口)(重写拦截器afterCompletion()方法)
  • 解决VRM格式模型在Unity中运行出现头发乱飞等问题
  • Java | Leetcode Java题解之第443题压缩字符串
  • ECCV 2024 | 融合跨模态先验与扩散模型,快手处理大模型让视频画面更清晰!
  • 数据中心解决方案
  • 手写体识别毕设——人工智能和深度学习技术的快速发展
  • 前端安装 lerna
  • Knots_3D 9.3.0 一款教你绑绳结的手机应用
  • 高标准农田灌区信息化:为农业可持续发展注入新动力
  • 开源模型应用落地-qwen2.5-7b-instruct-LoRA微调-LLaMA-Factory-单机单卡-V100(十八)
  • 抽象工厂模式和工厂模式的区别
  • 使用 IntelliJ IDEA 连接到达梦数据库(DM)
  • 大厂面试真题- RPC通讯协议如何保证数据完整性
  • 谷歌网站收录查询,怎么查看网站在谷歌的收录情况
  • 1. AOSP源码导入到AndroidStudio
  • JWT 令牌生成报错
  • Linux(含麒麟操作系统)如何实现多显示器屏幕采集录制
  • 8.代码风格调试%结课竞赛
  • Ubuntu篇——Ubuntu20.04备份成ISO镜像文件并安装到其他电脑上(完整步骤)
  • 安全无忧,简单便捷:打造财富通开锁小程序
  • 根据现有html里的元素上面动态创建el-tooltip组件并显示的几种方式
  • js中数组操作filter()、some()、every()等函数
  • 五,MyBatis-Plus 当中的 “ActiveRecord模式”和“SimpleQuery工具类”(详细实操)
  • 360° 镜头检测铝件内壁划痕与杂质:保障铝件内孔制造质量的精准方案
  • Flutter中使用FFI的方式链接C/C++的so库(harmonyos)