11

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?

2 Answers 2

17

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

2
  • 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
    Commented Nov 6, 2017 at 14:54
  • You should never put a using namespace declaration in a header file. This is bad coding practice. See stackoverflow.com/a/5849668/3964397 for more details.
    – tjwrona
    Commented May 31, 2019 at 4: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.