Important: Please read the Qt Code of Conduct -

How to get data read from tcp to display on Qchart?

  • I am new to QT and in desperate help for a complicating project. My TCPconsole can display the data that i sent through hercules. However, I am unable and unsure how to use the data to display on the chart in my Qwidget. I tried to open a file as well but it cant be opened. Pls help me out.

    void ThemeWidget::populateTextBrowser()
    // add items to legend combobox
    //QStringList strings;
    //for (int b=0;b<=20;b++){
    // QString data;
    // strings<<data;
    QFile files("C:\Users\User\Desktop\Log_dp05.txt");
    QTextStream in(&files);


  • Lifetime Qt Champion

    Hi and welcome to the forums

    Its not clear if you want to read from TCP or from a file.
    Also , you talk about QChart, but then use a text browser in code.

    Also what type of chart do you want ?

    Did you look at the examples ?

  • @mrjj hi, I want to read my data from TCP, however I cant seem to do it. I would like to use QLinechart but i dont seem to be able to append my data from TCP to chart.

    QChart *ThemeWidgetMedium::createLineChart() const

    QChart *chart = new QChart();
    chart->setTitle("Line chart");
    QString name("Series ");
    int nameIndex = 0;
    for (const DataList &list : m_dataTable) {
        QLineSeries *series = new QLineSeries(chart);
        for (const Data &data : list)
        series->setName(name + QString::number(nameIndex));
    chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);
    chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);
    // Add space to label to add space between labels and axis
    QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
    axisY->setLabelFormat("%.1f  ");
    return chart;


    I felt that I cant get the data to enter my themewidget, hence i decided to work on displaying my data on textbrowser to ensure that my data is in the file.

    QStringList strings;
    for (int b=0;b<=20;b++){

    but the data array is highlighted meaning the data is not located inside my widget. hope this is better

  • Lifetime Qt Champion

    @bunnyb Where do you read data from TCP?

  • @jsulm its in another .cpp file

    void TCPConsole::putData(const QByteArray &data)

    QScrollBar *bar = verticalScrollBar();


  • Lifetime Qt Champion

    @bunnyb I don't see anything related to TCP in that code...

  • #define TCPCONSOLE_H

    #include <QPlainTextEdit>
    #include <QDebug>

    class TCPConsole : public QPlainTextEdit

    void getData(const QByteArray &data);

    explicit TCPConsole(QWidget *parent = nullptr);

    void putData(const QByteArray &data);
    void setLocalEchoEnabled(bool set);

    void keyPressEvent(QKeyEvent *e) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseDoubleClickEvent(QMouseEvent *e) override;
    void contextMenuEvent(QContextMenuEvent *e) override;

    bool m_localEchoEnabled = false;

    #endif // TCPCONSOLE_H

    can i check usually the data is get from the function i call that is public means all files can use? or signals?

  • Lifetime Qt Champion

    @bunnyb Sorry, but this also has absolutely nothing to do with reading data from TCP! So, do you really read data from network (TCP) or what do you mean if you say TCP?

    "can i check usually the data is get from the function i call that is public means all files can use?" - can you please rephrase? I don't understand this. Public method can be called by everyone who has access to the instance...

  • oh i see. May i know how QT5 printf in widget? Example in python we use print("hello world");

  • Lifetime Qt Champion

    @bunnyb said in How to get data read from tcp to display on Qchart?:

    printf in widget?

    What widget do you mean exactly? If you want to show some text in your UI you can use
    If you want to print to stdout simply use std::cout.

  • Based on my understanding, qlabel, textbrowser only takes in QString correct? If i would like to display a QArrayByte is it possible?

  • Lifetime Qt Champion


    Sure it is, convert it to a QString. Just beware that depending on the content, it might have surprising results.

Log in to reply