C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 使用CMake和AUTORCC的Qt资源文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
解:

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

问题

(不在add_library()中)

无法设置主窗口图标.

笔记:

>当我不使用AUTORCC时,我遇到了一些编译问题:
QtCore / qglobal.h:没有这样的文件或目录.但是,我更喜欢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
@H_584_2@main_window.cpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>

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

#endif
@H_584_2@main_window.cpp

#include "main_window.hpp"

MainWindow::MainWindow()
{
    // i tried ":/images.png",":/resources/images/logo.png",":/logo.png"
    setWindowIcon(QIcon(":images/logo.png"));    
}
@H_584_2@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.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,0x2,0x1,// :/images
  0x0,// :/images/logo.png
  0x0,0x12,};

QT_BEGIN_NAMESPACE

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

extern Q_CORE_EXPORT bool qUnregisterresourceData
    (int,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_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

解决方法

我想你需要链接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

大佬总结

以上是大佬教程为你收集整理的c – 使用CMake和AUTORCC的Qt资源文件全部内容,希望文章能够帮你解决c – 使用CMake和AUTORCC的Qt资源文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。