【问题标题】:Qt resources files with CMake and AUTORCC带有 CMake 和 AUTORCC 的 Qt 资源文件
【发布时间】:2015-06-10 16:21:54
【问题描述】:

解决方案

add_executable()语句中添加资源文件

问题

(不在add_library()

设置主窗口图标失败。

注意事项

  • 当我不使用AUTORCC 时,会遇到一些编译问题: QtCore/qglobal.h: no such file or directory。但是,我更喜欢 AUTORCC 作为更现代的 CMake 方法。

  • 如果没有 AUTORCC(与提供的 CMakeLists.txt 不同)和 Qt-4.6.2,当前代码可以正常工作。 不同的 CMakeLists.txt)

代码

这是我项目的最小化代码。树:

|- CMakeLists.txt
|- main_window.hpp
|- main_window.cpp
|- main.cpp
|- resources
   | - resources.qrc
   | - images
       | - logo.png

main_window.cpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();
};

#endif

main_window.cpp

#include "main_window.hpp"

MainWindow::MainWindow()
{
    // i tried ":/images.png", ":/resources/images/logo.png", ":/logo.png"
    setWindowIcon(QIcon(":images/logo.png"));    
}

main.cpp

#include <QApplication>
#include "main_window.hpp"

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

    app.setOrganizationName("Organization");
    app.setApplicationName("Application Example");
    MainWindow mainWin;
    mainWin.show();

    return app.exec();

}

CMakeLists.txt.

cmake_minimum_required(VERSION 3.1)

project(qt_project)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt4 4.6 REQUIRED)

set(QT_USE_QTGUI TRUE)
set(QT_USE_QTXML TRUE)

include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

// NOTE: it would be more convenient to be able to add the
// resource file here upon the creation of the library
add_library(mylib main_window.cpp)

// SOLVED
// BEFORE: add_executable(qt_test main.cpp)
add_executable(qt_test main.cpp resources/resources.qrc)

target_link_libraries(qt_test
    mylib
    ${QT_LIBRARIES}
)

resources/resources.qrc

<!DOCTYPE RCC><RCC version="1.0">
    <qresource>
        <file>images/logo.png</file>
    </qresource>
</RCC>

编辑

这是生成的qrc_resources.cxx

#include <QtCore/qglobal.h>

static const unsigned char qt_resource_data[] = {
  // /users/ddakop/dev/misc/qt/resources/images/logo.png
  // ... removed hex data
};

static const unsigned char qt_resource_name[] = {
  // images
  // ... removed hex data
    // logo.png
  // ... removed hex data

};

static const unsigned char qt_resource_struct[] = {
  // :
  0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,
  // :/images
  0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x2,
  // :/images/logo.png
  0x0,0x0,0x0,0x12,0x0,0x0,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,

};

QT_BEGIN_NAMESPACE

extern Q_CORE_EXPORT bool qRegisterResourceData
    (int, const unsigned char *, const unsigned char *, const unsigned char *);

extern Q_CORE_EXPORT bool qUnregisterResourceData
    (int, const unsigned char *, const unsigned char *, const unsigned char *);

QT_END_NAMESPACE


int QT_MANGLE_NAMESPACE(qInitResources_resources)()
{
    QT_PREPEND_NAMESPACE(qRegisterResourceData)
        (0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
    return 1;
}

Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_resources))

int QT_MANGLE_NAMESPACE(qCleanupResources_resources)()
{
    QT_PREPEND_NAMESPACE(qUnregisterResourceData)
       (0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
    return 1;
}

Q_DESTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qCleanupResources_resources))

系统

CentOS-5、Qt-4.8.6、CMake-3.2.1、gcc-4.8.2

【问题讨论】:

  • :/images/logo.png?
  • :/image/logo.png 只是一个我想设置为徽标的 png 文件。例如flaticon.com/free-icon/eye-outline_23912
  • 抱歉,这有点太短了 ;) 你试过那个字符串吗?它应该是正确的,并且不在您尝试的列表中。
  • 不工作太...(目录是images/
  • 资源是否正确读入.cpp文件?检查您的构建文件夹中的qrc_resources.cpp。这是一个.cpp 文件,其中包含要编译到目标中的所有二进制数据。

标签: c++ linux qt cmake qt4


【解决方案1】:

我认为你需要链接 qrc_resources 生成的文件。

我想你知道下一个信息:

http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html

在哪里可以看到下一行:

add_executable(myexe main.cpp resource_file.qrc)

更多信息:

http://www.cmake.org/cmake/help/v3.0/prop_tgt/AUTORCC.html

【讨论】:

  • 它工作正常。谢谢@塔罗德。我会评论认为携带资源文件以添加到最终的可执行文件是不方便的。
  • 是的。在未来的 CMake 版本中可能会更方便。
  • 它可以与库一起使用吗?我的情况与 OP 类似,但我真的只想在 lib 中包含一次资源,而不是在使用它的每个可执行文件中。
【解决方案2】:

我不确定这是否是新的,但我已经找到了一种将资源添加到库的方法。

在我的库的 CMakeLists.txt 中,我有以下内容:

list (APPEND RESOURCES library.qrc)
qt5_add_resources (RCC_SOURCES ${RESOURCES})

...

add_library(library ${RCC_SOURCES} ${SOURCES})

在库中,在我的一个“主类”中(对我来说,这个类是在库中创建的第一个东西,也是最后一个被销毁的东西),我做了以下事情:

class libClass : public QMainWindow {
  Q_OBJECT

public:
  libClass() : QMainWindow(nullptr, 0) {
    Q_INIT_RESOURCE(library);
  }

  ~libClass() {
    Q_CLEANUP_RESOURCE(library);
  }
};

【讨论】:

    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多