简体   繁体   中英

How to get the current Path on Qt

I have a file with shaders inside, in my Qt project file.

When i tried to get the Path of the application

QCoreApplication::applicationDirPath()

It give me :

"C:/Users/USER_NAME/Desktop/PROJECT_NAME/build-PROJECT_NAME-Desktop_Qt_5_7_0_MinGW_32bit-Debug/debug"

But actually, the shaders files are in :

"C:/Users/USER_NAME/Desktop/PROJECT_NAME/PROJECT_NAME/shaders"

How can i get the correct path ?

You are getting the correct results. By default most IDEs, Qt Creator included, compile the project to a different directory than the sources are stored in and run it from there or from yet another location.

Assuming you're using Qt Creator, you can change the working directory of the executable in project settings pane:

屏幕截图

As per QCoreApplication::applicationDirPath() which returns the directory that contains the application executable. The output you're getting is based on while you're running your application from the Qt IDE. And, it expects your shaders files to be under the same location as your executable. So, either you can use a common location (like. %appdata%) or use hard coded path where your shader files are located. Another solution might be copy your shader files under the location of executable, in your case:

C:/Users/USER_NAME/Desktop/PROJECT_NAME/build-PROJECT_NAME-Desktop_Qt_5_7_0_MinGW_32bit-Debug/debug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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