QTCreator and VisualStudio Code compilation with CMake - qt

I have a problem concerning the compilation of a program. I don't know why the same CMakeLists.txt is compiling in QTcreator and not in Visual Studio. I am using the exact same compiler which is MinGW for Windows. I have read the documentation, it compiles well EXCEPT if use a translation file untitled_fr_FR.ts.
Please see the CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
#---------------------------------------------------------------------
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\\Qt\\5.15.0\\mingw81_64")
#---------------------------------------------------------------------
project(untitled 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(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)
set(TS_FILES untitled_fr_FR.ts)
add_executable(untitled
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
${TS_FILES}
)
target_link_libraries(untitled PRIVATE Qt5::Widgets)
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
This code works well in QTCreator but gives me this error in VSCode:
[main] Building folder: untitled
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/remi/Desktop/ok/untitled/build --config Debug --target all -- -j 6
[build] [ 16%] Automatic MOC and UIC for target untitled
[build] [ 16%] Built target untitled_autogen
[build] mingw32-make.exe[2]: *** No rule to make target '../', needed by '../untitled_fr_FR.ts'. Stop.
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/untitled.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:103: all] Error 2
[build] Build finished with exit code 2
***** No rule to make target '../', needed by '../untitled_fr_FR.ts'. Stop.**
How can I solve this one? How can I figure out what QTCreator is changing in the CMake file/env to compile the .ts file?
Thanks a lot for your help, hope I'm clear and I don't bother you.

