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

Qt开发第一讲

一、Qt项目里面有什么?

对各个文件的解释:

Empty.pro文件

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

相关文章:

  • 详细指南:如何有效解决Windows系统中msvcp140.dll丢失的解决方法
  • 【RabbitMQ】幂等性、顺序性
  • FFmpeg源码:avio_skip函数分析
  • Llama 3.1 技术研究报告-6
  • 更新日志-Python OS
  • Chrome浏览器的C++内存管理技术揭秘
  • Redis --- redis事务和分布式事务锁
  • SQL,将多对多的关联记录按行输出
  • 【SQL】筛选字符串与正则表达式
  • 【Redis入门到精通五】Java如何像使用MySQL一样使用Redis(jedis安装及使用)
  • 【 微信机器人+ AI 搭建】
  • VGG16网络介绍及代码撰写详解(总结1)
  • 多个excel表数据比对操作
  • golang学习笔记32——哪些是用golang实现的热门框架和工具
  • ZYNQ:开发环境搭建
  • 一步一步丰富生成式语言模型系统
  • Python中元组的常用方法
  • 新版本Android Studio如何新建Java code工程
  • 2024年世界职业院校技能大赛:全面升级的国际化职业技能竞赛
  • 前端vue相关常见面试题,包含MVVM、双向绑定原理、性能优化、vue2和vue3性能对比等
  • 生信初学者教程(十二):数据汇总
  • 常用大语言模型简单介绍
  • 云计算Openstack
  • ClickHouse复杂查询单表亿级数据案例(可导出Excel)
  • ST-GCN模型实现花样滑冰动作分类
  • 计算机网络基础--认识协议
  • 基本控制结构2
  • php 平滑重启 kill -SIGUSR2 <PID> pgrep命令查看进程号
  • 实时美颜功能技术揭秘:视频美颜SDK与API的技术剖析
  • word2vector训练代码详解