<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Update QChart in real time (QtWidgets)]]></title><description><![CDATA[<p dir="auto">Hello!!</p>
<p dir="auto">I am trying to use an histogram associated to a video streaming so, I want to update the chart frame by frame.<br />
I have a QPushButton associated to a signal wich shows the histogram with this slot:</p>
<pre><code>void MainWindow::createHistogram(bool status) {
    if (status) {
        showHist = true;

        chartHist = new QChart();

        int histSize[1] = {256}; // number of bins
        float hranges[2] = {0.0, 255.0}; // min andax pixel value
        const float* ranges[1] = {hranges};
        int channels[1] = {0}; // only 1 channel used

        cv::MatND hist;

        cv::Mat ret;
        imageFunctions::qimage_to_mat(imageViewer-&gt;item-&gt;pixmap().toImage(), ret);

        cv::calcHist(&amp;ret, 1, channels, cv::Mat(), hist, 1, histSize, ranges);

        series = new QtCharts::QBarSet("");

        for (int h = 0; h &lt; histSize[0]; ++h) {
            float bin_value = hist.at&lt;float&gt;(h);
            series-&gt;append(bin_value);
            if (valueHistMax &lt; bin_value) valueHistMax = bin_value;
        }

        QPen pen(0x09418B);
        pen.setWidth(3);
        series-&gt;setPen(pen);

        QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
        barseries-&gt;append(series);

        chartHist-&gt;removeAllSeries();
        chartHist-&gt;addSeries(barseries);
        chartHist-&gt;setAnimationOptions(QChart::SeriesAnimations);
        chartHist-&gt;legend()-&gt;setVisible(false);
        chartHist-&gt;setMaximumWidth(100);
        chartHist-&gt;setMargins(QMargins(0,0,4,0));
        chartHist-&gt;setAcceptHoverEvents(true);
        chartHist-&gt;layout()-&gt;setContentsMargins(0,0,0,0);
        chartHist-&gt;setBackgroundRoundness(0);

        ui-&gt;histLayout-&gt;addWidget(chartViewHist);
    } else {
        delete series;
        delete chartHist;
        delete chartViewHist;
        showHist = false;
    }
}
</code></pre>
<p dir="auto">And when I receive the next frame I call this function:</p>
<pre><code>void MainWindow::updateHistogram(cv::Mat *image) {

    int histSize[1] = {256}; // number of bins
    float hranges[2] = {0.0, 255.0}; // min andax pixel value
    const float* ranges[1] = {hranges};
    int channels[1] = {0}; // only 1 channel used

    cv::MatND hist;
    cv::calcHist(image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);

    //series = new QtCharts::QBarSet("");

    for (int h = 0; h &lt; histSize[0]; ++h) {
        float bin_value = hist.at&lt;float&gt;(h);
        series-&gt;replace(h, bin_value);
        //series-&gt;append(bin_value);
        if (valueHistMax &lt; bin_value) valueHistMax = bin_value;
    }

    QPen pen(0x09418B);
    pen.setWidth(3);
    series-&gt;setPen(pen);

    QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
    barseries-&gt;append(series);

    chartHist-&gt;removeAllSeries();
    chartHist-&gt;addSeries(barseries);

    //clearLayout(ui-&gt;histLayout);
    //ui-&gt;histLayout-&gt;addWidget(chartViewHist);
}
</code></pre>
<p dir="auto">But this gives me a memory access error. I don't know how to do it. What could be wrong here?</p>
<p dir="auto">Thank you very much!!</p>
]]></description><link>https://forum.qt.io/topic/87204/update-qchart-in-real-time-qtwidgets</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 17:09:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/87204.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 Jan 2018 11:20:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Update QChart in real time (QtWidgets) on Fri, 26 Jan 2018 11:59:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> I test in debug mode, and appears that. I was debugging line by line and only works the first time. The next calls of the function don't work.</p>
]]></description><link>https://forum.qt.io/post/438696</link><guid isPermaLink="true">https://forum.qt.io/post/438696</guid><dc:creator><![CDATA[ivanicy]]></dc:creator><pubDate>Fri, 26 Jan 2018 11:59:46 GMT</pubDate></item><item><title><![CDATA[Reply to Update QChart in real time (QtWidgets) on Fri, 26 Jan 2018 11:53:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ivanicy">@<bdi>ivanicy</bdi></a> Are you testing with a release build? Build in debug mode and try again.</p>
]]></description><link>https://forum.qt.io/post/438694</link><guid isPermaLink="true">https://forum.qt.io/post/438694</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 26 Jan 2018 11:53:32 GMT</pubDate></item><item><title><![CDATA[Reply to Update QChart in real time (QtWidgets) on Fri, 26 Jan 2018 11:52:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> I don't know where. Is one of these times that appears an hexadecimal window, Qt doesn't show where is it crashed:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/448b9c32-6b34-40f2-930e-268555b296ec.png" alt="0_1516967482301_fc1452c1-c6b7-4f1d-845f-27e379b58f81-imagen.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">I have this lines in the MainWindow constructor:</p>
<pre><code>    chartHist = new QChart();
    chartViewHist = new QChartView(chartHist);
    chartViewHist-&gt;setRenderHint(QPainter::Antialiasing);
    chartViewHist-&gt;setMaximumWidth(100);
</code></pre>
<p dir="auto">I am wondering that it is crashing when I do the instantiation of the objects but I don't know.</p>
]]></description><link>https://forum.qt.io/post/438693</link><guid isPermaLink="true">https://forum.qt.io/post/438693</guid><dc:creator><![CDATA[ivanicy]]></dc:creator><pubDate>Fri, 26 Jan 2018 11:52:10 GMT</pubDate></item><item><title><![CDATA[Reply to Update QChart in real time (QtWidgets) on Fri, 26 Jan 2018 11:46:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ivanicy">@<bdi>ivanicy</bdi></a> said in <a href="/post/438686">Update QChart in real time (QtWidgets)</a>:</p>
<blockquote>
<p dir="auto">memory access error</p>
</blockquote>
<p dir="auto">Where exactly? Did you try to debug? Did you check the stack trace when it crashes?</p>
]]></description><link>https://forum.qt.io/post/438690</link><guid isPermaLink="true">https://forum.qt.io/post/438690</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 26 Jan 2018 11:46:44 GMT</pubDate></item></channel></rss>