Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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);
    m_ui->textBrowser->setText(in.readAll());

    }


  • 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 ?
    https://doc-snapshots.qt.io/qt5-5.15/qtcharts-examples.html



  • @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->append(data.first);
        series->setName(name + QString::number(nameIndex));
        nameIndex++;
        chart->addSeries(series);
    }
    
    chart->createDefaultAxes();
    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());
    Q_ASSERT(axisY);
    axisY->setLabelFormat("%.1f  ");
    //![4]
    
    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++){
        strings<<data[b];
        m_ui->textBrowser->setText(strings.join("\n"));
    }
    

    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)
    {
    insertPlainText(data);

    QScrollBar *bar = verticalScrollBar();
    bar->setValue(bar->maximum());
    

    }


  • 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
    {
    Q_OBJECT

    signals:
    void getData(const QByteArray &data);

    public:
    explicit TCPConsole(QWidget *parent = nullptr);

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

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

    private:
    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 https://doc.qt.io/qt-5/qlabel.html
    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

    Hi,

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


Log in to reply