qt程序创建及模板代码分析

个人博客

qt ver 5.14.2

qt Creator创建程序

Build System

  • Qbs市场占有率低,将被弃用
  • qmake更轻量级
  • cmake功能强大

不繁琐的小程序用qmake,大工程文件用cmake更好

Details

Base class有三个选项:

  • QMainWindow
  • QWidget
  • QDialog

QMainWindow和QDialog是QWidget的子类。QWidget创建出来只有一个空白界面;QMainWindow在QWidget基础上多了菜单栏,工具栏和状态栏;QDialog对应对话框。

Generate form选项可以通过拖拽完成界面的设计

Translation

Translation File功能显见

Kits

版本套件选择,例如:Desktop对应.exe开发;UWP对应windows通用平台开发;

qt程序组成

main.cpp

#include "mywidget.h"

#include <QApplication> // 包含一个应用程序类的头文件

//main程序入口;argc命令行变量的数量;argv命令行变量的数组
int main(int argc, char *argv[]){
    //a应用程序对象,在Qt中,应用程序对象有且仅有一个
    QApplication a(argc, argv);
    //窗口对象,myWidget父类->QWidget
    myWidget w;
    //窗口对象 默认不会显示,必须要调用show方法才能显示窗口
    w.show();
    
    //让应用程序对象进入消息循环机制,窗口不会一闪而过
    return a.exec();
}

.pro文件

QT       += core gui //QT包含模块

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets //版本号大于4所额外包含模块

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 \
    mywidget.cpp

HEADERS += \	//头文件
    mywidget.h

FORMS += \
    mywidget.ui

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

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
//防止被多次include,文件只会被编译一次和pragma once等效

#include <QWidget>//窗口类QWidget头文件

QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE

class myWidget : public QWidget
{
    Q_OBJECT //Q_OBJECT是一个宏,允许类中使用信号和槽的机制

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

private:
    Ui::myWidget *ui;
};
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"

//命名规范
//类名 首字母大写,单词和单词之间首字母大写
//函数名 变量名称 首字母小写,单词和单词之间首字母大写

//快捷键
//注释 ctrl + /
//运行 ctrl + r
//编译 ctrl + b
//字体缩放 ctrl + 鼠标滚轮
//自动对齐 ctrl + i

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

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值