<?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[QChart -problem with time axis X]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I did a class with my own QChart. I would like incoming messages to be displayed in a chart. I would like the X axis to show the time when value was added. I also made the chart in such a way that it was dynamic. Unfortunately , when it is displayed, the X axis is not shown to me and I do not know what it may be coused by. I used timer as a messaging simulator.</p>
<p dir="auto">My .cpp file</p>
<pre><code>#include "chart.h"
#include &lt;QtCharts/QAbstractAxis&gt;
#include &lt;QtCharts/QSplineSeries&gt;
#include &lt;QtCharts/QValueAxis&gt;
#include &lt;QtCore/QRandomGenerator&gt;
#include &lt;QtCore/QDebug&gt;

Chart::Chart(const QColor color, const qreal minYRange, const qreal maxYRange, const QString &amp;units, QGraphicsItem *parent, Qt::WindowFlags wFlags):
    QChart(QChart::ChartTypeCartesian, parent, wFlags),
    series(nullptr),
    axisX(new QDateTimeAxis()),
    axisY(new QValueAxis()),
    m_step(0),
    m_x(9),
    m_y(1)
{
    QObject::connect(&amp;m_timer, &amp;QTimer::timeout, this, &amp;Chart::handleTimeout);
    m_timer.setInterval(1000);

    visualConfig();

    series = new QSplineSeries(this);
    QPen green(color);
    green.setWidth(3);
    series-&gt;setPen(green);

    series-&gt;append(QDateTime::currentDateTime().toMSecsSinceEpoch(), m_y);

    addSeries(series);

    addAxis(axisX,Qt::AlignBottom);
    addAxis(axisY,Qt::AlignLeft);
    series-&gt;attachAxis(axisX);
    series-&gt;attachAxis(axisY);
    axisX-&gt;setTickCount(12);
    axisX-&gt;setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime());
    axisX-&gt;setFormat("h mm");


    axisY-&gt;setRange(minYRange, maxYRange);
    axisY-&gt;setTickCount(5);
    axisY-&gt;setTitleText(units);

    m_timer.start();
}

Chart::~Chart()
{

}

void Chart::handleTimeout()
{
    QPointF last_sample = series-&gt;at(series-&gt;count()-1);
    qreal last_time = last_sample.x();

    QDateTime actual_time = QDateTime::currentDateTime();
    m_y = QRandomGenerator::global()-&gt;bounded(10, 50);
    series-&gt;append(actual_time.toMSecsSinceEpoch(), m_y);

    qreal time_difference = actual_time.toMSecsSinceEpoch() - last_time;
    scroll(time_difference, 0);
}

void Chart::visualConfig()
{
    setMargins(QMargins(0,0,0,0));
    legend()-&gt;hide();
    setAnimationOptions(QChart::AllAnimations);

    setBackgroundVisible(false);
    axisX-&gt;setGridLineVisible(false);
    axisY-&gt;setGridLineVisible(false);

    QFont labelsFont;
    labelsFont.setPixelSize(10);
    axisX-&gt;setLabelsFont(labelsFont);
    axisY-&gt;setLabelsFont(labelsFont);

    axisX-&gt;setLabelsBrush(Qt::white);
    axisY-&gt;setLabelsBrush(Qt::white);
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/117442/qchart-problem-with-time-axis-x</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 17:24:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/117442.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 28 Jul 2020 12:34:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QChart -problem with time axis X on Tue, 28 Jul 2020 18:28:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/creatorczyk">@<bdi>Creatorczyk</bdi></a><br />
I don't know.  Did you try removing lines of code till it did work?  Does the Y axis show?  Is <code>axisX-&gt;setLabelsBrush(Qt::white);</code> by any chance writing on a while background?  In <code> axisX-&gt;setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime());</code> is it a good idea to set a range where the minimum &amp; maximum are equal?</p>
]]></description><link>https://forum.qt.io/post/609313</link><guid isPermaLink="true">https://forum.qt.io/post/609313</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 28 Jul 2020 18:28:51 GMT</pubDate></item></channel></rss>