PDA

View Full Version : Creating a new c++ class with QObject as a base class



toufic.dbouk
26th September 2013, 20:55
Greetings everyone,
so i was working on a project and i had a class sub-classing QObject added it as follows:
right click on the project in Qt creator >> add new >> C++ class >> set the base class to QObject. And in my .h class it automatically adds
#include <QObject>.
so for a couple of weeks everything was working great but today i was building the project and it just gave an error : C:\X\Examplethread.h:4: error: C1083: Cannot open include file: 'QObject': No such file or directory.
cleaned the project , ran qmake , built it again and compiled in debug mood and the error kept. But when removing that automatically added include line from the .hpp class:
#include <QObject> the problem is actually solved...
i found this a bit odd... so any explanation or help to make me understand what is going on is helpful.
Thanks in Advance.

ChrisW67
26th September 2013, 21:23
You are compiling a source file that includes "Examplethread.h" without the relevant Qt include paths set. Beyond that we have no information to work on.

toufic.dbouk
26th September 2013, 21:34
You are compiling a source file that includes "Examplethread.h" without the relevant Qt include paths set.
i dont think my question points this issue.
i said i have a class X with a Base class of QObject. when i created this C++ class , the line #include <QObject> was added in the X.h class.
the X.cpp class has the line #include "X.h".
the project worked without any errors for almost 3 weeks. Now when i compiled it again it gave me that error : Cannot open include file: 'QObject': No such file or directory.. But when i deleted the #include <QObject> from X.h the problem was solved and the project worked great.
So my question is why did this happen ?

ChrisW67
26th September 2013, 21:54
i dont think my question points this issue.
That is precisely the error message you gave us.

C:\X\Examplethread.h:4: error: C1083: Cannot open include file: 'QObject': No such file or directory.
When attempting to compile "Examplethread.h" (included by some unspecified source file, let's call it xyzzy.cpp) the compiler could not locate QObject required at line 4. The most likely reason that it could not locate the file is that xyzzy.cpp is being compiled without the include paths for Qt typically arranged by qmake when it sees "QT += core" in the pro file. Other possibilities include, you have moved Qt and not rerun qmake for your project so that the generated Makefiles contain incorrect paths, your Qt install has been corrupted etc.

Look at the actual, full compiler command being executed.

toufic.dbouk
26th September 2013, 22:18
c:\X\namequerythread.h(4) : fatal error C1083: Cannot open include file: 'QObject': No such file or directory
c:\X\namequerythread.h(4) : fatal error C1083: Cannot open include file: 'QObject': No such file or directory
jom: C:\build-X-Desktop_Qt_5_1_1_MSVC2012_64bit-Debug\Makefile.Debug [debug\main.obj] Error 2
cl -c -nologo -Zm200 -Zc:wchar_t -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc -DUNICODE -DWIN32 -DWIN64 -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude\QtWidgets" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude\QtSql" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude\QtGui" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude\QtANGLE" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\inc lude\QtCore" -I"debug" -I"." -I"." -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\mks pecs\win32-msvc2012" -Fodebug\ @C:\Users\Toufic\AppData\Local\Temp\moc_plates.obj .6016.63.jom
jom: C:\build-X-Desktop_Qt_5_1_1_MSVC2012_64bit-Debug\Makefile.Debug [debug\moc_plates.obj] Error 2
moc_plates.cpp
c:\X\Examplethread.h(4) : fatal error C1083: Cannot open include file: 'QObject': No such file or directory
jom: C:\build-X-Desktop_Qt_5_1_1_MSVC2012_64bit-Debug\Makefile.Debug [debug\plates.obj] Error 2
jom: C:\build-X-Desktop_Qt_5_1_1_MSVC2012_64bit-Debug\Makefile.Debug [debug\Examplethread.h.obj] Error 2
jom: C:\build-X-Desktop_Qt_5_1_1_MSVC2012_64bit-Debug\Makefile [debug] Error 2
23:57:56: The process "C:\Qt\Qt5.1.1\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project X (kit: Desktop Qt 5.1.1 MSVC2012 64bit)
When executing step 'Make'

just a note here , the project works fine when i delete the #include <QObject> from the .h class.
i dont get how the problem started.
didnt move Qt , the Qt install is fine since the project compiles and runs without errors after deleting the #include <QObject> from the .h class.
i ran a registry cleaner setup and didnt open the project for a couple of days , when i did open it the problem appeared.

amleto
26th September 2013, 23:29
here's a tip: don't use relative includes for library paths.

toufic.dbouk
26th September 2013, 23:35
here's a tip: don't use relative includes for library paths.
thats the thing , the #include <QObject> is automatically added when i create the C++ class with Base class QObject.
Qt is creating it and adding this specific line , it was all working fine( 3 weeks ) but suddenly it stopped and gave that error so i had to delete the line #include <QObject>.
why is this happening ?

ChrisW67
26th September 2013, 23:54
Ultimately, if the cpp file compiles without the <QObject> include in that header then you have no problem. It clearly was not required to compile.

The compiler is not lying. It cannot find that file, which means that the include paths it has to work with are not correct. The generated command uses relative paths generated by qmake so moving the shadow build directory without rerunning qmake can cause issues like this.

Given that the affected cpp file is a moc generated file, moc_plates.cpp, you should ensure that qmake and moc are rerun. This is essential if you have added or removed a Q_OBJECT macro (it should be in your QObject subclass).


@amleto: The relative paths are generated by qmake.

toufic.dbouk
27th September 2013, 00:07
Alright thanks for the explanation ChrisW67.
But i was wondering why was it working before even with the include <QObject>?
Ill make sure i run qmake and moc again.
I didnt edit the Q_OBJECT macro , i just left the project for a couple of days and the problem appeared suddenly.
If the problem persists ill reinstall Qt because its not normal to suddenly show such an error when it was working fine for almost 3 weeks. Just doesnt sound right.
Best Regards.

ChrisW67
27th September 2013, 01:38
Why don't you manually find where the file "QObject" exists and see if the relative path from the shadow build area is correct.

Really, you first port of call is to delete the shadow build directory completely and rebuild the application.

toufic.dbouk
27th September 2013, 12:21
Really, you first port of call is to delete the shadow build directory completely and rebuild the application.
That's what i did before posting the thread, but it didn't help.
Thanks for the tips. But its just weird to stop working without any changes...
Anyway i will just keep it without the #include <QObject> in the .h class and if any further complications happens i will re install Qt
Regards.

amleto
30th September 2013, 23:56
thats the thing , the #include <QObject> is automatically added when i create the C++ class with Base class QObject.
Qt is creating it and adding this specific line , it was all working fine( 3 weeks ) but suddenly it stopped and gave that error so i had to delete the line #include <QObject>.
why is this happening ?

I'm talking about your project settings, not source code, but Chris says it is auto generated by qmake