Compile error in Qt/C++ - qt

I am trying to compile an example from a library.
I have Qt installed but I think I have to link it and I don't know how.
This is the error:
g++ face_recognition.cpp -o test
In file included from face_recognition.cpp:29:0:
/usr/local/include/openbr/openbr_plugin.h:22:23: fatal error: QDataStream: No such file or directory
#include <QDataStream>
^
compilation terminated.

You can't compile a Qt application directly with g++ because the application must first go through Qt's moc compiler.
If you want to build a Qt application from the cmd-line, make sure you define an appropriate .pro file that specifies the Qt modules and other 3rd party headers/libraries you want might to use. For instance:
QT += core widgets
SOURCES += \
main.cpp
Then invoke qmake on the command line in the same directory as the .pro file to build the appropriate Makefiles, and finally execute make to build the app.

Related

How to use moc in a qmake project that doesn't use Qt modules?

I have a qmake project that includes the needed Qt source files directly, thus I don't need to link with any Qt libraries:
TEMPLATE = app
CONFIG -= qt
Yet the removal of Qt support also forces qmake not to process any headers with moc. Is there a workaround?
Yes:
CONFIG += moc
This causes the mkspecs/features/moc.prf to be included by qmake, adding moc to its repertoire.

Error: undefined reference to QQuickRectangle::QQuickRectangle(QQuickItem*)

I want to use Qt Quick 2 directly from C++ without those qml/javascript stuffs that could slows down the app, but i got compilation error when using QQuickRectangle (Rectangle when used from qml).
In .pro file i have: QT += quick quick-private core-private gui-private declarative-private qml-private
and in .cpp file - #include <private/qquickrectangle_p.h>
I compiled the Qt 5.5 from sources and the command nm -D /usr/local/Qt-5.5.0/lib/libQt5Quick.so.5 | grep "Rectangle"
doesn't print anything related to QQuickRectangle, so it looks that it is not included in the shared library, but i don't know why, because the qquickrectangle.cpp exists in qt sources, so it should be included in the shared library after compilation.

Qt Cannot open include file: 'QPrinter'

I am new to Qt. Downloaded source code for a Qt application of SourceForge, and tried to build and run it. After working through a few similar problems by adding QT += statements to .pro files, I am stuck on this one:
On attempting to build in Qt Creator, I get errors saying
error: C1083: Cannot open include file: 'QPrinter': No such file or directory
I tried adding QT += printsupport to the .pro file, cleaning, and rebuilding, but that gives this error
Error: dependent '..\..\..\..\..\..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\include\QtPrintSupport\qtprintsupportglobal.h' does not exist."
When I go to C:\Qt\Qt5.1.1\5.1.1\msvc2012_64\include\QtPrintSupport, qtprintsupportglobal.h IS THERE!
You have to add QPrinter Support to your project's .pro file:
QT += printsupport
In my case the solution was to
Delete the shadow build directory and build again
after adding printsupport, as #KubaOber suggests in the comments.
Easy mistake to make: After you edit your .pro
QT += printsupport
You have to SAVE the file before your .h will be aware of it.
Because QMake will eventually be dropped in favor of CMake, here's the solution for CMake users:
Pass PrintSupport to the find_package call, to the right side of COMPONENTS, like in this example:
find_package(Qt5 ${QT5_MIN_VERSION} REQUIRED COMPONENTS Core Gui Qml QuickControls2 PrintSupport)

Qt OpenGL compile Static

I wrote a small app with qt and opengl (code) and I want to compile it static now.
So i downloaded Qt library and compile it static using this guide.
I added a line CONFIG += static to the pro file cd to the project location and hit:
make clean
qmake -config release
make
but I am getting several undefined references like:
qpixmapdata_gl.cpp:-1: error: undefined reference to `_imp___Z14qt_defaultDpiYv'
What I am doing wrong? Do I need to add the libs to the pro file? How do I do this and more important what libs should I use?
I tried something like this with no result:
LIBS += C:\Qt\4.7.3\bin\QtOpenGL4.dll
I see that symbol is defined in qfont.cpp, so it should be there. I would try to add:
QT += gui
in your .pro file. And I suppose you need:
QT += core
also.
EDIT: Try also to check that the symbol is included in the libQtGui.lib and the line used to compile.
Are you using the same compiler also? The mangling reported seems that of gcc. Did you compile Qt libraries with mingw or nmake?

undefined reference linker error with signals and slots

Using Qt Creator, I am creating a class with custom slots in Qt:
class CustomEdit : public QTextEdit
{
Q_OBJECT
public:
CustomEdit(QWidget* parent);
public slots:
void onTextChanged ();
};
However, I'm getting this linker error:
undefined reference to 'vtable for CustomEdit'
The documentation says:
if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.
... but it is not obvious what that means.
Is there something I need to add to my class, or to the .pro file?
EDIT -- Here is my .pro file:
QT += network \
script \
webkit \
xml
TARGET = MyApp
TEMPLATE = app
SOURCES += main.cpp \
MainWindow.cpp \
CustomEdit.cpp
HEADERS += MainWindow.h \
CustomEdit.h
FORMS += mainwindow.ui
EDIT:
This question deals with the same problems, but the answers don't make it clear how I can fix this issue in Qt Creator. Do I need to explicitly add the moc-generated .cpp file to my list of files to be linked? That seems like a pain.
Qt creator is an IDE which is used to create your Qt projects. That's all. But qmake allows you to buid the project. From qmake manual,
qmake automates the generation of Makefiles so that only a few lines of information are needed to create each Makefile. qmake generates a Makefile based on the information in a project file. qmake contains additional features to support development with Qt, automatically including build rules for moc and uic.
You don have to include your moc files separately, but you have to run qmake which contains all the information about the required moc files. Take a look at the generated Makefile.debug or Makefile.release after qmake and you can find all the moc files will be included for you.
Now to run qmake in Qt creator, just go to Build->RebuildAll which will run the qmake and all the (moc) files will be generated and linked (in Makefile.debug) as well.
Hopefully this will elimate your undefined reference.

Resources