4

I am trying to use the Qt QChart to plot a line graph. I am using visual Studio 2013 with Qt 5.8. I created a simple QApplication. When I paste QLineSeries *series it says QLineSeries is undefined. How can I fix this?

#include "QtGuiApplication2.h"
#include <QtWidgets/QApplication>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtGuiApplication2 w;

    QLineSeries *series = new QLineSeries();

    w.show();
    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
amanda45
  • 535
  • 10
  • 29

2 Answers2

3

you need to link against QT5Charts under:

general properties
- Linker
  - Input
   - add the path: e.g. C:\Libraries\Qt\Qt5.7.0\msvc\lib\Qt5Chartsd.lib

I suppose, you don't forget to add the namespace?

using namespace QtCharts;

if you work with cmake, just add this to your cmake file:

find_package(Qt5Charts)
target_link_libraries(${targetName} Qt5::Charts)
Soeren
  • 1,725
  • 1
  • 16
  • 30
1

In my situation, I had also "undefined" all #include for QtCharts library. After a prolonged series of google searches, the following actions helped me (considering Visual Studio 2019 with Qt 5.15.2):

  1. Check if you have macros $(Qt_INCLUDEPATH_) in (Project -> [Project] Properties -> C/C++ -> General -> Additional Include Directories) and macros $(Qt_LIBS_) in (Project -> [Project] Properties -> Linker -> Input -> Additional Dependencies). They should already have all the paths for the lib directories. You can check it by pressing near the field (Arrow down -> Edit... -> Macros>> -> [Insert the macros name]). If you have the paths in these macros then you don't need to add them again and go directly to point 3. If not, go to point 2.

  2. Add a common path in (Project -> [Project] Properties -> General -> Additional Library Directories) as C:\Qt\5.15.2\msvc2019_64\lib (it can be a little different for you). With this precision, only the relative path can be mentioned in (Linker -> Input -> Additional Dependencies) i.e. Qt5Chartsd.lib. In (Project -> [Project] Properties -> C/C++ -> General -> Additional Include Directories) write the absolute path as C:\Qt\5.15.2\msvc2019_64\include\QtCharts (again, can be different).

  3. With Qt VS Tools, there are new fields in (Project -> [Project] Properties ->...). You need (Qt Project Settings -> Qt Modules). As in Qt Creator, there's a need to add a new module charts. With (Arrow Down -> Select Modules...) near the field, you can even see all the modules which it's possible to activate. For me, I finally had core;gui;widgets;charts.

  4. The solution worked for me without that point but just in case - add all the required dynamic libraries .dll in the same directory with your .exe project file. .dll libraries load at run time so they might help in certain cases.

tkonov55
  • 11
  • 2