Results 1 to 3 of 3

Thread: using QT unit testing with CMake

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X

    Question using QT unit testing with CMake

    I am trying to build my QT test unit appl (myTest.cpp) using CMake. The test appl compiles/runs fine if I use qmake.
    However, when I use CMake it complains about line at end of file: include "myTest.moc".
    I needed this line when I use qmake. So I tried:
    a). Removing line (include "myTest.moc")
    b). Adding a qt4_wrap_cpp to create myTest.moc.
    Now, I get a bunch of moc_myTest.cxx errors.
    This unit test file doesn't have a header file. Any ideas what I am missing.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QT unit testing with CMake

    I use the following macros to mimic qmake behavior in cmake and so far it works fine :
    Qt Code:
    1. # Smarter versions of moc wrapping macros that mimic qmake behavior
    2. function(qt4_wrap_hdrs _moc_srcs)
    3. qt4_get_moc_flags(_moc_incs)
    4. set(_mocs)
    5. foreach(_current_file ${ARGN})
    6. get_filename_component(_abs_file ${_current_file} ABSOLUTE)
    7. if(EXISTS ${_abs_file})
    8. file(READ ${_abs_file} _contents)
    9. get_filename_component(_basename ${_abs_file} NAME_WE)
    10. string(REGEX MATCH "Q_OBJECT" _match "${_contents}")
    11. if(_match)
    12. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/moc_${_basename}.cpp")
    13. qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
    14. macro_add_file_dependencies(${_abs_file} ${_moc})
    15. list(APPEND _mocs ${_moc})
    16. endif(_match)
    17. endif(EXISTS ${_abs_file})
    18. endforeach (_current_file)
    19. set(${_moc_srcs} ${_mocs} PARENT_SCOPE)
    20. endfunction(qt4_wrap_hdrs)
    21.  
    22. function(qt4_wrap_srcs)
    23. qt4_get_moc_flags(_moc_incs)
    24. foreach(_current_file ${ARGN})
    25. get_filename_component(_abs_file ${_current_file} ABSOLUTE)
    26. if(EXISTS ${_abs_file})
    27. file(READ ${_abs_file} _contents)
    28. get_filename_component(_abs_path ${_abs_file} PATH)
    29. get_filename_component(_basename ${_abs_file} NAME_WE)
    30. string(REGEX MATCH "# *include +\"${_basename}\\.moc\"" _match "${_contents}")
    31. if(_match)
    32. string(REGEX MATCH "[^ <\"]+\\.moc" _moc "${_match}")
    33. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_moc}")
    34. qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
    35. macro_add_file_dependencies(${_abs_file} ${_moc})
    36. endif(_match)
    37. endif(EXISTS ${_abs_file})
    38. endforeach(_current_file)
    39. endfunction(qt4_wrap_srcs)
    To copy to clipboard, switch view to plain text mode 

    Typical usage (assuming _hdrs, _srcs, _forms and _rsrcs variable are defined and hold respectively the HEADERS, SOURCES, FORMS and RESOURCES of your qmake project) :
    Qt Code:
    1. qt4_wrap_hdrs(_moc_srcs ${_hdrs})
    2. qt4_wrap_srcs(${_srcs})
    3. qt4_wrap_ui(_forms_hdrs ${_forms})
    4. qt4_add_resources(_rsrcs_srcs ${_rsrcs})
    5. add_library(${_name} ${_srcs} ${_moc_srcs} ${_rsrcs_srcs})
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

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

    carpevitam (3rd January 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X

    Default Re: using QT unit testing with CMake

    Thank you so much for your help. I don't understand everything your macro does (yet),
    but I believe it fixed my building issue. I have much to learn. Thanks a lot.

Similar Threads

  1. Testing Qt-Software with a GUI Testing Tool
    By nightghost in forum Qt Programming
    Replies: 8
    Last Post: 22nd February 2012, 07:43
  2. Qt unit testing
    By chandan in forum Newbie
    Replies: 3
    Last Post: 13th July 2010, 19:20
  3. Problem when linking a Qt Unit Test with CMake
    By NoRulez in forum Qt Programming
    Replies: 6
    Last Post: 10th June 2010, 17:30
  4. Unit testing Gui applications
    By yop in forum Qt Programming
    Replies: 0
    Last Post: 17th May 2009, 15:40
  5. QTest Unit Testing
    By bothapn in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2006, 22:11

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.