PDA

View Full Version : Dynamically change data to QCharts



Blitzor DDD
28th July 2016, 11:14
Hello.

I studied this example http://doc.qt.io/qt-5/qtcharts-scatterchart-example.html
and wonder how can I dynamically change my data to plot?

this is main

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ChartView *chartView = new ChartView();
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();

return a.exec();
}

and since window.show(); I cannot do anything with my plot...
Is it utterly possible dynamically change my plot?

anda_skoa
28th July 2016, 11:51
and since window.show(); I cannot do anything with my plot...

What do you mean with "can't to anything"?
What do you want to do?



Is it utterly possible dynamically change my plot?
Why would that be impossible?

The QChartView has a setter for a QChart and QChart has several methods to change its data.
Does any of these methods not work when you call it?

Cheers,
_

d_stranz
28th July 2016, 16:45
I cannot do anything with my plot...

Your problem is that you have written your code in a way that gives you no ability to access your ChartView. You use a plain QMainWindow as the main window for your app and create your ChartView in main() where the pointer is not exposed to anything else.

Derive your own MainWindow class from QMainWindow. In the constructor for that class, create your ChartView, save the pointer to a member variable of the MainWindow class, and add it as the central widget:



// MainWindow.h

#include <QMainWindow>
#include <ChartView.h> // or whatever the name of the file is

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow( QWidget * parent = 0 );

private:
ChartView * pChartView;
}

// MainWindow.cpp

#include "MainWindow.h"

MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{

pChartView = new ChartView();
setCentralWidget( pChartView );
resize( 400, 300 );
}

// main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MainWindow window;
window.show();

return a.exec();
}


Now you have a pointer to your ChartView inside your MainWindow instance, so you can do whatever you want to change the plot or update its data.