【Qt 学习笔记】使用QtCreator创建及运行项目 | 项目初始代码解释

简介: 【Qt 学习笔记】使用QtCreator创建及运行项目 | 项目初始代码解释


  • 关注博主,后期持续更新系列文章
  • 如果有错误感谢请大家批评指出,及时修改
  • 感谢大家点赞👍收藏⭐评论✍

使用QtCreator创建及运行项目 | 项目初始代码解释

文章编号:Qt 学习笔记 / 04



一、新建项目

  1. 打开QtCreator,点击文件,新建项目或项目

  2. 选择项目模板,这里选择Application,然后选择Qt Widgets Application,点击Choose

  3. 编辑文件的名称,选择创建项目保存的项目路径(注意:文件路径不要有中文

  4. 选择qmake,点击下一步

  5. 在Base class中选择Qwidget,点击下一步

  6. 点击下一步

  7. 点击下一步

  8. 点击完成,完成创建一个项目


二、运行项目

  1. 创建项目完成,进入项目即可点击运行。
  2. 运行完成,会弹出一个界面(如下图所示)

三、项目代码解释

1. main.cpp

  • 文件源码
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w; //  Widget是创建项目时。填写生成的类名
    w.show(); // .show()方法让控件显示
    return a.exec();  //.exec() 执行代码
}
  • main.cpp 文件代码图解

2. widget.h

  • 文件源码
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
  • widget.h 文件代码图解

3. widget.cpp

  • 文件源码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}
Widget::~Widget()
{
    delete ui;
}
  • widget.cpp 文件代码图解

4. widget.ui

  • 文件源码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
 </widget>
 <resources/>
 <connections/>
</ui>
  • widget.ui 文件代码图解

5. .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
FORMS += \
    widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
  • .pro 文件代码图解

目录
相关文章
|
存储 文件存储 数据库
【QT项目】QT项目综合练习之简易计数器(QT6+文件存储)
【QT项目】QT项目综合练习之简易计数器(QT6+文件存储)
415 2
|
XML 数据可视化 C语言
001 Qt_从零开始创建项目
本文是Qt专栏的第一篇,介绍了如何创建一个Qt项目。
580 4
CMake自动打包--Qt项目
CMake自动打包--Qt项目
316 0
|
数据安全/隐私保护
【qt】考试系统项目
【qt】考试系统项目
310 0
|
网络协议 容器
【qt】 TCP编程小项目
【qt】 TCP编程小项目
385 0
【qt】平面CAD(计算机辅助设计 )项目 上
【qt】平面CAD(计算机辅助设计 )项目 上
329 0
|
开发工具 C++
qt开发技巧与三个问题点
本文介绍了三个Qt开发中的常见问题及其解决方法,并提供了一些实用的开发技巧。
962 0
|
C++
C++ Qt开发:QUdpSocket网络通信组件
QUdpSocket是Qt网络编程中一个非常有用的组件,它提供了在UDP协议下进行数据发送和接收的能力。通过简单的方法和信号,可以轻松实现基于UDP的网络通信。不过,需要注意的是,UDP协议本身不保证数据的可靠传输,因此在使用QUdpSocket时,可能需要在应用层实现一些机制来保证数据的完整性和顺序,或者选择在适用的场景下使用UDP协议。
1264 2