Results 1 to 7 of 7

Thread: Why does qmake/moc only process header files?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Why does qmake/moc only process header files?

    I was wondering, why qmake/moc only processes header (*.h) files and not source (*.cpp) files?
    I just wonted to declare a class locally in a source module and discovered, that no moc files have been generated.
    Why this restriction and how should it be worked around?
    Thank you,
    Dieter

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why does qmake/moc only process header files?

    It only looks at *.h files because that is where classes are usually defined. The work around is to define your "private" class in a .h file.

  3. The following user says thank you to pitonyak for this useful post:

    doberkofler (2nd March 2010)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why does qmake/moc only process header files?

    If you want a private class that is local to a file, then create a private header file only included by that source file. For example, myfile_internal.h and define your classes there.

  5. The following user says thank you to squidge for this useful post:

    doberkofler (2nd March 2010)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does qmake/moc only process header files?

    There is a better way. If your implementation file that contains a local class is called xyz.cpp then write this under the class declaration:
    Qt Code:
    1. #include "xyz.moc"
    To copy to clipboard, switch view to plain text mode 
    This instructs qmake to force running moc on xyz.cpp. Remember to rerun qmake after you add this statement.

    Qt Code:
    1. class MyLocalClass : public QObject {
    2. Q_OBJECT
    3. public:
    4. // ...
    5. };
    6.  
    7. #include "myfile.moc"
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #5
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Why does qmake/moc only process header files?

    I'm not sure if I understand you correctly. Does the include xyz.moc have to to exist and what should it contain?

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why does qmake/moc only process header files?

    I believe the only reason it is there is that when the makefile is rebuilt it will automatically create a rule to parse the cpp file using MOC, as qmake will parse cpp files for #include lines for dependancy purposes.

    Some people also state that doing so can reduce compilation time: http://edu.kde.org/development/tips....emyfilemoccode
    There some discussion about it on the Qt interest archive: http://lists.trolltech.com/qt-intere...ad00793-0.html
    And it seems to be used more in KDE: http://www.kdevelop.org/3.0/doc/tutorial_settings/#p1

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does qmake/moc only process header files?

    For me it's just a matter of convinience that I don't have to create separate header files just to declare local classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 8th April 2010, 00:09
  2. header files problems
    By dognzhe in forum General Programming
    Replies: 1
    Last Post: 18th May 2009, 09:00
  3. qmake can't process project file on windows
    By s-troz in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2008, 15:51
  4. header files not getting included
    By nimmyj in forum Installation and Deployment
    Replies: 1
    Last Post: 19th December 2006, 07:18
  5. qt3 themes example: missing header files?
    By nikita in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2006, 13:12

Tags for this Thread

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.