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.
Related
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
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}
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} )
I use Qt with CMake because CMake integrates with my team's work easier than my own. I have frequently encountered an error along the lines of
ui_*.h: No such file or directory
Usually when my project already has a ui_*.h file to start with it will just modify that .h file. I do use the below commands in my CMake file, so it should be wrapping my .ui file with the appropriate ui_*.h file.
qt4_wrap_ui (mainwindow mainwindow.ui)
target_linked_library (mainwindow ${QT_LIBRARIES})
But sometimes that doesn't work and I have to completely rebuild the entire ui_*.h file. What am I doing wrong?
For anyone having this problem in the future. I pretty much followed the demo here.
http://doc.qt.io/qt-5/cmake-manual.html
Adding, the following line to the CMakeLists.txt should get rid of this problem.
set(CMAKE_AUTOUIC ON)
From the CMake documentation at
https://cmake.org/cmake/help/v3.0/prop_tgt/AUTOUIC.html
AUTOUIC is a boolean specifying whether CMake will handle the Qt uic code generator automatically, i.e. without having to use the QT4_WRAP_UI() or QT5_WRAP_UI() macro. Currently Qt4 and Qt5 are supported.
One small note, this property is available in CMake versions 3.0.2+. So below that, #rbaleksandar's solution should be more appropriate.
Hope that helps.
The quick solution is to use UIC. In bash navigate to the directory containing your *.ui file and run (for the mainwindow.ui example)
uic mainwindow.ui -o ui_mainwindow.h
and then move the newly generated ui_mainwindow.h file to your build directory.
mv ui_mainwindow.h ../build_Qt_4_8_5-Debug/
You shouldn't see the 'No such file or directory' error anymore and can confidently move on to the many other wonderful errors to find in the world of Qt with CMake.
If I remember correctly you actually have to add your UI files to the add_executable(...) like this:
qt4_wrap_ui(UI_HEADERS mainwindow.ui ...) # Add all UI files here like you've done it
...
add_executable(${PROJECT_NAME} ${SRC} ${UI_HEADERS}) # Add them to the executable
...
After all UI files are actually converted to header and source files, which naturally have to be compiled along with the rest of your code.
Building with CMake seems to have an error in
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
remove if in your CMakeLists.txt like this:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
This works in my system with CMake 3.12.1 and Windows 10.
when the ui files are stored in a different location the CMAKE_AUTOUIC_SEARCH_PATHS can be set to include the custom locations so that the CMAKE_AUTOUIC option will find the ui files.
None of the previous answers have helped me.
I tried Ctrl + RMB on #include "./ui_mainwindow.h" in Qt Creator and after that the error did not appear since.
Few details to mention:
set(CMAKE_AUTOUIC ON) was enabled before add_executable()
cmake_minimum_required(VERSION 3.5)
I was using qt6
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?