C++ Qt c++;VS代码:如何使用相对文件路径?
我在Mac上的VS代码中使用Qt6框架。 我有一个Window.h和Window.cpp文件来使用QOpenGLWindow和QOPENGLFUNCTION创建窗口 窗户C++ Qt c++;VS代码:如何使用相对文件路径?,c++,qt,cmake,resources,C++,Qt,Cmake,Resources,我在Mac上的VS代码中使用Qt6框架。 我有一个Window.h和Window.cpp文件来使用QOpenGLWindow和QOPENGLFUNCTION创建窗口 窗户 [已解决] 我在res文件夹中创建了一个res.qrc文件,代码如下: res.qrc 现在我可以使用着色器文件的相对路径: Window.cpp 谢谢@chehrlic 只需以源目录作为工作目录运行应用程序。如果通过VSCode运行应用程序,则相应地配置VSCode。或者将着色器放入Qt资源文件或相对于可执行文件,并使用QC
[已解决] 我在res文件夹中创建了一个res.qrc文件,代码如下: res.qrc 现在我可以使用着色器文件的相对路径: Window.cpp
谢谢@chehrlic 只需以源目录作为工作目录运行应用程序。如果通过VSCode运行应用程序,则相应地配置VSCode。或者将着色器放入Qt资源文件或相对于可执行文件,并使用QCoreApplication::applicationDirPath()
#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 */
#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, 1.0f), QVector3D(0.0f, 1.0f, 0.0f) ),
Vertex( QVector3D(-0.75f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 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(){
...
}
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
${CMAKE_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}"
)
<!DOCTYPE RCC><RCC version = "1.0">
<qresource>
<file>shaders/fragmentShader.frag</file>
<file>shaders/vertexShader.vert</file>
</qresource>
</RCC>
add_executable(wuw
res/res.qrc (that line)
src/main.cpp
src/Window.cpp hdr/Window.h
hdr/Vertex.h
)
...
Q_INIT_RESOURCE(res);
// ----- opengl events -----
void Window::initializeGL(){
// initialize opengl backend
initializeOpenGLFunctions();
printContextInformation();
// set global information
glClearColor(1.0f, 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();
...
}
}
...