Create a SerialPortReader and a Chart in the same main function
-
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
-
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?
-
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 ?
-
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 ?
@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
QCoreApplicationclass provides an event loop for Qt applications without UI.Is this a wise choice when you want a
QMainWindow/QChartView? -
@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
QCoreApplicationclass provides an event loop for Qt applications without UI.Is this a wise choice when you want a
QMainWindow/QChartView?