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());}
-
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
-
#define TCPCONSOLE_H
#include <QPlainTextEdit>
#include <QDebug>class TCPConsole : public QPlainTextEdit
{
Q_OBJECTsignals:
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?
-
@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...
-
@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. -
Hi,
Sure it is, convert it to a QString. Just beware that depending on the content, it might have surprising results.