QT 5.4,无法从代码访问资源 [英] QT 5.4, Unable to access Resource from code

查看:48
本文介绍了QT 5.4,无法从代码访问资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将我的应用程序的样式图像包含到q资源文件中.当我将文件直接包含在代码中时,它可以工作,但是当我尝试使用QResource时,它会失败(请不要加载文件).

I try to include style images of my app into a q-resource file. When I include the file directly in the code, it work, but when i try to use QResource, it fail (do not load the file).

我在主目录中有资源文件:

I have the resource file in the main directory:

AppFolder
  |- main.cpp
  |- darkstyle.qrc
  |- darkstyle
       |- WindowTitleBar.png

以下示例打印:失败1失败2

The following example print: failed1 failed2

#include <QApplication>
#include <QResource>
#include <Qfile>
#include <QDebug>


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

    bool ok= QResource::registerResource("darkstyle.qrc");
    if (!ok) qDebug()<<"failed1";

    QFile file(":/darkstyle/WindowTitleBar.png");
    //QFile file("../AppFolder/darkstyle/WindowTitleBar.png"); //that work

    if(!file.open(QFile::ReadOnly | QFile::Text)) qDebug()<<"failed2";
    else file.close();

    //return a.exec();
    return 0;
}

注意:默认情况下,Qt创建器在顶部文件夹中创建二进制文件(.exe):../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe执行根路径似乎是:../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug

Note: Qt creator by default create binaries (.exe) in a top folder: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe The execution root path seem to be: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug

我尝试了执行路径的大多数可能组合.

I tried most of possible combinations with execution paths.

注2:有些示例使用.rcc文件格式,我没有这些格式,但这可能是一个线索.

Note2: Some examples use a .rcc file format, I have none of these, but that could be a clue.

摘要:如何从QT应用程序内部访问QResource文件?

Summary: How to access a QResource file from inside a QT app?

qrc文件的内容:

<RCC>
    <qresource prefix="/">
        <file>darkstyle/WindowTitleBar.png</file>
        <file>darkstyle/WindowTitleButton.png</file>
        <file>darkstyle/WindowTitleButton1.png</file>
        <file>darkstyle/WindowTitleButton2.png</file>
        <file>darkstyle/WindowTitleButton3.png</file>
    </qresource>
</RCC>

推荐答案

问题与给定版本的QT与MSVS2013不兼容有关.通过下载另一个版本的QT或Visual Studio可以解决该问题.

The problem is related to an incompatibility of the given version of QT with MSVS2013. The problem is solved by downloading another version of QT or visual studio.

这篇关于QT 5.4,无法从代码访问资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