0

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Did you install QtCharts? I mean when installing from the online installer did you select the QtCharts from Additional Libraries section under Qt 6.2.1? It is not selected by default. You only showed the location of the QtCharts source files, not the libs. – vre Nov 15 '21 at 16:06
  • @vre Turns out that I had installed QtCharts in the online installer, but not the one under Qt 6.2.1. I've edited my question with a screenshot to clarify. Thank you!! – Bruno Serpa Nov 15 '21 at 16:33

1 Answers1

0

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