I have a QLM project MyProject compiled with CMake, for which I wanted to add tests.
I figured I would create another project MyProject_tests, and a top level CMakeLists containing both :
MetaMyProject
|-CMakeLists.txt (1)
|-MyProject
| |-CMakeLists.txt (2)
|-MyProject_test
| |-CMakeLists.txt (3)
First CMakeLists contains
cmake_minimum_required(VERSION 3.3)
project(MetaMyProject CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
add_subdirectory(MyProject)
add_subdirectory(MyProject_Test)
Second CMakeLists is not changed, except I added set_property(TARGET ${PROJECT_NAME} PROPERTY ENABLE_EXPORTS 1) at the end.
Third CMakeLists contains
cmake_minimum_required(VERSION 3.1)
project(MyProject_ContraintsTest)
find_package(Qt6 COMPONENTS QuickTest REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCE_FILES
main.cpp
)
set(HEADER_FILES
)
set(QML_TESTCASE_FILES
testcases/tst_test.qml
)
add_definitions(-DQUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/testcases")
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES} ${QML_TESTCASE_FILES})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::QuickTest)
Now for the component defined in MyProject to be available in MyProject_test, I wanted to add on the last line a PIVATE MyProject. It does not raise warning, but import MyProject does not work then.
What is the correct way to link my test project to the base project in order for all the components to be available in the test project?
ENABLE_EXPORTS enables symbol exports from an executable, these symbols can be used in another library or executable. To be able to use a symbol in your qml, you would need to re-register the exported Qt Class in your MyProject_test/main.cpp using:
qmlRegisterType<MyExportedClass>("MyExportedClass", 1, 0, "MyExportedClass");
Then you would be able to use it with import MyExportedClass 1.0
Related
I have an example ROS project with 1 package dashboard. The CMakeLists.txt of this package is here:
cmake_minimum_required(VERSION 3.14)
project(dashboard VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE on )
set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick QuickControls2 Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick QuickControls2 Widgets REQUIRED)
set(PROJECT_SOURCES
src/main.cpp
)
set(PROJECT_RESOURCES
qml.qrc
imports.qrc
)
list(APPEND QML_IMPORT "${CMAKE_SOURCE_DIR}/")
list(APPEND QML_IMPORT "${CMAKE_SOURCE_DIR}/imports")
list(REMOVE_DUPLICATES QML_IMPORT)
set(QML2_IMPORT_PATH "${QML_IMPORT}" CACHE STRING "" FORCE)
set(QML_IMPORT_PATH "${QML_IMPORT}" CACHE STRING "" FORCE)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME}
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
add_executable(${PROJECT_NAME}
${PROJECT_SOURCES}
${QT_RESOURCES_CPP}
${${PROJECT_NAME}_MOCS}
${${PROJECT_NAME}_QML}
${PROJECT_RESOURCES}
)
endif()
target_compile_definitions(${PROJECT_NAME} PUBLIC SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::QuickControls2
Qt${QT_VERSION_MAJOR}::Widgets
)
set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_import_qml_plugins(${PROJECT_NAME})
qt_finalize_executable(${PROJECT_NAME})
endif()
I have set both QML_IMPORT_PATH and QML_IMPORT_PATH, but still have an issue QML module not found (Themes.MyTheme).
Here is MyTheme qmldir:
module Themes.MyTheme
singleton Theme 1.0 Theme.qml
The project is compiled and launched, but autocomplete don't working and Theme isn't defined.
The question is: how to correctly import module in .qml file in ROS project for everything working?
I have a Qt/QML project using CMake, which contains a sub-directory for the tests.
But every-time I build the project on QtCreator (ctrl+b, or "build project "), every test have is run. My problem is that some of them are linked to graphic event and thus show a windows that will flash on the screen. I would like to run these tests only when I explicitly want.
Is there a way to build without specific tests?
Edit:
Top CmakeList:
cmake_minimum_required(VERSION 3.16)
project(Projet LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 COMPONENTS Core Quick Quick3D REQUIRED)
set(MAIN_FILE src/main.cpp)
set(SOURCE_FILES
[...]
)
set(HEADER_FILES
[...]
)
set(QRC_FILES
[...]
)
# Compile everything except main.cpp in a separate library (needed for unit testing)
set(LIB_NAME ${PROJECT_NAME}_LIB)
add_library(${LIB_NAME} STATIC ${SOURCE_FILES} ${HEADER_FILES} ${QRC_FILES})
target_link_libraries(${LIB_NAME} Qt6::Core Qt6::Quick Qt6::Quick3D)
target_include_directories(${LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/)
# Link the library to the executable
add_executable(${PROJECT_NAME} ${MAIN_FILE})
target_link_libraries(${PROJECT_NAME} ${LIB_NAME})
add_subdirectory(test)
The main folder contains a subfolder test, containing the Test CMakeList:
cmake_minimum_required(VERSION 3.1)
project(Projet_Test)
set(CMAKE_AUTOMOC ON)
set(SOURCE_FILES
[...]
)
set(HEADER_FILES
[...])
set(QML_TESTCASES
[...]
)
find_package(Qt6 COMPONENTS Quick Qml QuickTest REQUIRED)
set(QML_IMPORT_PATH "${QML_IMPORT_PATH};${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "Standard Qt variable")
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES} ${QML_TESTCASES})
# Qt macro where all qml testcase are located
target_compile_definitions(${PROJECT_NAME} PRIVATE -DQUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
add_definitions(-DQML_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Quick Qt6::Quick3D Qt6::QuickTest PRIVATE Projet_LIB)
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
I'm trying to compile qtserialport to my project in QtCreator4.9.2 with ROS plugin in.
Of course, I had follow the doc to install qtserialport to my Ubuntu16.04. And then, I had do some tests like this:
#include "QtSerialPort/qserialport.h"
#include "QtSerialPort/qserialportinfo.h"
...
Q_FOREACH(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
ROS_INFO("Name: %s", info.portName().toStdString().c_str());
ROS_INFO("Description: %s", info.description().toStdString().c_str());
ROS_INFO("Manufacturer: %s", info.manufacturer().toStdString().c_str());
...
}
But, get erros:
undefined reference to 'QSerialPortInfo::availablePorts()'
undefined reference to 'QSerialPortInfo::portName() const'
undefined reference to 'QSerialPortInfo::description() const'
...
undefined reference to 'QSerialPortInfo::~QSerialPortInfo()'
You know, I'm working with CMake, so the official docs about qmake or .pro introductions is useless to me. And I had find some suggestions like this, however, I'm confused with this: how to make the qtserialport as a module?
Anyway, all I want to say is that: how can I compile qtserialport with my ros CMake project? And certainly, it should also work with my QtCreator.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.0)
project(xxx)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS qt_build roscpp)
include_directories(${catkin_INCLUDE_DIRS})
catkin_package()
rosbuild_prepare_qt4(QtCore QtGui QtOpenGL)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} rsources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS include/xxx/*.hpp)
QT4_ADD_RESOURCES(QT_RSOURCES_CPP ${QT_RESOURCES})
QT4_WRAP_CPP(QT_MOC_HPP ${QT_MOC})
set(QT_SOURCES ...)
if(MSVC)
set(...)
endif(MSVC)
add_definitions(...)
include_directories(...)
set(EXTERNAL_LIBRARIES OpenThreads osg osgDB osgViewer osgUtil osgText osgGA pthread lz4 bz2)
add_executable(... ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_MOC_HPP})
target_link_libraries(xxx ${QT_LIBRARIES} ${EXTERNAL_LIBRARIES} ${catkin_LIBRARIES})
install(TARGETS xxx RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
Help!
QtCreator4.9.2 is your IDE's version, not the framework's version you probably are using Qt 5.1x.x version.
If you want to compile out of your IDE this line is essential
set(CMAKE_PREFIX_PATH your/qt/path/which/can/be/obtained)
check this path from QT Creator > Open a Qt Project > Projects option on Left bar > Project Build Option, on CMake settings check cmake_prefix_path
# Before add_executable
find_package(Qt5 COMPONENTS Core SerialPort REQUIRED)
# After add_executable
target_link_libraries(${PROJECT_NAME} PRIVATE
Qt5::Core Qt5::SerialPort)
You also can learn package names under CMake folder of your Qt path.
/your/sys/path/Qt5/lib/cmake
I'm using Qt 5 and I put all my headers in separate include folders, and AUTOMOC is unable to find my headers.
src
|- sim
|- include
|- sim
|- client.h (contains Q_OBJECT)
|- client.cpp
My CMake Code:
set(CMAKE_AUTOMOC ON)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
add_library(client client.cpp)
target_include_directories(client PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(client sim Qt5::Widgets)
This results in undefined references to my signals because the QObject MOC class in client is not being generated. I tried adding this to force it to search for client.h...
#include "moc_client.cpp"
However then I get the error
AutoMoc error
-------------
"/home/peter/projects/smartmouse/smartmouse-simulator/src/sim/client.cpp"
The file includes the moc file "moc_client.cpp", but the header "client.{h,hh,h++,hm,hpp,hxx,in,txx}" could not be found.
I get why this is, because the docs only say it searches the current folder so it's not going to look int the include/sim folder. My question is how do I get AUTOMOC to find include/sim/client.h?
I also tried using qt5_wrap_cpp but it doesn't seem to actually call the moc compiler, it only sets up some information. I tried:
qt5_wrap_cpp(client include/sim/client.h)
But it did not generate any cmake targets as far as I can tell
EDIT:
I've also noticed in the AutogenInfo.cmake file that no header is found: set(AM_HEADERS "")
I just ran into this similar issue and while I'm not satisfied with this answer, it might be helpful.
CMAKE_AUTOMOC is not picking up client.cpp's #include "sim/client.h" but you can manually add it to the target sources:
add_library(client client.cpp include/sim/client.h)
I would like to create an installer for my program, which uses OpenCV and some Qt libraries. I also want to make sure that all dependencies are included in the installer so that anyone could easily install my program without installing OpenCV and/or Qt.
I did some research and it seems that the easiest way to do so would be using Qt Installer Framework. I have the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
# Add folder where are supportive functions
set(CMAKE_AUTOMOC ON)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11)
# Include qt basic functions
include(QtCommon)
# Include qt current directories in order to use promoted objects
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules/app)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules/cores)
# Basic info about project
project(SSAT VERSION 1.0)
# Set PROJECT_VERSION_PATCH and PROJECT_VERSION_TWEAK to 0 if not present, needed by add_project_meta
fix_project_version()
file(GLOB APP_GUI_FILES "modules/app/*.ui")
file(GLOB APP_SOURCE_FILES "modules/app/*.cpp")
file(GLOB CORE_SOURCE_FILES "modules/core/*.cpp")
file(GLOB CORE_INCLUDE_FILES "modules/core/*.h")
file(GLOB CMT_SOURCE_FILES "modules/tracker/*.cpp")
file(GLOB CMT_INCLUDE_FILES "modules/tracker/*.h")
add_project_meta(META_FILES_TO_INCLUDE)
set(RESOURCE_FILES ssat.qrc)
find_package( OpenCV REQUIRED )
find_package(Qt5Widgets REQUIRED)
find_package(OpenCV COMPONENTS opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui REQUIRED)
find_path(OpenCV_INCLUDE_DIR "opencv/cv.h" PATHS "${OpenCV_DIR}" PATH_SUFFIXES "include" DOC "")
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# source_group("UI Files" FILES ${UI_FILES})
add_library(${PROJECT_NAME}_CORE ${CORE_SOURCE_FILES} ${CORE_INCLUDE_FILES})
target_link_libraries(${PROJECT_NAME}_CORE ${OpenCV_LIBS})
target_include_directories(${PROJECT_NAME}_CORE PUBLIC modules/core/)
add_library(${PROJECT_NAME}_CMT ${CMT_SOURCE_FILES} ${CMT_INCLUDE_FILES})
target_link_libraries(${PROJECT_NAME}_CMT ${OpenCV_LIBS})
target_include_directories(${PROJECT_NAME}_CMT PUBLIC modules/tracker/)
add_executable(${PROJECT_NAME} ${OS_BUNDLE} # Expands to WIN32 or MACOS_BUNDLE depending on OS
${APP_GUI_FILES} ${APP_SOURCE_FILES} ${META_FILES_TO_INCLUDE} ${RESOURCE_FILES}
)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_CORE)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_CMT)
target_link_libraries(${PROJECT_NAME} pthread)
target_include_directories(${PROJECT_NAME} PUBLIC ${OpenCV_INCLUDE_DIR})
qt5_use_modules(${PROJECT_NAME} Widgets)
Since Qt Installer Framework demonstrates how to create an an installer from a Qt Project (.pro) instead of a CMake file, I would like to know if it is possible to create an installer from a CMake project in such a way that all its dependencies are included.