The best way to include translation is not using this method. The CMakeLists.txt to compile the program is :
cmake_minimum_required(VERSION 3.5)
#---------------------------------------------------------------------
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\\Qt\\5.15.0\\mingw81_64")
#---------------------------------------------------------------------
project(untitled 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(Qt5 COMPONENTS Widgets REQUIRED)
add_executable(untitled
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
target_link_libraries(untitled PRIVATE Qt5::Widgets)
If you want to compile it (out of the Qt environment you will need to use this one :
cmake_minimum_required(VERSION 3.5)
#---------------------------------------------------------------------
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\\Qt\\5.15.0\\mingw81_64")
#---------------------------------------------------------------------
project(untitled 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(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)
set(TS_FILES mainwindow.ts)
add_executable(untitled
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
${TS_FILES}
)
target_link_libraries(untitled PRIVATE Qt5::Widgets)
qt5_create_translation(QM_FILES ${TS_FILES})
And rename untitled_fr_FR.ts in mainwindow.ts

Related

CMake, QT Creator - projects bar doesn't display folders

I created a QT project with QMake. I have two files. somewidget.h and somewidget.cpp in the same folder as .pro file. Then I created a folder SomeFolder and here I created two files someclass.h and someclass.cpp. I see the Project panel on the sidebar:
I see the files in the folder, that I created.
Then I did the same, but with CMake:
But QT Creator doesn't display my folder. There is just a list of header and source files. I deleted all QT configs, reinstalled QT Creator, but nothing has changed. How to fix it?
P.S. I didn't try use add_subdirectory, but anyway I don't want to create CMakeLists in each folder. I use Qt Creator 4.11.0 Based on Qt 5.12.8 (GCC 9.3.0, 64 bit)
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(testing LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Network REQUIRED)
add_executable(${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/main.cpp
${PROJECT_SOURCE_DIR}/somewidget.h
${PROJECT_SOURCE_DIR}/somewidget.cpp
${PROJECT_SOURCE_DIR}/somefolder/someclass.h
${PROJECT_SOURCE_DIR}/somefolder/someclass.cpp
)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets Qt5::Network)
There was a bug in QT Creator 4.11.0. The latest version 4.13.0 doesn't have this problem.

How to add new C++ header and source files in Qt Creator with CMake build?

I'am currently looking for a way to add new C++ header and source file from the QT creator GUI Application with CMake builder. The issue is CMakeLists.txt file is not include those files. I m stuck in there.
This is the CMakeLists.txt file
cmake_minimum_required(VERSION 3.5)
project(MyTest 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(Qt5 COMPONENTS Widgets REQUIRED)
if(ANDROID)
add_library(MyTest SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(MyTest
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
endif()
target_link_libraries(MyTest PRIVATE Qt5::Widgets)
I will then go to QT creator and create a new C++ file. (This file will create seperate header(.h) and source(.cpp) files. If the file name is MyCustom, then it will create mycustom.h & mycustom.cpp files. CMakeLists file is not automatically added those into the add_executable() section. I have to manually add them to add_executable() to include them. Is that the way QT creator CMakeLists will work or is there another way ?
Yes , As of version Qt Creator 4.10.2 . When you add new files it were not added to CMakeLists.txt by default. But you can make work around by using CMake file command.
Move your source and header files to a new subfolder ( sources )
Edit your CMakeLists.txt to include that.
example)
file(GLOB SRCS "${CMAKE_SOURCE_DIR}/sources/*.cpp")
add_executable(MyTest ${SRCS}

CMake Qt UIC failed

I'm currently moving my project from QMake to CMake and I'm facing a problem with Qt UIC which try to process an ui file that does not exist instead of the actual file I want him to process.
I have the following architecture
.
|___ CMakeLists.txt
|___ MyProject.pro
|___ mainwindow.ui
|___ resource.qrc
|___ source
| |___ mainwindow.cpp
| |___ *.cpp
|___ include
| |___ mainwindow.h
| |___ *.h
And here is my cmake
cmake_minimum_required(VERSION 3.2)
# Project name
project(project)
# Tell CMake to compile with C++11
set(CMAKE_CXX_STANDARD 11)
# Tell CMake to run moc when needed.
set(CMAKE_AUTOMOC ON)
# Tell CMake to run uic when needed.
set(CMAKE_AUTOUIC ON)
# Tell CMake to run rcc when needed
set(CMAKE_AUTORCC ON)
# Moc generated files are located in the current dir so we need to tell CMake to look for them.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Find Qt5
find_package(Qt5 COMPONENTS Widgets Core Gui OpenGL REQUIRED)
# Add Qt5 definitions and includes to build libraries.
# Widgets add Widgets Core and Gui
add_definitions(${Qt5Widgets_DEFINITIONS})
include_directories(${Qt5Widgets_INCLUDES})
add_definitions(${Qt5OpenGL_DEFINITIONS})
include_directories(${Qt5OpenGL_INCLUDES})
# Find OpenGL
find_package(OpenGL REQUIRED)
# Set include directories
include_directories(${CMAKE_SOURCE_DIR}/include
# Use Qt5 ressources
set(RESOURCE resources.qrc)
# Use Qt5 ui
set(UI mainwindow.ui)
# Adding sources
set(SOURCES
source/main.cpp
source/mainwindow.cpp
source/octree.cpp
source/mesh.cpp
source/pgm3d.cpp
source/glwidget.cpp
source/camera.cpp
source/scene.cpp
source/light.cpp
source/obj.cpp
source/alignedbox3f.cpp
source/wireboundingbox.cpp
include/mainwindow.h
include/utils.h
include/octree.h
include/mesh.h
include/pgm3d.h
include/glwidget.h
include/camera.h
include/scene.h
include/model3d.h
include/light.h
include/obj.h
include/alignedbox3f.h
include/wireboundingbox.h)
add_executable(${PROJECT_NAME} ${UI} ${RESOURCE} ${SOURCES} )
target_link_libraries(${PROJECT_NAME} ${Qt5Widgets_LIBRARIES} ${Qt5OpenGL_LIBRARIES} ${OPENGL_LIBRARIES})
And I've got the following error:
File '/*/project/source/mainwindow.ui' is not valid
AUTOUIC: error: process for ui_mainwindow.h needed by
"/*/project/source/mainwindow.cpp"
failed:
File '/*/project/source/mainwindow.ui' is not valid
This error is perfectly logical since my source folder does not contain the ui file. I've tried to wrap the ui instead of using autouic and it didn't work either.
The AUTOUIC property of the target (which is set from the variable CMAKE_AUTOUIC when the target is created) defines behavior of generator - when AUTOUIC property is enabled CMake will scan source files for the includes of ui files (like #include "ui_*.h" or #include <ui_*.h>) and will automatically process them with uic tool.
In CMake 3.9 another target property was added AUTOUIC_SEARCH_PATHS. Similar question was already asked here.
Another option for you would be to disable AUTOUIC and use qt5_wrap_ui with full path to the UI file:
set(CMAKE_AUTOUIC OFF)
set(UI ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui)
...
qt5_wrap_ui(UI_HEADERS ${UI})
add_executable(${PROJECT_NAME} ${UI_HEADERS} ${RESOURCE} ${SOURCES} )

How to use a build system other than qmake with a Qt project?

In qtcreator a template qt project has a simple .pro configuration file.
From .pro file qmake utility generates Makefiles each of which contains lots of includes for every qt dependent source file:
release/moc_MainWindow.cpp: src/controller/Controller.hpp \
...
../../../../Qt/5.6/mingw49_32/include/QtWidgets/QMainWindow \
../../../../Qt/5.6/mingw49_32/include/QtWidgets/qmainwindow.h \
... 100 lines here ...
../../../../Qt/5.6/mingw49_32/include/QtWidgets/qpushbutton.h \
../../../../Qt/5.6/mingw49_32/include/QtWidgets/qabstractbutton.h \
src/view/qt/MainWindow.hpp
I have difficulties configuring .pro files so I decided to configure a qt project with another build system: make, automake, cmake for example.
Is there a way to configure any build system automatically for including lots of qt header files? Or to not include them but build qt project without qtcreator?
My question is different from Using Cmake with Qt Creator because i don't need a qt creator to present to solve my problem
My CMake template:
cmake_minimum_required (VERSION 2.8)
project(qttest)
set(PROJECT_VERSION 0.0.1)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -pthread -fno-permissive -pedantic -Wall -Wextra -fPIC")
endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(QT_VERSION_REQ "5.2")
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5DBus ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND SOURCES
)
list(APPEND MAIN_SOURCES
main.cpp
${SOURCES}
)
list(APPEND LIBS
Qt5::Core
Qt5::Quick
Qt5::Widgets
Qt5::DBus
)
add_executable(${PROJECT_NAME} ${MAIN_SOURCES})
target_link_libraries(${PROJECT_NAME} ${LIBS})

No generated moc_* files when cmake with CMAKE_AUTOMOC ON

I have a CMake file which builds a QT project. It looks like:
...
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Widget)
...
However, after cmake this file, no moc_* files generated. Where did I got wrong?

Resources