undefined reference linker error with signals and slots - qt

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.

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.

Compile error in Qt/C++

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.

Can I get qmake -project to add LIBS += .... to my .pro file?

I have a project that uses Qt. So I have "qmake" make my Makefile from the .pro file. But Qmake can also make that .pro file: qmake -project . This worked until I needed to add an external extra library to my project.
I get lots of hits on google that tell me to add LIBS += ... to my project file, but I want to tell qmake -project something that causes it to add it for me. In effect of course I'll be doing it myself, but I don't think that it's proper that I am editing the generated project file.
If for example I add files to the project directory, I'll have to recreate it and add in the library again, or I'll have to manually add the files to the (almost completely computer-generated) project file. I'm now using a script to auto-generate the project file, and then add in the LIBS += directive, but is there a proper way to do this?
When you are developing without the Qt Creator IDE, unless the IDE includes by itself some automatic utilities, you must edit manually the .pro configuration file.
The generated .pro file is a skeleton file which YOU must fill in with the libraries that you need, then the qmake system figures out the other dependencies and compiles your project. It is a essentially a much better version of pkg-config of gtk + Makefiles.
When you add more source and resource files to your project then manually you must add them to the .pro file.
Example:
QT += core gui
TARGET = qtcp1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
miwidget1.cpp \
lcdrange.cpp
HEADERS += mainwindow.h \
miwidget1.h \
lcdrange.h
FORMS += mainwindow.u
Alternately, you can issue qmake -project over and over again, but this can cause some unforseen accidents, since it includes everything that is in the current directory at the time, including the pre-processed files for conversion to standard C++ from QT dialect. This intermediate files must be erased (cleaned), before the remaking the project and the next make or can lead to tricky problems.
Using the official and free QT Creator IDE takes away most of this burden by adding automatically the new data to the .pro file and cleaning loose ends. Some other IDEs like Code::BLocks and Codelite provide some facilities for QT, but not to the level of QT creator. Some prefer to edit the .pro themselves for custom reasons, other like more other styles of IDEs, like Eclipse.
You should test the waters and decide by yourself what fits best to your needs.
ReEdited, to clarify a few things.

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?

Resources