Unsolved Error initalizing QChart
-
Hi all,
I tried to initialize a new QChart:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPainter> #include <QtCharts/QAreaSeries> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QLineSeries *series = new QLineSeries(); series->append(0, 6); series->append(2, 4); series->append(3, 8); series->append(7, 4); series->append(10, 5); *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); QChart *chart = new QChart(); //---------->cant't initialize this chart->legend()->hide(); chart->addSeries(series); chart->createDefaultAxes(); chart->setTitle("Simple line chart example"); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); }
I get the following error:
mainwindow.cpp:70:9: error: unknown type name 'QChart'; did you mean 'QChar'? qmetatype.h:1970:1: note: 'QChar' declared here qmetatype.h:106:17: note: expanded from macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
Could you please pointout where I'm doing wrong?
-
@viniltc
Always just look at the docs page. https://doc.qt.io/qt-5/qchart.html :Header: #include <QChart>
-
@JonB , Thankyou
I tried to initialize a
QChart
in theQMainWindow
as follows:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QLineSeries *series = new QLineSeries(); series->append(0, 16); series->append(1, 25); series->append(2, 24); series->append(3, 19); series->append(4, 33); series->append(5, 25); series->append(6, 34); // Create chart, add data, hide legend, and add axis QChart *chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); chart->createDefaultAxes(); // Customize the title font QFont font; font.setPixelSize(18); chart->setTitleFont(font); chart->setTitleBrush(QBrush(Qt::black)); chart->setTitle("Barry Bonds HRs as Pirate"); // Change the line color and weight QPen pen(QRgb(0x000000)); pen.setWidth(5); series->setPen(pen); chart->setAnimationOptions(QChart::AllAnimations); // Change the x axis categories QCategoryAxis *axisX = new QCategoryAxis(); axisX->append("1986",0); axisX->append("1987",1); axisX->append("1988",2); axisX->append("1989",3); axisX->append("1990",4); axisX->append("1991",5); axisX->append("1992",6); chart->setAxisX(axisX, series); // Used to display the chart QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); }
However , this is not showing anything , can you spot an issue?
-
@viniltc
Well, I've never used aQChart
/QChartView()
, but I imagine yourQChartView()
needs adding onto the main window somehow in order to appear?QChartView *chartView = new QChartView(chart, this);
Does that make it visible? Then you can think about exactly how/where you want it added onto the main window properly, e.g. I imagine you'll want to put it into
QMainWindow::setCentralWidget()
? -
@viniltc said in Error initalizing QChart:
mainwindow.cpp:70:9: error: unknown type name 'QChart';
The error states that QChart is an unknown type, so try including QtCharts after QPainter. See below.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPainter> #include <QtCharts> ...
-
Hi,
Are you missing
QT += charts
in your .pro file ?