Qt5 cmake引用private头文件
Qt5 cmake引用private头文件
如何引用Qt的qzipreader_p.h头文件 、xlsxzipreader_p.h头文件
使用 target_include_directories
target_include_directories(TestQtXlsx PRIVATE
${Qt${QT_VERSION_MAJOR}Gui_PRIVATE_INCLUDE_DIRS}
${Qt${QT_VERSION_MAJOR}Xlsx_PRIVATE_INCLUDE_DIRS}
)
注:Qt Xlsx是第三方库,安装请参照Qt Xlsx安装教程
完整的例子
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(TestQtXlsx LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui Xlsx REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Xlsx REQUIRED)
add_executable(TestQtXlsx
main.cpp
)
target_include_directories(TestQtXlsx PRIVATE
${Qt${QT_VERSION_MAJOR}Gui_PRIVATE_INCLUDE_DIRS}
${Qt${QT_VERSION_MAJOR}Xlsx_PRIVATE_INCLUDE_DIRS}
)
target_link_libraries(TestQtXlsx Qt${QT_VERSION_MAJOR}::Core)
target_link_libraries(TestQtXlsx Qt${QT_VERSION_MAJOR}::Xlsx)
target_link_libraries(TestQtXlsx Qt${QT_VERSION_MAJOR}::Gui)
C++源码
#include <QCoreApplication>
#include <QDate>
#include <QTime>
#include <QDebug>
#include "xlsxdocument.h"
#include <private/qzipreader_p.h>
#include <private/xlsxzipreader_p.h>
static void test_xlsx_zip()
{
//定义一个QXlsx文档对象
try {
QXlsx::ZipReader reader("test3_example.xlsx");
bool ret = reader.exists();
QStringList paths = reader.filePaths();
qDebug()<<"ret="<<ret;
qDebug()<<"paths="<<paths;
QXlsx::Document xlsx("test3_example.xlsx");
qDebug()<<"======";
QZipReader qreader("test3_example.xlsx");
bool ret2 = qreader.isReadable();
qDebug()<<"ret2="<<ret2;
int status = qreader.status();
qDebug()<<"status="<<status;
QVector<QZipReader::FileInfo> qvec = qreader.fileInfoList();
qDebug()<<"qvec size="<<qvec.size();
}
catch (...) {
qDebug()<<"Unknown error";
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test_xlsx_zip();
return a.exec();
}
参考文献:
Qt5 cmake引用private头文件
qt modules public internal 私有头文件 private
Qt 自带的压缩QZipWriter和解压QZipReader详解~含Demo
QT QZipReader改进,以支持大于2G的zip文件
升级到qt6.6.2后,提示QZipReader和QZipWriter两个类不存在了的解决方法