How to define a reusable folder path in Qt project file? - c++

When working with a Qt project file *.pro one can use for example the $$PWD to reference to the folder containing the project file.
Is there a possible way to define a custom folder path?
Like saying: $$CUSTOMFOLDER = /home/user/folder
I would like to define CUSTOMFOLDER once and then reuse it in the project file so that I dont always have to paste the long folder path. By That the code would also become more readable.

Related

Accessing the same folder from different cpp files relative to the Project folder

If my project has the following folder structure:
Project
├───build
├───images
├───include
├───Apps
├───Models
├───source
└───tests
what is the best way to make the folder "images" accessable to all .cpp files inside build, tests, apps and src without using the absolute path. So every image created inside this project should be saved to the "images" folder.
I am building with Cmake if this is important(started using CMake last week so no deep knowledge). Main CmakeLists.txt file is the the root folder. Tests, Apps and source each have their own CMakeLists.txt files and executables.
Every image will be created with the same class so I think I could use std::filesystem::current_path() with a wrapper function inside the class which would generate and set the desired path but there should be another way.
I will also load files from the folder Models in the future, so the same problem.
If I am understanding your question correctly you want to access the images from the "images" folder, right? You should be thinking about the path from the point of the final executable and not the source files.
If the executable will be in the build directory then you simply need to write "../images", the 2 dots mean that you are going back 1 directory.

How to copy a .ui file in my qwidgets project?

I wish to try this qt example. I created a qwidgets project, copied all headers and sources from the following example to my project.
http://doc.qt.io/archives/qt-5.5/qtmultimedia-audiorecorder-example.html
What is the way to copy the .ui file of that project to my project?
What should be the extention of that file in my project?
The *.ui files are GUI designer files and define the actual GUI.
Copy it as-is. You'll need to invoke the uic (User Interface Compiler) on it to produce the ui_audiorecorder.h header file, which is included in the audiorecorder.cpp file.
Read more on the uic toolchain in the docs: Using a Designer UI File in Your Application.
You copy the .ui file into your project's folder using your favorite file manager or terminal or what have you. Suppose that the file is named foo.ui.
Then you add the file to the project by either:
Adding the following line to the .pro file:
FORMS += foo.ui
Right-clicking the root project node, selecting "Add Existing Files...", and navigating to the foo.ui file in the file dialog.
The two procedures are equivalent, the result is the same: a new FORMS item in the project file.
That's all. As soon as you save the .pro file, Qt Creator will parse it and show the foo.ui under a Forms project tree node.

File not being created in correct directory C++

I'm trying to use fstream to create a file, however the file i'm trying to create wont appear in the .exe directory or anywhere else in the file directory. After searching in my computer for the file, I found that was created in a different directory entirely even though i'm using a relative directory.
This is the code to create the file:
ofstream file;
file.open("something.jpg", ios::out|ios::binary|ios::beg);
Directory of created file: C:\Users\user-pc
Directory of project: D:\Users\user-pc\Documents\Visual Studio 2012\Projects\recvFile
by the way, using an absolute directory works perfectly fine. Could this be a problem with the projects working directory?
In your Visual Studio right-click on your project, click Properties, then go to Configuration Properties, then Debugging. There is a row "Working Directory". You can set the working directory there. If you need to do this programmatically, you can use SetCurrentDirectory .
If you need to create the file in the same directory as the .exe location, you can use this approach: https://stackoverflow.com/a/124901/1915854
Call GetModuleFileName() using 0 as a module handle...
If the .exe is installed in a shared directory like Program Files, then creation of the file in the same directory could require additional permissions and may be a bad idea. If the .exe is just cloned to the directory where it should create files, then there is no such problem.
Try adding "../" to the link:
file.open("../something.jpg", ios::out|ios::binary|ios::beg);
File will be created in Debug/Release folder of your project. try what Timo Rzipa suggested.

Eclipse c++ project folders

I have a lot of classes in my project which means a lot of files too. I want to put everything in subfolders so it's a grouped together and a bit more clear.
There is already a folder names scr where all my cpp and header files are in but I want to create a folder named 'products' and put all the product-related files in there.
But when I do so, I get this error (after pressing build) that one of my classes can't be build. It says:
make: * [scr/Producten/AudioKaart.o] Error 1 KassaSysteem C/C++
Problem recipe for target `scr/Producten/AudioKaart.o'
failed subdir.mk /KassaSysteem/Debug/scr/Producten line 30 C/C++
Problem
What's the right way to put files in subfolders? Do I need to change the includes? Or do something more than just dragging and dropping them into the folder?
If you put headers in your Producten directory, you need to include this directory in your project settings. Another way is to include your header by specifying the sub-directory, something like
#include "Producten/xxx.h"
Eclipse uses directories for project folders, so you need to adjust all includes as well.

Eclipse C++ including header file from my source folder

I'm pretty new to C++ and Eclipse in general so I apologise if I'm missing something fairly obvious.
The problem I'm having is that I'm trying to include a header file in one of my source files but they're in different folders in my project directory. I have no idea how I should be including them. I've uploaded an image showing my problem with the header file I want to include highlighted.
If someone could tell me what '#include' statement I should be using them that would be brilliant.
Thanks!
There are a couple of different options to make this work. Simplest is to change the #include to
#include "../Statistics/Statistics.h"
This will work without any other modifications. However, if you move either file, or somehow change the relative path between the two, this will break.
Alternately, you can add the path to the Statistics folder to your compiler's include file search path. Right click on the project name, select Properties -> C/C++ Build -> Settings and then find the includes files path option for your compiler. For g++, it is -I<path/to/include/folder>. Adding this will make the #include statement work as you currently have it.
A very similar option to the second one is to add the path to the src folder (instead of the Statistics folder) to the includes search path. In this case, you'll have to change the statement to
#include "Statistics/Statistics.h"
When you create subfolders in your src folder then each cpp file is compiled in that folder it is located in. Thus, any "" includes need to specify the relative path to get from that folder to another.
In your case, to get from inside the FileInOut folder you need to go back one level and then into the Statistics folder
eg
#include "../Statistics/Statistics.h"
Another alternative is, if you are keeping your includes in your src directory, to add the src directory to the include path. Now when you include you need only specify the path from the src root.
eg.
#include "Statistics/Statistics.h"