QT QChart can't show,refresh doesn't work
-
@printfne
I suggest you look at my code at https://forum.qt.io/topic/145986/qchart-line-series-not-shown/3 and read through that thread where much the same about "not being to able to see updates" was asked. I have not needed anyrepaint()
s orupdate()
s. As per there, you do have to sort out the axes yourself, I suspect that might be your issue here? -
@JonB I think, I found a minimal demo:
auto chart = new QChart(); auto series = new QBarSeries(); auto view = new QChartView(); setCentralWidget(view); auto bar = new QBarSet(""); *bar << 1 << 2; series->append(bar); chart->addSeries(series); view->setChart(chart);
it's perfectly normal.But when I move
*bar << 1 << 2;
to any position afterchart->addSeries(series);
, the bars of the histogram are no longer displayed, like the screenshot of the main window given in the original question. -
I haven't used
QChart
that much,but I think you need to set the series after you add the points. Therefore you cant modify just the series when it has been drawn already.
[ok, judging from the example, you dont need to, but you need to set the axis to match your data]
You could search for "dynamic qchart series" or somethingEdit:
There is even a Qt Example
wth... this example has no code available in 6.5?!
There is a page in the example section but it seems that the code got removed?!The code is available in Qt5.15
-
@Pl45m4 @JonB
I modified the axis to followChart::handleTimeout
in examplehttps://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/dynamicspline/chart.cpp?h=5.15
, but it seems to have no effect on the display of the histogram. Also I noticed that in the example the scroll is called after adding a point, but I only added the barset once at the beginning, I don't know if that would have an effect.
Here is the modified code:auto view = new QChartView(); auto chart = new QChart(); auto series = new QBarSeries(); auto bar = new QBarSet(""); setCentralWidget(view); view->setChart(chart); chart->addSeries(series); series->append(bar); QValueAxis *m_axisX = new QValueAxis(); QValueAxis *m_axisY = new QValueAxis(); chart->addAxis(m_axisX,Qt::AlignBottom); chart->addAxis(m_axisY,Qt::AlignLeft); *bar << 1 << 2 << 3; m_axisX->setTickCount(5); m_axisX->setRange(0, 100); m_axisY->setRange(0, 100); qreal x = chart->plotArea().width() / m_axisX->tickCount(); qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount(); chart->scroll(x, 0);
This is the effect after running:
It is worth mentioning that I need to frequently modify the content displayed by the bar later, so in this demo I write the output of the bar later. -
@JonB Oh! I saw the form, but it's so weird, first of all, the code in form
series->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30;
doesn't work properly, but the form*bar << 1 << 2 << 3; series->append(bar); chart->addSeries(series); *bar << 20 << 5 << 30;
does, I think it's weird. Secondly, the display of this chart looks so strange, I found that the displayed content is related tom_axisX->setTickCount(1);
andqreal x = chart->plotArea().width() / m_axisX->tickCount();chart->scroll(x, 0);
, but what confuses me is that it seems that I should not manually assign a value tochart->scroll(x, 0)
, Because I can't seem to calculate how much I should scroll.
The complete code is as follows:auto view = new QChartView(); auto chart = new QChart(); auto series = new QBarSeries(); auto bar = new QBarSet(""); setCentralWidget(view); view->setChart(chart); QValueAxis *m_axisX = new QValueAxis(); QValueAxis *m_axisY = new QValueAxis(); chart->addAxis(m_axisX,Qt::AlignBottom); chart->addAxis(m_axisY,Qt::AlignLeft); *bar << 1 << 2 << 3; series->append(bar); chart->addSeries(series); *bar << 20 << 5 << 30; m_axisX->setTickCount(1); m_axisX->setRange(-500, 1000); m_axisY->setRange(0, 10000); qreal x = chart->plotArea().width() / m_axisX->tickCount(); chart->scroll(x, 0);
The running effect is as follows:
-
@printfne
My code does not use any of tick counts, plat area sizes or scrolling. So that is specific to the example you copied from. If you stick with what you have you will have to play with the values if you want all 4 bars to have the same visible width as each other. -
@JonB That is to say, I have to manually specify the range of
chart->scroll
?Also I have another question, in the above code, I used the code
*bar << 1 << 2 << 3;series->append(bar);chart->addSeries(series);*bar< < 20 << 5 << 30;
, he can work normally, let’s call it code A, and use the codeseries->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30;
, it cannot be displayed normally, let’s call it code B for now. Here code A can run normally, that is to say, afterseries-append
andchart->addSeries
The code*bar << num1 << num2
can be displayed, but in code B, this is not displayed, what is the reason What caused it? -
@printfne
My understanding is the same as I keep saying. When you add new points to a chart after it has been shown, it's not thatQChartView
doesn't update, it's that its current axes --- which were set up when the initial points were added before being added to the chart --- are out of range for the new point. In some shape or form, you need to rescale the axes horizontally and/or vertically so that they include the new point(s) within their range.Keep your horizontal & vertical axes visible at least while you develop. If you do not see a new point that you added, look to see whether it is outside the current horizontal/vertical ranges. That is what my code was doing and it displays the new points in real time without my having to write any extra "updating/repainting" calls.
-
-
-