Qt6 with Cmake submodule ReferenceError - qt

I have a QT6 Qml with a Cmake File like this
cmake_minimum_required(VERSION 3.16)
project(appTEST VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.2 COMPONENTS Gui Qml Quick REQUIRED)
set(CPPSOURCES
...)
set(CPPHEADERS
...)
qt_add_executable(appTEST
main.cpp
${CPPSOURCES}
${CPPHEADERS}
)
target_link_libraries(appTEST
PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Quick
Qt6::Qml)
install(TARGETS appTEST
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
qt6_add_qml_module(appTEST
URI "Main"
VERSION 1.0
NO_PLUGIN
QML_FILES main.qml
)
add_subdirectory(content)
add_subdirectory(imports)
target_link_libraries(appTEST PRIVATE
contentplugin
impplugin
)
and the content module:
qt_add_library(content STATIC)
qt6_add_qml_module(content
URI "content"
VERSION 1.0
QML_FILES
...
)
and imp module :
set_source_files_properties(Constants.qml
PROPERTIES
QT_QML_SINGLETON_TYPE true
)
...
qt_add_library(imp STATIC)
qt6_add_qml_module(imp
URI "imp"
VERSION 1.0
QML_FILES
Constants.qml
...
)
with the constatnt.qml marked as a Singleton like this:
pragma Singleton
import QtQuick 6.3
QtObject {
...
}
so I expect to be able to import imp on any page and have access to constatnt.qml properties.
but in run time get this error
ReferenceError: Constants is not defined
note tath I tried to adopt the structure from Qt Design Studio default project template.

Related

QML Svg in qt 5.15 static build, using CMake

I would like to migrate from 5.9 to 5.15 and I don't get to load svg from QML.
The original version, using qt provided by Ubuntu (5.9) did load svg from QML with
no extra steps. The new build uses a static Qt.
I use:
Ubuntu 18
In the house static build of Qt 5.15
CMake (3.18)
build Qt
./configure \
-confirm-license \
-release \
-static \
-no-pch \
-nomake tests \
-nomake examples \
-nomake tools \
-nomake examples \
-nomake tests \
-skip qtdoc \
-skip wayland \
-skip qtwebengine \
--prefix=/opt/Qt-5.15.0
CMake
cmake_minimum_required(VERSION 3.18)
project(test VERSION 0.0.0 LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 5.15
PATHS /opt/Qt-5.15.0
COMPONENTS
Core
Widgets
Qml
Svg
Quick
QuickControls2
Charts
Multimedia
LinguistTools
QmlImportScanner
Xml
REQUIRED QUIET
)
add_executable(test_qt
# c++ code files
main.cpp
# qml code files
qml/Main.qml
# resource files
qml/resources.qrc
)
target_include_directories(test_qt PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(test_qt
PRIVATE
Qt5::Widgets
Qt5::Qml
Qt5::Quick
Qt5::QuickControls2
Qt5::Svg
Qt5::Xml
Qt5::Gui
)
qt_import_qml_plugins(test_qt
INCLUDE
Qt5::QtQuick2Plugin
Qt5::QSvgPlugin
Qt5::QtQuickControls2Plugin
)
main.cpp
#include <QGuiApplication>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>
int main(int argc, char *argv[]) {
int status = 0;
try {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl("qrc:/Main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit);
if (engine.rootObjects().isEmpty())
return -1;
status = app.exec();
} catch (std::exception &) {
return -1;
}
return status;
}
Main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
id: window
width: 850
height: 850
visible: true
color: "lightgray"
Image{
source: "image.svg"
}
}
I omit the resource file, but it lists qml, png and svg files.
When using PNG image is loaded successfully, but when using SVG the following error appears:
qrc:/Main.qml:13:5: QML Image: Error decoding: qrc:/image.svg: Unsupported image format
The current Qt build contains the following files related to SVG handling:
./lib/libQt5Svg.a
./lib/libQt5Svg.la
./lib/cmake/Qt5Svg
./lib/cmake/Qt5Svg/Qt5SvgConfigVersion.cmake
./lib/cmake/Qt5Svg/Qt5SvgConfig.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin.cmake
./lib/pkgconfig/Qt5Svg.pc
./lib/libQt5Svg.prl
./include/QtSvg
./include/QtSvg/QGraphicsSvgItem
./include/QtSvg/5.15.0/QtSvg
./include/QtSvg/QSvgRenderer
./include/QtSvg/QSvgWidget
./include/QtSvg/QSvgGenerator
./include/QtSvg/QtSvg
./include/QtSvg/QtSvgVersion
./include/QtSvg/QtSvgDepends
EDIT
I added the missing Svg library dependency to the find_package command. The issue remains unsolved.
If I link against a shared build of the Qt libraries, this project works correctly.
The solution for me was in addition to including Qt5::Svg in find_package and target_link_libraries, eg:
find_package(Qt5 REQUIRED COMPONENTS Core Quick Qml Xml Charts Gui Svg QuickControls2)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::Qml Qt5::Xml Qt5::Charts Qt5::Gui Qt5::Svg Qt5::QuickControls2)
To also include the following when statically linking Qt 5.15:
qt5_import_plugins(${PROJECT_NAME} INCLUDE Qt5::QSvgPlugin)
That resolved errors such as "QML ButtonIcon: Error decoding: qrc:/resources/images/add-white-36dp.svg: Unsupported image format"
You are using cmake, but in a qmake .pro project,using static linking, you
must add
QT += svg
Note that svg plugin depends of widgets:
"QT += svg" is the same that "QT += svg widgets"
In android/IOS is better skip svg images to reduce app size.
/opt/Qt/5.15.1/gcc_64/lib$ ldd libQt5Svg.so
linux-vdso.so.1 (0x00007fff7bff0000)
libQt5Widgets.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Widgets.so.5 (0x00007ff0cbf55000)
libQt5Gui.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Gui.so.5 (0x00007ff0cb624000)
You can try to add some C++ QTSVG code in your main.cpp ,to test linking
https://doc.qt.io/qt-5/qsvggenerator.html

Adding sbt native packager plugin in SBT

I have a very organized build file that is composed of the following scala files:
Build.scala - the main Build file
Dependencies.scala - where I define the dependencies and the versions
BuildSettings.scala - where I define the build settings
plugins.sbt
A snippet of the Build.scala is as below:
import sbt._
import Keys._
object MyBuild extends Build {
import Dependencies._
import BuildSettings._
import NativePackagerHelper._
// Configure prompt to show current project
override lazy val settings = super.settings :+ {
shellPrompt := { s => Project.extract(s).currentProject.id + " > " }
}
// Define our project, with basic project information and library dependencies
lazy val project = Project("my-project", file("."))
.settings(buildSettings: _*)
.settings(
libraryDependencies ++= Seq(
Libraries.scalaAsync
// Add your additional libraries here (comma-separated)...
)
).enablePlugins(JavaAppPackaging, DockerPlugin)
}
All the 4 files that I mentioned above are in the same directory which is inside the project directory. But when I run this build file, I get the following error:
not found value: NativePackagerHelper
Any clues why his this?
I figured out what the problem was. I had to use the following in my build.properties
sbt.version=0.13.11
I originally had 0.13.6 and it was causing the import statements to fail!

"undefined reference to `boost::system::generic_category()" qtcreator with cmake

I try to use boost in qtcreator(windows) with Cmake, Cmake can find boost "system" and "thread" sucessfully, but there is there is a problem "undefined reference to `boost::system::generic_category()" while compiling. I searched problem like this, adding "-lboost_system" maybe a solution,but there is no ".pro" file in my project,so how to add it in CMakeLists.
Below is part of my CmakeLists:
project(demo)
cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(DEBUG_FLAG _d)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Qml REQUIRED)
find_package(Qt5Declarative)
find_package(OpenGL REQUIRED)
find_package(Boost 1.58 REQUIRED COMPONENTS system thread)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
set(OGRE_RESOURCE_FOLDER ${PROJECT_SOURCE_DIR}/resources)
set(OGRE_HOME "D:/3rd/OGRE")
set(OGRE_PLUGIN_FOLDER "${OGRE_HOME}\\bin")
include_directories(
"${OGRE_HOME}\\include"
"${OGRE_HOME}\\include\\OGRE"
"${OGRE_HOME}\\include\\OGRE\\RenderSystems\\GL"
"${OGRE_HOME}\\boost")
link_directories(${LINK_DIRECORIES} "${OGRE_HOME}\\lib" "${OGRE_HOME}\\lib\\opt")
include_directories("C:/3rd/Eigen" "C:/3rd/robotics/include" "D:/3rd/Eigen" "D:/3rd/robotics/include")
link_directories("C:/3rd/robotics/lib" "D:/3rd/robotics/lib")
add_library(robot src/robot.cpp)
add_library(QOgre src/QOgreWidget.cpp)
qt5_use_modules(QOgre OpenGL Widgets Xml)
add_executable(OGREDemo src/QOgreWidgetDemo.cpp src/ogredemo.cpp )
qt5_use_modules(OGREDemo OpenGL Widgets Xml)
target_link_libraries(OGREDemo ${OPENGL_LIBRARIES} ${BOOST_LINK_LIBS_DEBUG}
OgreMain${DEBUG_FLAG} OIS${DEBUG_FLAG}
orocos-kdl${DEBUG_FLAG} robot QOgre )
file(GLOB_RECURSE QMLS "." "*.qml")
qt5_add_resources(RESOURCES_ADDED resources/qml.qrc)
set_source_files_properties(${RESOURCE_ADDED} PROPERTIES GENERATED ON)
add_executable(recat src/main.cpp src/QmlOgre.cpp ${RESOURCES_ADDED} ${QMLS})
qt5_use_modules(recat Widgets Xml Quick Qml Core Gui Declarative)
target_link_libraries(recat ${OPENGL_LIBRARIES} ${BOOST_LINK_LIBS_DEBUG}
OgreMain${DEBUG_FLAG} OIS${DEBUG_FLAG} RenderSystem_GL${DEBUG_FLAG}
orocos-kdl${DEBUG_FLAG} robot)
configure_file (
"${PROJECT_SOURCE_DIR}/plugins.cfg.in"
"${PROJECT_BINARY_DIR}/plugins.cfg"
)
configure_file (
"${PROJECT_SOURCE_DIR}/resources.cfg.in"
"${PROJECT_BINARY_DIR}/resources.cfg"
)
And detailed problem description:
D:\3rd\robotics_android\src\QOgreWidgetDemo.cpp:-1: error: undefined reference to `boost::system::generic_category()'
error: CMakeFiles\OGREDemo.dir/objects.a(QOgreWidgetDemo.cpp.obj): bad reloc address 0x1b in section `.text$_ZNK5boost6system10error_code7messageEv[__ZNK5boost6system10error_code7messageEv]'
The right variable to link against is Boost_SYSTEM_LIBRARY_DEBUG. As #usr1234567 stated it is not necessary to use link_directories.
For reference see: https://cmake.org/cmake/help/v3.0/module/FindBoost.html

