Using libraries with C++ and Qt - c++

I am trying to use a library to generate barcode images inside a Qt/C++ app.
I've been looking at ZINT and ZXING, but I don't understand how would I use them inside my code.
If I download and compile the library, how do I call it afterwards? Could I set up the Qt makefile to compile it for the desired platform?
Thanks, and sorry for the broad question, I'm not very experienced in C++ projects.

In QT you usually include 3rd party libraries through the .pro file. Use the LIBS variable to tell QT where to find it.
Example lines from the QT docs:
TARGET = MyQtApp
TEMPLATE = app
INCLUDEPATH += 3rdparty/CatWhisperer/include
SOURCES += src/main.cpp
LIBS += -L"3rdparty/CatWhisperer/lib" -lCatWhisperer

If you're using Qt Creator, you can add libraries to your .pro by using a wizard provided by the IDE. Right-click your project in the project browser, click Add library... and, then, follow the wizard.
More details here: http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

Related

Qt-Based C++ Project Works If Run From cmd.exe But Not From Within Qt Creator

I am writing a very minimal C/C++ Qt-based application for Windows (only Windows -- not cross platform at all) that uses a VISA library (visa64.dll) to talk to some external hardware. That library, in turn, uses some other libraries:
(screenshot from Dependency Walker a.k.a. depends.exe)
Originally I wrote it in Visual Studio and it worked great. Then I ported it to Qt Creator (using Qt5, w/ MSVC 2015 Visual C++ toolchain) and I got runtime errors. It knows where to find the external header files, so I think my INCLUDEPATH is right, and it builds fine so I think the LIBS variables in my .pro file are right, which is to say it can find the .lib files it needs. However, the first API I call from this external library (viOpenDefaultRM) returns the following error: VI_ERROR_LIBRARY_NFOUND. This happens whether I make a debug build or a release build, and whether or not I am running it with a debugger. As long as I run the program from within Qt Creator, it gets runtime errors.
Here is my .pro file:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'
LIBS += -L$$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Lib_x64/msc/' -lvisa64
INCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'
DEPENDPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'
The paths that end with /Include have header (.h) files (it's a C library), and the path that ends with /msc has a .lib file. The .lib files are not static libraries, they are the interface files for some corresponding DLLs. Those DLL files are in C:\System32. There are also 32-bit versions in C:\SysWOW64. They may also exist elsewhere but if they do I am not aware of it.
Now, if I run it from cmd.exe it works fine. That is, if I open a cmd.exe terminal window and navigate to my Qt project's build directory (c:\blah\blah\blah\obj\debug\) and run the executable from cmd.exe, I get no runtime errors. It can connect to the external hardware, talk to it, all good things are happening, much rejoicing.
I've done a decent amount of searching and researching about this problem, and I am somewhat cursed by the fact that most people have the opposite problem, which means that problem (the opposite one of mine) is what turns up in Google/DuckDuckGo/StackOverflow/forum.qt.io/doc.qt.io searches. That problem usually has to do with missing/misplaced Qt libraries. Here is an example. The answer to this question usually ends up with a link to a page on how to deploy Qt projects for Windows, e.g. this article.
Also I've read this article from Qt on how to add libraries to your project, and it didn't help me out, but I could be missing something and/or doing it wrong.
This might be something really dumb I'm missing and frankly I hope it is. Thanks*10^6.
TL;DR: The kit I was using to compile in Qt Creator had a different PATH set than my system PATH. To fix this, I did echo %PATH in cmd.exe and copied all the stuff that had to do with the drivers I'm trying to use into the PATH for the kit I'm using in Qt Creator. More details below.
I got this to work this morning. As suggested by #adrien-lerevat, when run from within Qt Creator, my executable couldn't find some DLLs it needed. The long and short of it is that I was defining a PATH in my kit (a "kit" in Qt is basically a compiler, a debugger, and some environment variables) that was different from, and not a superset of, my normal system path. I had inherited this kit for other purposes, you see, from other projects, and I didn't realize a PATH could be set in it, or that I was setting one. So to find the PATH I was setting for Qt Creator, I went to the Tools dropdown and selected Options..., then Build & Run, then Kits. Then click on the kit you are using to edit it. As such:
That should give you a list of stuff, one thing of which is called Environment. That should have a Change... button you can press:
which should open a new window with all your environment stuff:
(screenshot is from after I fixed the problem)
This is where I found PATH, as well as some library and include paths that were worth knowing about. So now that I knew what my Qt Creator PATH was, I opened cmd.exe and typed the command echo %PATH% to find out what my system PATH was. I grabbed everything that had to do with these VISA drivers I'm using (basically anything with VISA and/or IVI Foundation in the path) and pasted them into my PATH in Qt Creator. This was the list of stuff I pasted in there to make it work:
C:\WINDOWS\system32;C:\Program Files\IVI Foundation\VISA\Win64\ktvisa;C:\Program Files\IVI Foundation\VISA\Win64\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\winnt\agvisa;C:\Program Files\Keysight\IO Libraries Suite\bin;C:\Program Files (x86)\Keysight\IO Libraries Suite\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\ktvisa;C:\Program Files (x86)\IVI Foundation\IVI\bin;C:\Program Files\IVI Foundation\IVI\bin;
I added c:\system32 because I know that's where visa64.dll is, which is at least one top-level DLL I know I need. Oddly enough, though, when I added just c:\system32 without all the VISA and IVI Foundation stuff, that didn't work. So, I don't know if everything I added to my Qt Creator path was necessary, as I've just come upon this solution, but once I pare down the list to find out what all I actually needed I will add that information here. Just in case anyone else ever comes across this problem or is curious. And for the sake of completeness I suppose. Okay thanks everyone ;)

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

Adding QtSoap Reference to Qt Creator

i have built the dll its QtSolutions_SOAP-2.7d.dll and now i want to reference that in Qt Creator how do i do that ?
thanks
In the QtSolutions package you also have an examples directory with several examples.
Basically you just have to include src/qtsoap.pri in the .pro file of your project.
And the dll should be in your PATH.

Qt Creator / QMake Linker Libraries

I'm using SFML, and I want to use Qt Creator in conjunction with it. When I'm compiling manually, I supply the following arguments to the linker -lsfmlsystem -lsfmlwindow.
How do I do this if I'm using Qt Creator and (I think) QMake?
Just add
LIBS += -L/path/to/sfml -lsfmlsystem -lsfmlwindow
to the .pro file.
You can open project files with QtCreator from the Projects view of the sidebar or searching for it via Ctrl-K. (BTW, the sidebar is not the list of icons down the left, it's the pane to the right of that which can be shown/hidden with Alt-0.)