程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Qt C++ VS 代码:如何使用相对文件路径?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Qt C++ VS 代码:如何使用相对文件路径??

开发过程中遇到Qt C++ VS 代码:如何使用相对文件路径?的问题如何解决?下面主要结合日常开发的经验,给出你关于Qt C++ VS 代码:如何使用相对文件路径?的解决方法建议,希望对你解决Qt C++ VS 代码:如何使用相对文件路径?有所启发或帮助;

我在 Mac 上的 VS 代码中使用 Qt6 框架。 我有一个 Window.h 和 Window.cpp 文件来使用 QOpenGLWindow 和 QOpenGLFunctions 创建窗口。

窗口.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QOpenGLWindow>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>

class QOpenGLShaderProgram;

class Window: public QOpenGLWindow,protected QOpenGLFunctions{
    Q_OBjeCT
public:
    ~Window();

    // opengl events
    voID initializeGL();
    voID resizeGL(int wIDth,int height);
    voID paintGL();
    voID teardownGL();

private:
    // opengl state information
    QOpenGLBuffer m_vertex;
    QOpenGLVertexArrayObject m_object;
    QOpenGLShaderProgram *m_program;

    // private Helper
    voID printContexTinformation();
};

#endif /* WINDOW_H */

窗口.cpp

#include "hdr/Window.h"
#include "hdr/Vertex.h"

#include <QDeBUG>
#include <QString>
#include <QOpenGLShaderProgram>

// create a colored triangle
static const Vertex sg_vertexes[] = {
    Vertex( QVector3D( 0.00f,0.75f,1.0f),QVector3D(1.0f,0.0f,0.0f) ),Vertex( QVector3D( 0.75f,-0.75f,QVector3D(0.0f,1.0f,Vertex( QVector3D(-0.75f,1.0f) )
};

Window::~Window(){
    makeCurrent();
    teardownGL();
}

// ----- opengl events -----
voID Window::initializeGL(){
    // initialize opengl BACkend
    initializeOpenGLFunctions();
    printContexTinformation();

    ...

    // application-specific initialization
    {
        // create shader dDo not release until vao is created)
        m_program = new QOpenGLShaderProgram();
        m_program->addShaderFromsourcefile(QOpenGLShader::Vertex,"shaders/vertexShader.vert");
        m_program->addShaderFromsourcefile(QOpenGLShader::Fragment,"shaders/fragmentShader.frag");
        m_program->link();
        m_program->bind();

        ...
    }
}

voID Window::resizeGL(int wIDth,int height){
    ...
}

voID Window::paintGL(){
    ...
}    

voID Window::teardownGL(){
    ...
}

// ----- private Helper -----
voID Window::printContexTinformation(){
    ...
}

因此,在我的 window.cpp 中,我使用相对路径“shaders/vertexShader.vert”/“shaders/fragmentShader.frag”从源文件添加着色器,但这不起作用。 实际上,我使用着色器文件的唯一方法是将整个文件路径 ("/Users/[name]/c++/WuW/res/shaders/vertexShader.vert" / "/Users/[name]/c++/WuW/ res/shaders/fragmentShader.frag").

这是我的项目文件夹的组织方式:

Qt C++ VS 代码:如何使用相对文件路径?

CMakeLists.txt

cmake_minimum_required(VERSION 3.16.0)
project(wuw VERSION 0.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_required ON)

set(CMAKE_autoMOC ON)
set(CMAKE_autoRCC ON)
set(CMAKE_autoUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(NOT defineD INSTall_EXAMPLESDIR)
    set(INSTall_EXAMPLESDIR "examples")
endif()

set(INSTall_EXAMPLEDIR "${INSTall_EXAMPLESDIR}/WuW/Build")

find_package(Qt6 COMPONENTS CorE)
find_package(Qt6 COMPONENTS Widgets)
find_package(Qt6 COMPONENTS Gui)
find_package(Qt6 COMPONENTS OpenGL)
find_package(Qt6 COMPONENTS OpenGLWidgets)

add_executable(wuw
    src/main.cpp
    src/Window.cpp hdr/Window.h
    hdr/Vertex.h
)

set_target_propertIEs(wuw PROPERTIES
    WIN32_EXECUtable TRUE
    MACOSX_BUNDLE TRUE
)

target_include_directorIEs(wuw PUBliC
    ${CR_670_11845@AKE_CURRENT_sourcE_DIR}
)

target_link_librarIEs(wuw PUBliC
    Qt::Core
    Qt::Widgets
    Qt::Gui
    Qt::OpenGL
    Qt::OpenGLWidgets
)

install(TARGETS wuw
    RUNTIME DESTinATION "${INSTall_EXAMPLEDIR}"
    BUNDLE DESTinATION "${INSTall_EXAMPLEDIR}"
    liBRARY DESTinATION "${INSTall_EXAMPLEDIR}"
)

解决方法

[已解决]

我使用以下代码在 res 文件夹中创建了一个 res.qrc 文件:

res.qrc

<!DOCTYPE RCC><RCC version = "1.0">
<qresource>
    <file>shaders/fragmentShader.frag</file>
    <file>shaders/vertexShader.vert</file>
</qresource>
</RCC>

并将此文件添加到可执行文件中:

CMakeFiles.txt

add_executable(wuw
    res/res.qrc (that linE)
    src/main.cpp
    src/Window.cpp hdr/Window.h
    hdr/Vertex.h
)

现在我可以使用着色器文件的相对路径了:

窗口.cpp

...
Q_INIT_resourcE(res);

// ----- opengl events -----
void Window::initializeGL(){
    // initialize opengl BACkend
    initializeOpenGLFunctions();
    printContexTinformation();

    // set global information
    glClearColor(1.0f,1.0f,1.0f);

    Q_INIT_resourcE(res);

    // application-specific initialization
    {
        // create shader dDo not release until vao is created)
        m_program = new QOpenGLShaderProgram();
        m_program->addShaderFromsourceFile(QOpenGLShader::Vertex,":/shaders/vertexShader.vert");
        m_program->addShaderFromsourceFile(QOpenGLShader::Fragment,":/shaders/fragmentShader.frag");
        m_program->link();
        m_program->bind();
    
        ...
    }
}

...

谢谢@chehrlic!

大佬总结

以上是大佬教程为你收集整理的Qt C++ VS 代码:如何使用相对文件路径?全部内容,希望文章能够帮你解决Qt C++ VS 代码:如何使用相对文件路径?所遇到的程序开发问题。

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

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