Qt/C++使用小记2【qt工程整理/qt工程内的文件整理】

第二个小收获:

众所周知,Qt创建项目后,会在工程名字下将.h文件、.cpp文件、.ui文件、.pro文件以及.user放在文件夹工程名称下的文件夹内,小demo工程就还好,但是一但文件多了,就显得杂乱无章,找起来很麻烦,看起来也很不专业,所以就有了这样一个小的收获~

直接上代码:

首先:创建一个工程:创建完成后的pro文件是这样的(如下)
默认的.pro文件
#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TidyUpQtProject
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

 这就是默认的工程.pro,只需在pro文件内加入一段代码,然后就可以指定文件的生成路径,如下所示:

修改之后的.pro文件
#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TidyUpQtProject
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

win32:{
    win32-msvc*{
        MSVC_VER = $$(VisualStudioVersion)
        equals(MSVC_VER, 14.0){
            contains(QT_ARCH, i386){
                CONFIG(debug, debug|release) {
                    LIB_DIR = $$PWD/../Lib/Debug_32
                    DESTDIR = $$PWD/../Bin/Debug_32
                    DLLDESTDIR = $$PWD/../Bin/Debug_32
                    TARGET = TidyUpQtProjectd
                }
                CONFIG(release, debug|release) {
                    LIB_DIR = $$PWD/../Lib/Release_32
                    DESTDIR = $$PWD/../Bin/Release_32
                    DLLDESTDIR = $$PWD/../Bin/Release_32
                    TARGET = TidyUpQtProject
                }
            }else{
                CONFIG(debug, debug|release) {
                    LIB_DIR = $$PWD/../Lib/Debug_64
                    DESTDIR = $$PWD/../Bin/Debug_64
                    DLLDESTDIR = $$PWD/../Bin/Debug_64
                    TARGET = TidyUpQtProjectd
                }
                CONFIG(release, debug|release) {
                    LIB_DIR = $$PWD/../Lib/Release_64
                    DESTDIR = $$PWD/../Bin/Release_64
                    DLLDESTDIR = $$PWD/../Bin/Release_64
                    TARGET = TidyUpQtProject
                }
            }
        }
    }
}
#  包含文件路径
INCLUDEPATH +=  ../Include  \

SOURCES += \
        main.cpp \
        MainWindow.cpp

HEADERS += \
        MainWindow.h

FORMS += \
        MainWindow.ui

上述更改中,只需要将TARGET即名称改为工程文件即可(其他的按需修改),添加完之后,还需要进行一步操作就是创建文件夹,默认文件夹是这样的(如下)

默认文件夹:

修改后的文件夹:

默认文件夹内的内容全部剪切到Source文件夹内,再重新打开工程即可,下面是文件夹的简要说明

Bin: 即根目录,生成的可执行文件全都再里面
Doc:即Document,文档文件,一些文档参考可以放在里面
Include:包含的动态库头文件,如果有使用动态库,可以将动态库的头文件放在里面(若在其中又创建了文件夹,需要在pro文件中的INCLUDEPATH下增加上相对路径,不然包含对应动态库的头文件的时候需要写文件夹路径,不然无法找到新增文件夹内的头文件)
Lib:动态库的.Lib文件全放在里面
Source:工程的源码全放在里面(若在其中又创建了文件夹,需要在pro文件中的INCLUDEPATH下增加上相对路径,不然包含对应的头文件的时候需要写文件夹路径,不然无法找到新增文件夹内的头文件)

以上就是整理的全部内容了

最后运行即可

这样工程直接简洁明了许多,还有一个提示,因为运行的时候会生成如:build-TidyUpQtProject-Desktop_Qt_5_9_4_MSVC2015_64bit-Debug的文件,可以在项目-Build下修改构建目录(如下):

修改前:

修改后: 

这样就不会胡乱生成类似于build-TidyUpQtProject-Desktop_Qt_5_9_4_MSVC2015_64bit-Debug的文件夹了,而是都会放在同级目录下的TempBuild文件内

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值