[CMake] Qt4

John Biddiscombe biddisco at cscs.ch
Thu Jul 21 03:18:19 EDT 2005


For people searching the archives, I am happy using Qt4 now, I've taken 
Filipe's snippets and created these macros in my CMakeLists file. Two 
macro's follow, then further below, the code I use when adding the 
sources to the build. I'll probably tweak the macros to create a list 
instead of doing them one at a time (but since I only have a couple of 
files in the project so far...), and remove the extension param since 
it's always the same,

Many thanks for help. I like these macros more than the old cmake qt 
wrap commands, because I know exactly what they're doing. I am having no 
trouble with cmake regenerating files when requred, when I modify the ui 
file etc, all is regenerated as expected.

Note that a variable is returned from the macro with a value set. (This 
feature seems to be flakey, so any improvements/suggestions welcome)

#-----------------------------------------------
# Macros for generating Qt wrapped files
#-----------------------------------------------
MACRO(WRAP_QT_CPP moc_file header source_ext)
  GET_FILENAME_COMPONENT(basename ${header} NAME_WE)
  GET_FILENAME_COMPONENT(path ${header} PATH)
  SET(${moc_file} ${path}/${basename}.moc.${source_ext})

  ADD_CUSTOM_COMMAND(
    OUTPUT   ${${moc_file}}
    COMMAND  ${QT_MOC_EXECUTABLE}
    ARGS     ${header} -o ${${moc_file}}
    DEPENDS  ${QT_MOC_EXECUTABLE} ${header}
    COMMENT  "Qt Wrap Files"
  )
ENDMACRO(WRAP_QT_CPP)

MACRO(WRAP_QT_UI ui_header ui_file header_ext)
  GET_FILENAME_COMPONENT(basename ${ui_file} NAME_WE)
  SET(${ui_header} ${CMAKE_CURRENT_BINARY_DIR}/ui_${basename}.${header_ext})

  ADD_CUSTOM_COMMAND(
    OUTPUT  ${${ui_header}}
    COMMAND ${QT_UIC_EXECUTABLE}
    ARGS    ${ui_file} -o ${${ui_header}}
    DEPENDS ${ui_file}
  )
ENDMACRO(WRAP_QT_UI)

#-----------------------------------------------
# Create rules for generated Qt wrapped files
#-----------------------------------------------
WRAP_QT_CPP(moc_mainwindow "${CMAKE_CURRENT_SOURCE_DIR}/mainwindow.h" "cxx")
WRAP_QT_CPP(moc_glwidget   
"${CMAKE_CURRENT_SOURCE_DIR}/widgets/GlWidget.h" "cxx")
WRAP_QT_CPP(moc_dockwbase  
"${CMAKE_CURRENT_SOURCE_DIR}/dialogs/DockWidgetBase.h" "cxx")
WRAP_QT_UI( ui_mainwindow  "${CMAKE_CURRENT_SOURCE_DIR}/mainwindow.ui" "h")

#-----------------------------------------------
# Include the widgets & dialogs subdirs
# Include the binary dir for Qt generated files
#-----------------------------------------------
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/widgets)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/dialogs)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

#-----------------------------------------------
# Add all sources to project
#-----------------------------------------------
SET( molekel_SRCS
  main.cpp
  mainwindow.cxx
  widgets/GlWidget.cpp
  dialogs/DockWidgetBase.cpp
  ${moc_mainwindow}
  ${moc_glwidget}
  ${moc_dockwbase}
  ${ui_mainwindow}
)




More information about the CMake mailing list