Unable to use QTLocation and QTPositioning in my QML view - qt

In my QT/QML project created with QT 6.1 and operating system Ubuntu 20.04 I would use the QTMapView. In order to do this, I tried to install QTLocation and QTPosition by using the following command:
sudo apt install qml-module-qtlocation qml-module-qtpositioning
As suggested from the following topic:
https://askubuntu.com/questions/1115899/qtlocation-and-qtpositioning-not-installed
After that, I tried to import in my QML view the QTLocation and QTPosition with the following code
import QtLocation 5.12
import QtPositioning 5.12
But I receive a "QML Module not found" message for both import.
Below, you can find also the CMakeLists.txt file of the project:
cmake_minimum_required(VERSION 3.14)
project(myapp 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 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 COMPONENTS Core Quick REQUIRED)
find_package(Qt6 COMPONENTS Core Quick REQUIRED)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(PROJECT_SOURCES
main.cpp
qml.qrc
images.qrc
)
qt_add_executable(myapp
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
target_compile_definitions(myapp
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(myapp
PRIVATE Qt6::Core Qt6::Quick)
set_target_properties(myapp PROPERTIES
QT_QML_MODULE_VERSION 1.0
QT_QML_MODULE_URI com.my.myapp
)
list(APPEND QML_IMPORT_PATH .)
qt6_qml_type_registration(myapp)
qt_import_qml_plugins(myapp)
qt_finalize_executable(myapp)
Did I miss something? How can I solve the problem and import correctly the map components?

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.

QTCreator and VisualStudio Code compilation with CMake

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

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}

Building Qt Qml Plugin using CMake

I want to compile Qt Qml Plugin using CMake (instead of QMake) and add this plugin to the some application. Plugin and application script should be in separate CMakeLists.txt.
File structure:
CMakeLists.txt
main.cpp
main.qml
qml.qrc
plugin/
CMakeLists.txt
MyItem.cpp
MyItem.h
Plugin.h
qmldir
I have a script plugin/CMakeLists.txt that generates libmyplugin.dylib and qmldir and put it to the plugin/MyPlugin subfolder of plugin binary dir:
plugin/CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
add_library(myplugin SHARED
Item.cpp
Item.h
Plugin.h
)
set_target_properties(myplugin PROPERTIES
AUTOMOC TRUE
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin
)
# Copy qmldir file
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/qmldir
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/qmldir" "${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir"
COMMENT "Copy qmldir"
)
target_sources(myplugin PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
)
find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
Now I need to build myplugin target before/after app target and copy plugin files from <myplugin binary dir>/plugin into location of app target binary (in case of Windows and Linux) or into <app bundle>/Contents/PlugIns (in case of macOS):
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
add_executable(app
main.cpp
qml.qrc
)
set_target_properties(app PROPERTIES
AUTORCC TRUE
)
add_subdirectory(plugin)
add_dependencies(app plugin)
# Needs to copy plugin files...
find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
I need to copy plugin files only if they changed. It would be great to write function that will take app target and plugin target and create appropriate dependencies.
For my Qt projects and qml plugins I using next rules. It's actual for win platform, but I hope it will be helpfull for your macOS
Output directory is same for all projects:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
So all sub_directories with CMakeLists and it's project targets will build into same directory. It's helpfull for project running and finnaly deploying.
Qml plugins I copy to directory
set_target_properties(targetProject PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>/plugins/${QmlPluginName}/)
After build qml plugin I'm copy qmldir file to RUNTIME_OUTPUT_DIRECTORY
add_custom_command(TARGET ${targetProject} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
${QML_DIR_FILE}
${CMAKE_BINARY_DIR}/bin/$<CONFIG>/plugins/${QmlPluginName}/qmldir)
After this steps I had hext directory structure:
/build_dir/bin/Debug/
|application.exe
|plugins/
| |QmlPluginName/
| | |qmldir
| | |QmlPluginName.dll
So after this steps don't forget about:
QQmlApplicationEngine engine;
engine.addImportPath("./plugins");
P.S. sorry for my English

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} )

Resources