Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error initalizing QChart
Forum Update on Monday, May 27th 2025

Error initalizing QChart

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by viniltc
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • V viniltc

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @viniltc
      Always just look at the docs page. https://doc.qt.io/qt-5/qchart.html :

      Header: #include <QChart>

      1 Reply Last reply
      5
      • V Offline
        V Offline
        viniltc
        wrote on last edited by
        #3

        @JonB , Thankyou

        I tried to initialize a QChart in the QMainWindow 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?

        JonBJ 1 Reply Last reply
        0
        • V viniltc

          @JonB , Thankyou

          I tried to initialize a QChart in the QMainWindow 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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @viniltc
          Well, I've never used a QChart/QChartView(), but I imagine your QChartView() 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()?

          1 Reply Last reply
          3
          • A Offline
            A Offline
            AndyJ
            wrote on last edited by
            #5

            @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>
            
            ...
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Are you missing QT += charts in your .pro file ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved