简体   繁体   English

使用CMake和AUTORCC的Qt资源文件

[英]Qt resources files with CMake and AUTORCC

Solution : 方案

Add resource files in the add_executable() statement add_executable()语句中添加资源文件

Problem 问题

(not in the add_library() ) (不在add_library()

Fail to set main window icon. 无法设置主窗口图标。

Notes : 备注

  • When I don't use AUTORCC I run into some compilation problems: QtCore/qglobal.h: no such file or directory . 当我不使用AUTORCC我遇到了一些编译问题: QtCore/qglobal.h: no such file or directory But, I do prefer AUTORCC as a more modern CMake approach. 但是,我更喜欢AUTORCC作为一种更现代的CMake方法。

  • Without the AUTORCC (different CMakeLists.txt than the provided) and Qt-4.6.2 the current code worked. 如果没有AUTORCC(与提供的CMakeLists.txt不同)和Qt-4.6.2,则当前代码无效。 different CMakeLists.txt) 不同的CMakeLists.txt)

Code

This is a minimized code of my project. 这是我项目的最小化代码。 Tree: 树:

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

main_window.cpp main_window.cpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>

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

#endif

main_window.cpp 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 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 . 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 资源/ resources.qrc

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

Edit 编辑

This is the generated qrc_resources.cxx 这是生成的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))

System 系统

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

I think you need to link qrc_resources generated file. 我认为你需要链接qrc_resources生成的文件。

I suppose you know the next info: 我想你知道下一个信息:

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

Where you can see the next line: 在哪里可以看到下一行:

add_executable(myexe main.cpp resource_file.qrc)

More info: 更多信息:

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

I'm not sure if this is new or not, but I have figured out a way to add resources to libraries. 我不确定这是否是新的,但我已经想出了一种向库添加资源的方法。

In my library's CMakeLists.txt I have the following: 在我的图书馆的CMakeLists.txt中,我有以下内容:

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

...

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

In the library, in one of my "main classes" (for me, this class was the first thing that gets created in the library and the last thing that gets destroyed), I did the following: 在库中,在我的一个“主类”中(对我来说,这个类是在库中创建的第一个东西,最后一个被破坏的东西),我做了以下内容:

class libClass : public QMainWindow {
  Q_OBJECT

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM