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. Create a SerialPortReader and a Chart in the same main function
QtWS25 Last Chance

Create a SerialPortReader and a Chart in the same main function

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 329 Views
  • 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.
  • D Offline
    D Offline
    Denta1000
    wrote on last edited by
    #1

    Hi,

    I want to create a splinechart whose series is built from serialport datas.
    My serialport part is running well. The SerialPortReader is called in the main.

    But when I try to create a QMainWindow in the main, the application will wether stop, wether display "QWidget: Cannot create a QWidget without QApplication"

    So my question is : Can I create a second application in the main and associate it with the window ?

    Or can I create the chart in the same application as the SerialPort ?

    Thanks for your help

    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

      @Denta1000

      So my question is : Can I create a second application in the main and associate it with the window ?
      

      As the problem statement says "reading the data from serial port and displaying in the splinechart", it is better to have single application integrated with both the features.

      Or can I create the chart in the same application as the SerialPort ?
      

      Yes, Have you implemented the features and facing any issues?

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Denta1000
        wrote on last edited by
        #3

        @nagesh

        Here is my main function :

        #include "serialportreader.h"
        //#include "themewidget.h"
        #include <QCoreApplication>
        #include <QSerialPort>
        #include <QStringList>
        #include <QTextStream>
        #include <QDebug>
        #include <QLine>
        #include <QtWidgets>
        #include <QtCharts>
        #include <QVector>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication coreApplication(argc, argv);
            const int argumentCount = QCoreApplication::arguments().size();
            const QStringList argumentList = QCoreApplication::arguments();
        
            QTextStream standardOutput(stdout);
        
            if (argumentCount == 1) {
                standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]")
                                  .arg(argumentList.first())
                               << "\n";
                return 1;
            }
        
            DataTable data_test(nullptr);
        
            QSerialPort serialPort;
            const QString serialPortName = argumentList.at(1);
            serialPort.setPortName(serialPortName);
        
            const int serialPortBaudRate = (argumentCount > 2)
                    ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
            serialPort.setBaudRate(serialPortBaudRate);
        
        //  Test perso
            qDebug() << "Connection " << serialPortName << "succeeded , baudrate : " << serialPortBaudRate << "\n";
        
        //
            if (!serialPort.open(QIODevice::ReadOnly)) {
                standardOutput << QObject::tr("Failed to open port %1, error: %2")
                                  .arg(serialPortName)
                                  .arg(serialPort.errorString())
                               << "\n";
                return 1;
            }
        
            QMainWindow window;
            QChart *chart = new QChart;
            chart->setTitle("Dynamic spline chart");
            chart->legend()->hide();
            chart->setAnimationOptions(QChart::AllAnimations);
            QChartView chartView(chart);
            chartView.setRenderHint(QPainter::Antialiasing);
            window.setCentralWidget(&chartView);
            window.resize(400, 300);
            window.show();
        
            qDebug() << "Initiating SerialPortReader \n";
            SerialPortReader serialPortReader(&serialPort, &data_test);
            //serial_start(&serialPort, &coreApplication);
        
            qDebug() << "Exit Serialport \n";
        
            /*while(1)
            {
                series->append(data_test.temps[data_test.temps.size()-1], data_test.altitude[data_test.temps.size()-1]);
            }*/
        
            return coreApplication.exec();
        }
        
        

        When the application reach the QMainWindow window line, it blocks and stop the application. So I wondered if the two Objects (chart and serialport) are able to run at the same time ?

        JonBJ 1 Reply Last reply
        0
        • D Denta1000

          @nagesh

          Here is my main function :

          #include "serialportreader.h"
          //#include "themewidget.h"
          #include <QCoreApplication>
          #include <QSerialPort>
          #include <QStringList>
          #include <QTextStream>
          #include <QDebug>
          #include <QLine>
          #include <QtWidgets>
          #include <QtCharts>
          #include <QVector>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication coreApplication(argc, argv);
              const int argumentCount = QCoreApplication::arguments().size();
              const QStringList argumentList = QCoreApplication::arguments();
          
              QTextStream standardOutput(stdout);
          
              if (argumentCount == 1) {
                  standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]")
                                    .arg(argumentList.first())
                                 << "\n";
                  return 1;
              }
          
              DataTable data_test(nullptr);
          
              QSerialPort serialPort;
              const QString serialPortName = argumentList.at(1);
              serialPort.setPortName(serialPortName);
          
              const int serialPortBaudRate = (argumentCount > 2)
                      ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
              serialPort.setBaudRate(serialPortBaudRate);
          
          //  Test perso
              qDebug() << "Connection " << serialPortName << "succeeded , baudrate : " << serialPortBaudRate << "\n";
          
          //
              if (!serialPort.open(QIODevice::ReadOnly)) {
                  standardOutput << QObject::tr("Failed to open port %1, error: %2")
                                    .arg(serialPortName)
                                    .arg(serialPort.errorString())
                                 << "\n";
                  return 1;
              }
          
              QMainWindow window;
              QChart *chart = new QChart;
              chart->setTitle("Dynamic spline chart");
              chart->legend()->hide();
              chart->setAnimationOptions(QChart::AllAnimations);
              QChartView chartView(chart);
              chartView.setRenderHint(QPainter::Antialiasing);
              window.setCentralWidget(&chartView);
              window.resize(400, 300);
              window.show();
          
              qDebug() << "Initiating SerialPortReader \n";
              SerialPortReader serialPortReader(&serialPort, &data_test);
              //serial_start(&serialPort, &coreApplication);
          
              qDebug() << "Exit Serialport \n";
          
              /*while(1)
              {
                  series->append(data_test.temps[data_test.temps.size()-1], data_test.altitude[data_test.temps.size()-1]);
              }*/
          
              return coreApplication.exec();
          }
          
          

          When the application reach the QMainWindow window line, it blocks and stop the application. So I wondered if the two Objects (chart and serialport) are able to run at the same time ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Denta1000 said in Create a SerialPortReader and a Chart in the same main function:

          QCoreApplication coreApplication(argc, argv);

          When the application reach the QMainWindow window line, it blocks and stop the application.

          https://doc.qt.io/qt-5/qcoreapplication.html#details

          The QCoreApplication class provides an event loop for Qt applications without UI.

          Is this a wise choice when you want a QMainWindow/QChartView?

          D 1 Reply Last reply
          3
          • JonBJ JonB

            @Denta1000 said in Create a SerialPortReader and a Chart in the same main function:

            QCoreApplication coreApplication(argc, argv);

            When the application reach the QMainWindow window line, it blocks and stop the application.

            https://doc.qt.io/qt-5/qcoreapplication.html#details

            The QCoreApplication class provides an event loop for Qt applications without UI.

            Is this a wise choice when you want a QMainWindow/QChartView?

            D Offline
            D Offline
            Denta1000
            wrote on last edited by
            #5

            @JonB

            In fact, I needed a QApplication and not a QcoreApplication , as the error message said...
            Thanks for you help

            1 Reply Last reply
            0

            • Login

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