|
add track line to finacedemo |
Posted by Morning song on May-03-2015 23:18 |
|
hi!
I want to draw track line in finacedemo. so i copy some codes from trackfinance demo.
MarketTrendWidget::MarketTrendWidget(QWidget *parent) :
QDialog(parent, Qt::Window)
{
...........
...........
drawChart(m_ChartViewer);
// Set up the mouseMovePlotArea handler for drawing the track cursor
connect(m_ChartViewer, SIGNAL(mouseMovePlotArea(QMouseEvent*)),
SLOT(onMouseMovePlotArea(QMouseEvent*)));
}
void MarketTrendWidget::onMouseMovePlotArea(QMouseEvent *)
{
trackFinance((MultiChart *)m_ChartViewer->getChart(), m_ChartViewer->getPlotAreaMouseX(), m_ChartViewer->getPlotAreaMouseY());
m_ChartViewer->updateDisplay();
}
void MarketTrendWidget::trackFinance(MultiChart *m, int mouseX, int mouseY)
{
//no codes
}
but when i run finacedemo.this app will crash. i use c++ and QT. how can i do??? thank you! |
Re: add track line to finacedemo |
Posted by Peter Kwan on May-05-2015 02:31 |
|
Hi Morning,
In the original sample code, the FinanceChart is a local variable allocated on the stack in
drawChart:
FinanceChart m(width);
From the above code, it can be seen that the FinanceChart will be destroyed and no longer
exists outside of drawChart. If your code use it outside of drawChart (such as in
onMouseMovePlotArea), it will be using invalid memory and will crash the code.
For your case, please allocate the FinanceChart on the heap instead:
FinanceChart *m = new FinanceChart(width);
Now the chart can live outside the drawChart, but your code would need to eventually free
it to avoid memory leak. I suggest before your code pass a new chart to QChartViewer, it
can free the previously chart in the QChartViewer. Also, when the dialog is finally closed,
there should be a chart still in QChartViewer. Please delete it in the destructor of the dialog.
Hope this can help.
Regards
Peter Kwan |
|