10

It seems I can't find a way to include <QtCharts/QLineSeries> in my header so Qt knows about the QLineSeries class.

  • I added Qt += charts to my .pro file.
  • I added #include <QtCharts/QLineSeries> to MainWindow.h
  • Then I delete .pro.user file and any builds I've done. And then reopen the .pro file to reconfigure the project in the cleanest way.
  • Finally I set the build mode to Release, run QMake by right-clicking the project and selecting it and Run it.

I get the message:

 ...\PlottingCharts\mainwindow.h:14: error: 'QLineSeries' does not name a type
     QLineSeries *series;
     ^

So clearly Qt doesn't know anything about QLineSeries.

For reference, the linechart and openglseries examples work correctly.

Any one knows what I'm missing?

A. Vieira
  • 1,213
  • 2
  • 11
  • 27

2 Answers2

16

As I wrote the question I found the answer.

I was missing using namespace QtCharts; in the header file. Got the reference from: http://doc.qt.io/qt-5/qtcharts-index.html

A. Vieira
  • 1,213
  • 2
  • 11
  • 27
  • 1
    Indeed the error you had was a compiler error, complaining that a type was undefined, not that the include was not found ! For people falling on this issue, if they got linker error about symbols not found, then it's because they have forgotten to add Qt += charts in the .pro file (or forgotten to reapply qmake) – sandwood Nov 06 '17 at 14:54
  • You should never put a `using namespace` declaration in a header file. This is bad coding practice. See https://stackoverflow.com/a/5849668/3964397 for more details. – tjwrona1992 May 31 '19 at 04:03
4

If you want to avoid specifying the namespace, you can also declare your variable as:

QtCharts::QLineSeries *series;

With this solution you always have to prepend QtCharts::, but you also know your scope and the origin of your calls.

Mario García
  • 172
  • 10