FindQt4 fails to link QtOpenGL project properly

Projects that use the QtOpenGL fail to link to gl calls, but only on Windows (Linux is happy). The MSVC 2008 error messages for the following minimal case project are:
1>gllink.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity#0 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL#ImageWidget##MAEXXZ)
1>gllink.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode#4 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL#ImageWidget##MAEXXZ)
1>gllink.exe : fatal error LNK1120: 2 unresolved externals
These errors disappear when I manually add "opengl32.lib" to the "Additional Dependencies" list, but I believe this should not be necessary, since this should either be done by FindQt4.cmake, or should be taken care of by the dependency of QtOpenGL on opengl32. Of course, I must be mistaken, so I would really appreciate some input on how to properly fix this project.
gllink.cpp
#include <QtOpenGL>
#include <QWidget>
class ImageWidget : public QGLWidget
{
public:
ImageWidget(QWidget* parent = 0) :
QGLWidget(parent)
{
}
protected:
void initializeGL()
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
};
int main()
{
ImageWidget w;
return 0;
}
CMakeLists.txt
PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
FIND_PACKAGE( Qt4 4.6.0 REQUIRED COMPONENTS QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )
ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${QT_LIBRARIES} )
FindQt4 doesn't take care of that, you have to search for OpenGL by yourself. Now CMake provides a FindOpenGL package, so fixing your project is just a matter of adding find_package( OpenGL ) and linking the libraries to your target:
PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
FIND_PACKAGE( OpenGL )
FIND_PACKAGE( Qt4 4.6.0 REQUIRED QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )
ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${OPENGL_LIBRARIES} ${QT_LIBRARIES} )
Btw, you should check if those libraries are really found and warn the user about the problem if they're not :)

