How can I add QtCharts library to my project? - c++

I was trying to add QtCharts library to my project but an error occurred:
Target "PTP3" links to target "Qt6::Charts" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
I installed Qt5::Charts and Qt6::Charts via Qt MaintenanceTool but the issue remained. Anyone knows how to fix it?
I'm using Qt Creator 7.0.2. Here is my Cmake file. I followed the instructions from https://doc.qt.io/qt-6/qtcharts-index.html but it didn't help.:
cmake_minimum_required(VERSION 3.5)
project(PTP3 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)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Charts REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
set(PROJECT_SOURCES
main.cpp
footballmatches.cpp
footballmatches.h
footballmatches.ui
pivottable.h
pivottable.cpp
pivottable.ui
stadiumschart.h
stadiumschart.cpp
stadiumschart.ui
some_qml.qml
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(PTP3
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET PTP3 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(PTP3 SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(PTP3
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(PTP3 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(PTP3 PRIVATE Qt${QT_VERSION_MAJOR}::Charts)
set_target_properties(PTP3 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_finalize_executable(PTP3)
endif()

you should have both these lines in your Cmake File for adding one of the qt libs to your project for example for qt charts you need these:
find_package(Qt5Charts REQUIRED)
target_link_libraries(${PROJECT_NAME}
Qt5::Charts
)
Be sure to add target_link_libraries at the end of your CMakeLists.txt file.

Related

QT6 Insists on using cdb instead of gdb

I am using Qt 6.0.4 in Win10, the 'Help/About' says QtCreator version is 4.15.0. I have a very simple desktop application, built using CMake, and using QCustomPlot that crashes. I am using the Kit Qt 6.0.4 mingw81_64. When I look into the settings under manage kits, I see the debugger is supposedly set to 'GNU gdb 8.1 for MinGW 8.1.0 64Bit'. But when I start debugging, I get a message box that seems to indicate it is using cdb, not gdb. In the Debuggers tab for the kit, it shows all the GNU gdb debuggers, but also to CDB, all auto-detected. I can not remove the cdb entries, at least not that I can figure out. How do I turn off cdb so it only uses the gdb that seems to be called for in the kit settings??
My CMakeLists.txt file is:
project(QT60DataAcq 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)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package( ...) calls below.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(QT NAMES Qt6 COMPONENTS Widgets PrintSupport REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets PrintSupport REQUIRED)
include_directories(C:/Pgms/Qt/QCUSTOMPLOT2X)
set(PROJECT_SOURCES
main.cpp
dacqmain.cpp
dacqmain.h
dacqmain.ui
C:/Pgms/Qt/QCUSTOMPLOT2X/qcustomplot.cpp
)
set(CMAKE_BUILD_TYPE Debug)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(QT60DataAcq
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(QT60DataAcq SHARED
${PROJECT_SOURCES}
)
else()
add_executable(QT60DataAcq
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(QT60DataAcq PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PrintSupport)
set_target_properties(QT60DataAcq
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib/
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(QT60DataAcq)
endif()

Where are Python headers included in MacOS Monterey arm64

I am currently struggling to find the python headers on my system and can't include them therefore into a C++ application
this is my cmake file -->
cmake_minimum_required(VERSION 3.14)
project(ondoki-desktop VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_TOOLCHAIN_FILE /Users/ahoehne/repos/vcpkg/scripts/buildsystems/vcpkg.cmake)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Python3 COMPONENTS Interpreter REQUIRED)
if(DEFINED ENV{VIRTUAL_ENV} OR DEFINED ENV{CONDA_PREFIX})
set(_pip_args)
else()
set(_pip_args "--user")
endif()
include(ExternalProject)
ExternalProject_Add(ondoki-daemon
GIT_REPOSITORY "https://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#github.com/ondoki-org/ondoki-daemon.git"
GIT_TAG develop
SOURCE_DIR "${PROJECT_SOURCE_DIR}/ondoki-daemon"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
execute_process(COMMAND ${Python3_EXECUTABLE} -m pip install -r ${PROJECT_SOURCE_DIR}/ondoki-daemon/requirements.txt)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)
find_package(ZeroMQ CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(cppzmq CONFIG REQUIRED)
find_package(protobuf CONFIG REQUIRED)
find_package( Boost REQUIRED COMPONENTS )
find_package (Boost COMPONENTS filesystem regex REQUIRED)
find_package(msgpack CONFIG REQUIRED)
find_package(pybind11 CONFIG REQUIRED)
set(PROJECT_SOURCES
main.cpp
qml.qrc
images.qrc
python.qrc
zmqbridge.cpp
zmqbridge.h
zmqworker.h
zmqworker.cpp
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(ondoki-desktop
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET ondoki-desktop APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(ondoki-desktop SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(ondoki-desktop
${PROJECT_SOURCES}
)
endif()
endif()
target_compile_definitions(ondoki-desktop
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(ondoki-desktop
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick
protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite libzmq
libzmq-static nlohmann_json nlohmann_json::nlohmann_json
cppzmq cppzmq-static ${Boost_LIBRARIES} ยข{PYTHON_LIBRARIES}
msgpackc msgpackc-cxx pybind11::lto pybind11::embed pybind11::module
)
set_target_properties(ondoki-desktop 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}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_import_qml_plugins(ondoki-desktop)
qt_finalize_executable(ondoki-desktop)
endif()
The Error message for including #include <pybind11/embed.h> is common.h Python.h not found
locate python.h results in
/Applications/CLion.app/Contents/plugins/python-ce/helpers/pydev/pydevd_attach_to_process/common/python.h
/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd_attach_to_process/common/python.h
/opt/homebrew/Cellar/boost/1.76.0/include/boost/mpi/python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/parameter/python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/arg_from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/arg_from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/arg_to_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/obj_mgr_arg_from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/return_from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/shared_ptr_from_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/converter/shared_ptr_to_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/detail/wrap_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python/register_ptr_to_python.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/python.hpp
/usr/local/include has no Python.h as has /opt/homebrew/include
a "which python3" results in /opt/homebrew/bin/python3
and my cmake parameters are the following
-GNinja
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_PROJECT_INCLUDE_BEFORE:PATH=%{IDE:ResourcePath}/package-manager/auto-setup.cmake
-DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
-DCMAKE_TOOLCHAIN_FILE=/Users/ahoehne/repos/vcpkg/scripts/buildsystems/vcpkg.cmake
%{CMAKE_OSX_ARCHITECTURES:DefaultFlag}
Thank you for any help or hints into the right direction.
***EDIT ***
if i include
include_directories(/opt/homebrew/Frameworks/Python.framework/Headers)
at least he seems to find the header
but results in a "expected member name or ";" after declaration specifiers --> Following the object.h file I see
typedef struct{
int slot; /* slot id, see below */
void *pfunc; /* function pointer */
} PyType_Slot;
typedef struct{
const char* name;
int basicsize;
int itemsize;
unsigned int flags;
PyType_Slot *slots; /* terminated by slot==0. */
} PyType_Spec;
on the error code line ...if I remove PyType_Slot *slots; the program compiles but I highly doubt that this is a bug and rather a problem on my side
It seems to me that the first problem is with the order of these lines:
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Python3 COMPONENTS Interpreter REQUIRED)
So you refer to an unknown yet PYTHON_INCLUDE_DIRS variable.
The second problem is the variable name itself. You are searching for Python3 package so to me the variable name should be Python3_INCLUDE_DIRS as per the FindPython3 doc
The 3rd problem is that you are searching for Interpreter only, so the headers will not be found anyway.
Summarising, please check if something like the following works:
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
include_directories(${Python3_INCLUDE_DIRS})

Cannot use Qt Charts - QT6 | Cmake

I am using Qt 6.2.1 with cmake and I can't find a way to use Qt Charts.
The Qt Charts is installed under "C:\Qt\6.2.1\Src\qtcharts"
In my cmakelists.txt I've included the Qt Charts in find_package and target_link_libraries. That seemed to work on qt5 (found on other answers in StackOverflow) but don't work on qt6. (I've hidden the project sources files)
cmake_minimum_required(VERSION 3.5)
project(projectname 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_CXX_FLAGS_DEBUG "/MDd")
find_package(QT NAMES Qt6 COMPONENTS Widgets Charts REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Xml Widgets REQUIRED)
set(PROJECT_SOURCES
...
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(projectname
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
add_executable(projectname
${PROJECT_SOURCES}
)
endif()
target_link_libraries(projectname PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Xml Qt6::Charts)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(projectname)
endif()
I get this error:
C:\Qt\6.2.1\msvc2019_64\lib\cmake\Qt6Core\Qt6CoreMacros.cmake:559: error: Target "projectname" links to target "Qt6::Charts" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:523 (_qt_internal_create_executable) C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:847 (qt6_add_executable) CMakeLists.txt:78 (qt_add_executable)
SOLUTION (Thanks to #vre):
I had to select the Qt Charts under 6.2.1 and not under Qt in the online installer
Qt Online Installer

Qt6 desktop app and shared library with cmake

I am trying to make a project with subdirectories using cmake.
When using qmake this mechanism was built in Qt creator.
The project contains a shared library and a desktop app.
Below is the structure of the project and cmake I have:
Main cmake:
cmake_minimum_required (VERSION 3.5)
project(contractapp)
add_subdirectory(contract-core)
add_subdirectory(contract-desktop)
Desktop app cmake:
project(contract-desktop 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)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package( ...) calls below.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
find_package(contract-core REQUIRED)
set(PROJECT_SOURCES
main.cpp
MainWindow.cpp
MainWindow.h
MainWindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(contract-desktop
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(contract-desktop SHARED
${PROJECT_SOURCES}
)
else()
add_executable(contract-desktop
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(contract-desktop PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
Shared library cmake:
cmake_minimum_required(VERSION 3.14)
project(contract-core 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 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
add_library(contract-core SHARED
contract-core_global.h
Person.cpp
Person.h
)
target_link_libraries(contract-core PRIVATE Qt${QT_VERSION_MAJOR}::Core)
target_compile_definitions(contract-core PRIVATE CONTRACTCORE_LIBRARY)
My problem is that the desktop application does not see the library that I am making.
That library has to be copied to the same folder the application is in. Otherwise, each is in its own folder, and the application won't see it. Most likely you'd want to set the target folder for both binaries (the application and the library) to be some common build subfolder of both.

Why VS doesn't see included files?

I am a student writing a project. I have created a program (here's the link to SVN: https://mysvn.ru/Ilya_Antonov/antonov_sem_prj). I use CMake to build it and generate the .sln project. But when run it in Visual Studio, there are some errors: the compiler can't open the files "drumswindow.h" and "mainwindow.h" (fatal error C1083). Please, take a look at my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
project(MusicSimulator VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Gui REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS Multimedia REQUIRED)
add_executable(MusicSimulator
mainwindow.ui
mainwindow.cpp
mainwindow.h
main.cpp
drumswindow.ui
drumswindow.cpp
drumswindow.h
keyswindow.ui
keyswindow.cpp
keyswindow.h
images.qrc
drumres.qrc
keysres2.qrc
keysres3.qrc
keysres4.qrc
keysres5.qrc
keysres6.qrc
)
target_link_libraries(MusicSimulator Qt5::Widgets Qt5::Gui Qt5::Core Qt5::Multimedia)
What have I done wrong here?
You don't include the current source dir and therefore the compiler doesn't find it. Either add the current source dir or use the correct include statements (#include "keyswindow.h" for example)