Qt creator direct file include - c++

I am using Qt Creator 3.2.2 for windows. I have created an example project widget application project and that's okay. The problem is I had to add some external libraries (.c and .h files) that are in the project folder. The problem is they are not all in the same place but mixed in subcatalogs. Qt creator seemed to add them all correctly.
But the problem now is that I cannot use for example:
#include <somefile.h>
Because it says that there is no such file. Autocomplete also doesn't see it. I have to do:
#include <dir/deeper_dir/somefile.h>
And the whole library is created the way that every file is reachable from everywhere. Also I would not like to change that.
How can I make all files visible within the project?

Add the path to your headers into your .pro file with INCLUDEPATH:
INCLUDEPATH += path_to_the_folder_with_your_headers
After that you will be able to include any file in that folder just with #include <filename>

add
INCLUDEPATH += dir/deeper_dir/
in your pro file.

Related

how to link Qt with a visual studio c++ application project?

I have Qt 5.12.0 in a folder.
I need to create a dll and/or a bin project that can connect with a QML program.
My problem is that the vs project can't find the Qt files I need.
#include <QGuiApplication>
does not work.
This topic is the continuity of this one
How to link libraries to a project on visual studio? where I shared my problems with linking my dll with my bin project and to link Qt.
So I use a batch file to set the environment variable before launching visual studio. I use these variables to get the path to the Qt include folder, Qt lib etc.
Here is what I tried.
include the directory that contains the headers I need (I guess) :
configuration properties -> C/C++ -> General -> other include directories ->$(QT_INC)/. QT_INC is the path to the include folder. I also tried to write $(QT_INC)/* and $(QT_INC)/QtGui/. In any case,
#include <QGuiApplication>
couldn't compile.
i also added the path to the library folder :
linker->General->Additional library directories->$(QT_LIB)/
and some lib files in :
linker->entry->additionnal dependencies->Qt5Quick.lib;Qt5Gui;lib;Qt5Core.lib
none of these steps creates any error. It just doesn't help to find QGuiApplication.h nor QObject or anything I need.
I know my paths are correct and that using the environment variable like this works as I linked my dll using this method, and because wrong paths generates errors.
How to add Qt to my solution or to a project ?
thanks in advance
It works. The path was incorrect. There were "/" instead of "\" I think. And a synthax error in the batch file.
It's possible to add the header files of Qt by including the path in the properties.
include just the directory include of the Qt folder. Then, include headers again and again until all the errors are gone.
only for QGuiApplication, I need to include this
#include <QtGui/qtguiglobal.h>
#include <QtGui/qcoreapplication.h>
etc. all the includes of the beggining of QGuiApplication.h actually
here is the link to my other post I made about linking Qt to visual studio. How to link libraries to a project on visual studio? The problem was about the path to the dll.

How to list all paths that are used for including header files in a Qt project?

I've imported a C++ project (Shrewsoft VPN) into Qt creator.
Some include statements fail, because the included file cannot be found e. g.
#include <openssl/rand.h>
I know where the openssl/rand.h file resides. But I don't know the paths Qt / C++ compiler looks for it.
Is there a way to get a list of all paths Qt searches for this header files?
There are two things that influence the search path: The build environment and the project settings. The build environment is typically inherited from the system, and the project settings are defined in you .pro file
In Qt Create 4.3, you can see the build environment in
Project (on the left pane) > Build > Build Environment > Details > INCLUDE
But I recommend to add the include path in your project file. Add this in your .pro file:
INCLUDEPATH += (Base Path to openssl includes)
You may also need to extend the library search path for linking:
LIBS += -L(Path to openssl libs) -l(openssl lib name)
Qt searches header files in 3 way,
The first search path is your compiler path, exp. With MSVC2015 it will search in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include
The second search path is your Qt version folder and linked Qt library, exp. With Qt5.9.2 and widgets library, it will search in C:\Qt\5.9.2\msvc2015_64\include\QtWidgets
The last search path is your external header path contain in your INCLUDEPATH var parsed with QMake.
For openssl in windows app you need to add
QT += core gui network
win32{
LIBS += -LC:/OpenSSL-Win32/lib -lubsec
INCLUDEPATH += C:/OpenSSL-Win32/include
}
to your project file, and replace C:/OpenSSL-Win32/ with your downloaded OpenSSL path.

looking for files in project folder not in the debug/release folder

I'm coding some modern OpenGL which requires external files for the shaders. At this moment, I'm providing the full path of the file inside my application, however, I need to force Qt Creator to look for the files inside the project folder.
As you can see from the perceding image, the files are in the project folder and have been added as DISTFILES in .pro file, however, Qt Creator doesn't search for the files in the project folder. Is there any convenient approach to this problem rather than providing the entire the path as follows.
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "D:\\to the file\\vertexShaderSource.vs");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "D:\\to the file\\fragmentShaderSource.fs");

QMediaPlayer, QSound class - unable to #Include files (files are there)

I have been trying to play a simple audio file using:
http://qt-project.org/doc/qt-5/qmediaplayer.html and
http://qt-project.org/doc/qt-5/QSound.html
I am using Qt 5.3 but the problem is, when I type #include QMediaPlayer or QSound, QtCreator keeps underlining them with red because it can't find them. The thing is, the files are there. They are located in QtMultimedia/ and I have seen them. When I try to prefix QtMultimedia/QMediaPlayer or QtMultimedia/qmediaplayer.h it still can't find them.
If I type #include "QtMultimedia/" the auto-complete drop down in the text editor only shows QtMultimedia/QtMultimediaDepends. Normally you get a list of all the files in the folder but not with this one.
Looks like there is something wrong with the paths. How do I fix them? I am using a Windows machine.
Edit: I have included project settings - see screenshots.
I think, after you modified your .pro file, You didn't run the qmake.
build-> run qmake
qmake will generate the makefile you need to build the src code.
if you only change the .pro file, but didn't run it. Nothing is changed.
and that is reason, you create a new project and it is working.
Ok I figured it out. It's a problem with my Qt version.
My Qt 5.3 version is a custom compiled one. When I select the included version that came with the installer, QSound is found. When I change the kit to the custom compiled one, the file cannot be found. I must have forgotten to include some sort of multimedia option to the configure script during compilation.
Now the question is what is the missing option, I will need to do more reading...
Of course, comments and suggestions are welcome :)
You have to add the word 'multimedia' in your .pro file:
QT += core GUI
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets multimedia

Configuring Qt Creator with executable and DLL project

I am new to QT Creator coming from Visual Studio. I have a session with two projects in it. One is a DLL with some classes that I intend to use for other purposes. The other is an executable console app that uses some of the classes from the DLL.
I currently have these two projects side by side in QT Creator. I can include the header files from the DLL in my EXE project using relative paths "../MyPrject/header.h". But how do I get QT Creator to link and then copy the DLL into the executable debug folder for debugging.
Am I doing this all wrong? Is there a better way? If it includes adding code to the .pro file, please include a link so that I can learn more.
You should make some dependencies between this projects.
opening both projects - you have done.
on editor view, right click on exe-project and select add library...
follow creator hints to add it.
2nd option: you can make subprojects. follow QtCreator: Creating Projects from documentation (help view in Qt Creator)
GwyenBleidD provided a good starting point for including DLLs.
I however, have made a habit out of modifying the .pro file directly here and honestly I prefer to modify the .pro file in the event that something goes haywire.
Suppose I wanted to use the winsock DLL.
In the .pro file, I'd first specify the .dll's corresponding .lib file:
# WinSock2 library (ws2_32.lib file)
LIBS += -lws2_32
# Path to the WinSock2 library
LIBS += -L"c:/mylibraries/"
Additionally, you'll need to specify the include path to the header files here:
INCLUDEPATH += "c:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/INCLUDE"
Thirdly, in my code I'd have to make sure to include the headers for it:
// I ASSUME it'll be found under something like the
// Visual Studio/VC/INCLUDE directory mentioned above.
#include <winsock2.h>
Lastly, you need to ensure that your application can find the .dll file, typically pointed to using the %PATH% environment variable.
With regards to your setup, I'd make sure that your sub-projects are configured so that the library compiles FIRST (obviously). And then ensure that the LIBS variable in your .Pro project points correctly to your .lib destination according to the build configuration (debug|release).
Qt's PRO (qmake) isn't as terrible as some make it out to be. Just give it a solid half-hour to an hour and you'll get the hang of it. I assume though that you have a solid understanding of libs and DLLs and what not.
http://qt-project.org/doc/qt-5.0/qtdoc/qmake-manual.html
The right way is to switch on CMake based project and keep exe and dll within one root project. The main benefit of this decision is IDE independent approach: you can use Qt Creator, CLion, Visual Studio without any changes in project definition. As the start point consider to see the example project https://github.com/anatoly-spb/cmake_exe_dll