Build Qt Tests with CMake

Can anyone give me an example of some QT test code and a CMakeLists.txt that build with Cmake and ran with CTest. I can't seem to find any!
-Kurtis
Here is an example of using cmake 2.8.11 and Qt5.2. Note that cmake now supports testfiles with a .moc-include at the bottom.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11)
project(foo)
enable_testing()
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Test REQUIRED)
add_executable(foo foo.cpp)
add_test(foo foo)
target_link_libraries(foo Qt5::Test)
foo.cpp:
#include <QTest>
class Foo : public QObject {
Q_OBJECT
private slots:
void t1() { QVERIFY(true); }
};
QTEST_MAIN(Foo)
#include "foo.moc"
An example taken from Charm (Tests/CMakeLists.txt):
SET( TestApplication_SRCS TestApplication.cpp )
SET( TEST_LIBRARIES CharmCore ${QT_QTTEST_LIBRARY} ${QT_LIBRARIES} )
SET( SqLiteStorageTests_SRCS SqLiteStorageTests.cpp )
QT4_AUTOMOC( ${SqLiteStorageTests_SRCS} )
ADD_EXECUTABLE( SqLiteStorageTests ${SqLiteStorageTests_SRCS} )
TARGET_LINK_LIBRARIES( SqLiteStorageTests ${TEST_LIBRARIES} )
ADD_TEST( NAME SqLiteStorageTests COMMAND SqLiteStorageTests )
The only difference to a normal executable is that you call ADD_TEST macro.
Have a look at e.g. Charm to see it in action.

Resources