Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get data read from tcp to display on Qchart?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 812 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bunnyb
    wrote on 1 Mar 2021, 13:40 last edited by
    #1

    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());

    }

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Mar 2021, 13:50 last edited by mrjj 3 Jan 2021, 13:51
      #2

      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

      B 1 Reply Last reply 1 Mar 2021, 14:04
      1
      • M mrjj
        1 Mar 2021, 13:50

        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

        B Offline
        B Offline
        bunnyb
        wrote on 1 Mar 2021, 14:04 last edited by
        #3

        @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

        J 1 Reply Last reply 1 Mar 2021, 14:05
        0
        • B bunnyb
          1 Mar 2021, 14:04

          @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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 1 Mar 2021, 14:05 last edited by
          #4

          @bunnyb Where do you read data from TCP?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 1 Mar 2021, 14:14
          0
          • J jsulm
            1 Mar 2021, 14:05

            @bunnyb Where do you read data from TCP?

            B Offline
            B Offline
            bunnyb
            wrote on 1 Mar 2021, 14:14 last edited by
            #5

            @jsulm its in another .cpp file

            void TCPConsole::putData(const QByteArray &data)
            {
            insertPlainText(data);

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

            }

            J 1 Reply Last reply 1 Mar 2021, 14:24
            0
            • B bunnyb
              1 Mar 2021, 14:14

              @jsulm its in another .cpp file

              void TCPConsole::putData(const QByteArray &data)
              {
              insertPlainText(data);

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

              }

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 1 Mar 2021, 14:24 last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bunnyb
                wrote on 1 Mar 2021, 14:41 last edited by
                #7

                #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?

                J 1 Reply Last reply 1 Mar 2021, 14:46
                0
                • B bunnyb
                  1 Mar 2021, 14:41

                  #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?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 1 Mar 2021, 14:46 last edited by
                  #8

                  @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...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bunnyb
                    wrote on 1 Mar 2021, 14:51 last edited by
                    #9

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

                    J 1 Reply Last reply 1 Mar 2021, 14:53
                    0
                    • B bunnyb
                      1 Mar 2021, 14:51

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 1 Mar 2021, 14:53 last edited by
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bunnyb
                        wrote on 1 Mar 2021, 15:01 last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 1 Mar 2021, 19:24 last edited by
                          #12

                          Hi,

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          1/12

                          1 Mar 2021, 13:40

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved