自学内容网 自学内容网

【CMake】为可执行程序或静态库添加 Qt 资源文件,静态库不生效问题

【CMake】添加静态库中的 Qt 资源

这里介绍的不是使用 Qt 创建工程时默认的 CMakeLists.txt,是使用 Visual Studio + Qt 的方式开发的 CMakeLists.txt,开发环境 VS2019 + Qt 5.12

可执行程序

1. 创建资源文件(.qrc)

首先,创建一个Qt资源文件(例如resources.qrc),并在其中列出你想要包含的资源文件。例如:

<RCC>
    <qresource prefix="/">
        <file>images/logo.png</file>
        <file>styles/style.qss</file>
    </qresource>
</RCC>

示例目录结构

MyQtApp/
├── CMakeLists.txt
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── resources.qrc
└── resources/
    ├── images/
    │   └── logo.png
    └── styles/
        └── style.qss

2. 修改 CMakeLists.txt

一般为可执行程序,即 App 应用,可以使用以下方式修改 CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(MyQtApp)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

# 添加资源文件
set(RESOURCE_FILES resources.qrc)
qt5_add_resources(QT_RESOURCES ${RESOURCE_FILES})

# 添加源文件
set(SOURCE_FILES main.cpp mainwindow.cpp)

# 添加头文件
set(HEADER_FILES mainwindow.h)

# 添加可执行文件
add_executable(MyQtApp ${SOURCE_FILES} ${HEADER_FILES} ${QT_RESOURCES})

# 链接Qt5库
target_link_libraries(MyQtApp Qt5::Widgets)

3. 使用资源文件

在 Qt 代码中,可以通过使用:/前缀来访问资源文件。例如:

#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QPixmap>
#include <QFile>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow mainWindow;
    QLabel label(&mainWindow);

    // 加载图像资源
    QPixmap pixmap(":/images/logo.png");
    label.setPixmap(pixmap);

    // 加载样式表资源
    QFile file(":/styles/style.qss");
    if (file.open(QFile::ReadOnly | QFile::Text)) {
        QTextStream stream(&file);
        QString styleSheet = stream.readAll();
        app.setStyleSheet(styleSheet);
    }

    mainWindow.show();

    return app.exec();
}

静态库

通常在可执行文件拥有资源文件时,其他库使用 CMake 可能不生效

1. 修改 CMakeLists.txt

与可执行程序相同的方式创建 资源文件,此处不重复赘述。

# 添加资源文件
set(RESOURCE_FILES resources.qrc)
qt5_add_resources(QT_RESOURCES ${RESOURCE_FILES})

set(SOURCE_FILES mylib.cpp
set(HEADER_FILES mylib.h)

# 添加静态库
add_library(MyStaticLib STATIC ${SOURCE_FILES} ${HEADER_FILES} ${QT_RESOURCES})
# 链接Qt5库
target_link_libraries(MyStaticLib Qt5::Core)

目录结构示例:

MyStaticLib/
├── CMakeLists.txt
├── mylib.cpp
├── mylib.h
├── resources.qrc
└── resources/
    ├── images/
    │   └── logo.png
    └── styles/
        └── style.qss

MyQtApp/
├── CMakeLists.txt
└── main.cpp

2. 使用资源

使用资源文件的方式与可执行程序类似,使用 :/ 前缀来访问资源文件。可在静态库中使用资源文件,或主程序中使用资源文件。

若静态库中的资源未生效,则需要在可执行程序中手动初始化静态库中的资源文件。

2.1 初始化资源文件

在静态库源文件中定义初始化资源函数 ,使用 Q_INIT_RESOURCE 宏来初始化静态库的资源文件。

#include <QtCore/QResource>

// 定义初始化资源的函数
void initMyStaticLibResources()
{
    Q_INIT_RESOURCE(resources);
}
2.2 可执行程序中调用
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QPixmap>
#include <QFile>
#include <QTextStream>

// 声明初始化资源的函数
extern void initMyStaticLibResources();

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 初始化静态库的资源文件
    initMyStaticLibResources();

    QMainWindow mainWindow;
    QLabel label(&mainWindow);

    // 加载图像资源
    QPixmap pixmap(":/images/logo.png");
    label.setPixmap(pixmap);

    // 加载样式表资源
    QFile file(":/styles/style.qss");
    if (file.open(QFile::ReadOnly | QFile::Text)) {
        QTextStream stream(&file);
        QString styleSheet = stream.readAll();
        app.setStyleSheet(styleSheet);
    }

    mainWindow.show();

    return app.exec();
}

原文地址:https://blog.csdn.net/weixin_44488341/article/details/142925238

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!