Results 1 to 12 of 12

Thread: Creating a new c++ class with QObject as a base class

  1. #1
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Creating a new c++ class with QObject as a base class

    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
    Qt Code:
    1. #include <QObject>
    To copy to clipboard, switch view to plain text mode 
    .
    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:
    Qt Code:
    1. #include <QObject>
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a new c++ class with QObject as a base class

    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.

  3. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a new c++ class with QObject as a base class

    Quote Originally Posted by chrisW67
    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 ?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a new c++ class with QObject as a base class

    i dont think my question points this issue.
    That is precisely the error message you gave us.
    Qt Code:
    1. C:\X\Examplethread.h:4: error: C1083: Cannot open include file: 'QObject': No such file or directory.
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a new c++ class with QObject as a base class

    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\i nclude" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\i nclude\QtWidgets" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\i nclude\QtSql" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\i nclude\QtGui" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\i nclude\QtANGLE" -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\i nclude\QtCore" -I"debug" -I"." -I"." -I"..\..\..\..\..\..\Qt\Qt5.1.1\5.1.1\msvc2012_64\m kspecs\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.
    Last edited by toufic.dbouk; 26th September 2013 at 22:25.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a new c++ class with QObject as a base class

    here's a tip: don't use relative includes for library paths.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a new c++ class with QObject as a base class

    Quote Originally Posted by amleto
    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 ?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a new c++ class with QObject as a base class

    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.

  9. The following user says thank you to ChrisW67 for this useful post:

    toufic.dbouk (27th September 2013)

  10. #9
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a new c++ class with QObject as a base class

    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.

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a new c++ class with QObject as a base class

    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.

  12. The following user says thank you to ChrisW67 for this useful post:

    toufic.dbouk (27th September 2013)

  13. #11
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a new c++ class with QObject as a base class

    Quote Originally Posted by ChrisW67
    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.
    Last edited by toufic.dbouk; 27th September 2013 at 13:11.

  14. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a new c++ class with QObject as a base class

    Quote Originally Posted by toufic.dbouk View Post
    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
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 22nd November 2012, 05:03
  2. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  3. Replies: 7
    Last Post: 15th December 2009, 10:01
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.