Access Qt resource files in VS 2017 with CMake - visual-studio

I am having trouble accessing qt resource files in a VS 2017 project with CMake. I was able to access the qrc files using Qt Creator. I have the following questions:
Where do I put the resource files? Preferably I would like to put resource files in the root directory. I have tried to put the files in the root director and in the directory they will be called.
What changes I should make to the CMakeLists.txt files?
How can I refer to the resource files? I have tried all the instruction I can find on internet. But none of them seem to work. For example I have tried: ":/prefix/filepath/filename" or ":prefix/filepath/filename".
Thanks a lot!

You can put your .qrc file where you want, for my project I make it on /res folder but is your choice. You can add your files on the resources edit manually the .qrc file as a normal xml file. Here a simple example:
<RCC>
<qresource prefix="/menu_img">
<file alias="cancel_img">icons/cancel-512.png</file>
<file alias="open_img">icons/folder-512.png</file>
<file alias="print_img">icons/printer-512.png</file>
</qresource>
</RCC>
In this resource files the images are inside the folder icons and the .qrc is outside.
After that your .qrc file is ready (assuming that you use qt5) you must add to your CMakeList.txt this line:
qt5_add_resources(PROJECT_RESOURCE path_to_rosource_file/resources.qrc)
then on your target you must add this rosource:
add_executable(
${TARGET_PROJECT}
${PROJECT_SOURCES}
${PROJECT_HEADERS}
${PROJECT_RESOURCES})
Now your resources is available inside your project as usual :/menu_img/cancel_img

Related

Making local project items accessible from the shared projects in VS C++ 2017

I have deb_sets.h in the main project to set debugging options for the whole project.
Shared projects include this file too. However, it will be different in different main projects.
I get a compilation error fatal error: deb_sets.h: No such file or directory in every shared project file which include this file.
Is my idea possible to achieve?
You need to add the #include "deb_sets.h" in all source files that use this header, but if this header file is in another directory, you need to change "Additional Include Directories" (C++\General section) in the project options to set the header file path.

Move packages folder inside the solution folder

Visual Studio 2017 / ASP.NET Web Application solution.
The default folder structure is as follow:
What I want to achieve is keeping everything (including packages) inside the WebApplication1 folder (except WebApplication1.sln file).
I know about NuGet.Config but unfortunately it seems mandatory to place this file next to the .sln file. I would have preferred to place this file inside of WebApplication1 folder. I would like to have ONLY ONE file outside of the solution folder: the WebApplication1.sln file.
I know about NuGet.Config but unfortunately it seems mandatory to place this file next to the .sln file. I would have preferred to place this file inside of WebApplication1 folder. I would like to have ONLY ONE file outside of the solution folder: the WebApplication1.sln file.
I understand your requirement, you want to have only WebApplication1.sln file outside of the solution folder. But if you are using nuget.config to change the location of packages, this nuget.config file will be placed in a folder outside the solution folder, which is not what you want.
To resolve this issue, you can put this nuget.config file outside the solution folder, which is not necessary to put this file next to the .sln file, for example, you can put this file in the root directory of the C drive with following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="C:\Users\<UserName>\source\repos\WebApplication1\WebApplication1\packages" />
</config>
</configuration>
Then close your Visual Studio and delete the packages folder in the solution folder, re-open the Visual Studio and open the your solution, right click on the your solution, select restore the nuget packages, all the nuget packages will be place inside the solution folder and the only file outside of the solution folder is the WebApplication1.sln file:
Note: This method has its own limitations, we have to use absolute path in the nuget.config file and this setting will still be work for other solutions.
Besides, you can try to use PackageReference instead of packages.config in project files. With PackageReference, your packages are point to the global package folder C:\Users\<UserName>\.nuget\packages, so that we do not need add a nuget.config file to change the package folder.
To use the PackageReference, go to the Tools->NuGet PackageManager->Package Manage Settings:
Hope this helps.
My understanding of why the visual studio put the package folder next to .sln but outside WebApplication1 folder is, WebApplication1 is a project in the WebApplication1 solution. But one solution can contains multiple projects. In your case the project name happens to be same as the solution name(due to when you create this project and solution in vs). So if you have multiple projects in this solution but want to share some nuget packages, then it make sense for the package folder to be outside the project folder.
Thant's my understanding for why vs has this default setting. Hope that helps.

Working directory vs output path in visual studio

What is the difference of the working directory vs the output path in visual studio?
Is it bad to set both setting to the same directory like '....\bin\'
By default they are the same. Assuming you're debugging some application it will be bin\debug.
Output Directory is where your output files goes. By default, Visual Studio builds each project in a solution in its own folder inside the solution. You can change the build output paths of your projects to force all outputs to be placed in the same folder
Working Directory is the location which Specifies the working directory of the program being debugged. It is the default place in which a program is looking up it's files. Normally the working directory is the directory the application is launched from \bin\debug by default.
So every opened file will be opened relative to the working folder.Let's say if you have an application installed in c:\myapp with all lib files in the same directory then you will set your working directory to myapp folder and you can do so from project properties.
By default, working directory is the output directory. Both can be changed, you can set another directory or common directory for all projects for output directory that determines a relative path for the output file that might be an executable or a static library.
Working directory also provides a relative path to put files that are used by the program. You can put a log file into a place that you can use its directory as a relative path in the code instead of absolute path. If your working directory is myproject\src and your log file is in myproject\src\log\log.txt then you can open or write the log file with log\log.txt in the code rather than c:\blabla\myproject\src\log\log.txt.

How to automatically copy files in building project with Visual Studio

I have some custom made XML files and a read me file that I've included in my project.
How do I make it so that these files are copied to my debug/release folders automatically when I build my project? I've tried setting the Copy to Output Directory property to "Copy Always" but this doesn't seem to do the trick.
I'm using VS2010
I've found the answer. The build action needs to be set to Content if you want to just directly copy the file to the output folder.

Is there a way to extract info from vs project, eg cl.exe command line

Since I write a command line program to check cpp files, and it need lib path and include path of those cpp files.
How can I get the include and lib path info from visual studio project? Our project uses property sheets to make up those information.
It is made up from 3 distinct sources:
Tools + Options, Projects and Solutions, VC++ Directories. In turn built up from several registry keys
the settings in your .vsprops project property sheets
the settings in your .vcproj project
The first part is the hardest, you can get it by running vc\vsvarsall.bat. The .vsprops and .vcproj files are XML, easy to parse.
If you just want to find out what the command line should look like then you can get it from the buildlog.htm file, produced when building from the IDE. Or you could use vcbuild.exe on the command line to build your project from the .vcproj file. Or you could build using devenv.exe /build.
Check out the Visual Studio project files - they're typically only XML files, so you should be able to extract out whatever you need from those, really. Just a matter of understanding and parsing your XML contents in the project file, really.

Resources