Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add QAreaSeries to Qtchart?



  • Hello,

    I have the following code in which I create Qtchart, and whenever the reset button is pushed the graph should be shown with the QAreaSeries plotted on it.

    main::main(QWidget *parent) :
    	QDialog(parent),
    	ui(new Ui::main)
    {
    	ui->setupUi(this);
    	setWindowTitle("Test Config");
    
    	p_MainWindow = parent;	
    
    	qChart = new QChart();
    	myChartView = new QChartView(qChart);
    	qAxisX = new QValueAxis;
    	qAxisY = new QValueAxis;
    	qAxisX->setRange(0, 30);
    	qAxisY->setRange(0, 10000);
    
    }
    
    void main::maingraph()
    {
    	for (int k = 1; k < 3; k++)
    	{
    		if (true)
    		{
    			switch (k)
    			{
    			case 1:
    				series0 = new QLineSeries();
    				series1 = new QLineSeries();
    
    				*series0 << QPointF(1, 6) << QPointF(1, 10);
    				*series1 << QPointF(4, 6) << QPointF(4, 10);
    
    				series = new QAreaSeries(series0, series1);
    
    				AddBlockToGraph(series);
    
    				break;
    			case 2:
                                    series0 = new QLineSeries();
    				series1 = new QLineSeries();
    
    				*series0 << QPointF(10,16) << QPointF(10, 20);
    				*series1 << QPointF(14, 16) << QPointF(14, 20);
    
    				series = new QAreaSeries(series0, series1);
    
    				AddBlockToGraph(series);
    				break;
    			default:
    				break;
    			}
    		}
    	}
    }
    
    void main::AddBlockToGraph(QAreaSeries *series)      //I get this QAreaSeries from Switch cases
    {
    //This is the place where I need help. I should add QAreaSeries to the graph after every switch case.
    
    	myChartView->chart()->mapToParent(/*what to add here*/,series);
    	qChart->setAxisX(qAxisX, series);
    	qChart->setAxisY(qAxisY, series);
    
    	ui->verticalLayout->addWidget(myChartView);
    }
    
    void main::on_pushButton_clicked()               //the reset button function
    {
    	clearLayout(ui->verticalLayout, true);
    
    	maingraph(m_tcGFullXmlConfig);
    }
    

    Thank you.


  • Lifetime Qt Champion

    @preddy said in How to add QAreaSeries to Qtchart?:

    QAreaSeries

    Hi
    Im not sure what that
    myChartView->chart()->mapToParent
    is about ?
    i would expect something like
    myChartView->chart()->addSeries(series);


Log in to reply