Solved QDateTimeAxisexample work but not my project, I don't see why???
-
Hello, from QDateTimeAxis example I test this code and working fine:
/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCore/QDateTime> #include <QtCore/QFile> #include <QtCore/QTextStream> #include <QtCore/QDebug> #include <QtCharts/QValueAxis> #include <QtCharts/QDateTimeAxis> QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a(argc, argv); QString heure; qreal temp; qreal hygro; QString date; QString line; QString loaded; int line_count=0; //![1] QLineSeries *temperature = new QLineSeries(); QLineSeries *hygrometrie = new QLineSeries(); QFile fileOut; fileOut.setFileName("D:/Temp_Hygro_temp.txt"); if(fileOut.exists()) { fileOut.remove(); } QFile::copy("D:/Magasin_Temp_Hygro.txt","D:/Temp_Hygro_temp.txt"); fileOut.open(QIODevice::ReadOnly | QIODevice::Text); QString format="dd/MM/yy hh'h'mm "; while( !fileOut.atEnd())//loop until end of QTextStrem { line=fileOut.readLine(); if(line.size()>=47) { date=line.split(":")[0]; QString format="dd/MM/yyhh'h'mm"; QDateTime dateTime=QDateTime::fromString(date.replace(" ",""),format).addYears(100); bool ok = false; temp=line.split(":")[2].toDouble(&ok); hygro=line.split(":")[4].toDouble(&ok); temperature->append(dateTime.toMSecsSinceEpoch(), temp); hygrometrie->append(dateTime.toMSecsSinceEpoch(), hygro); } line_count++; } fileOut.close(); fileOut.remove(); //![2] //![3] QChart *chart = new QChart(); chart->addSeries(temperature); chart->addSeries(hygrometrie); chart->legend()->hide(); //chart->setTitle("Sunspots count (by Space Weather Prediction Center)"); //![3] //![4] QDateTimeAxis *axisX = new QDateTimeAxis; axisX->setTickCount(10); axisX->setFormat("hh'h'mm"); axisX->setTitleText("Date"); chart->addAxis(axisX, Qt::AlignBottom); temperature->attachAxis(axisX); hygrometrie->attachAxis(axisX); QValueAxis *axisY = new QValueAxis; axisY->setLabelFormat("%i"); //axisY->setTitleText("Sunspots count"); chart->addAxis(axisY, Qt::AlignLeft); temperature->attachAxis(axisY); QValueAxis *axisYhygro = new QValueAxis; axisY->setLabelFormat("%i"); //axisY->setTitleText("Sunspots count"); chart->addAxis(axisYhygro, Qt::AlignRight); hygrometrie->attachAxis(axisYhygro); //![4] //![5] QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); //![5] //![6] QMainWindow window; window.setCentralWidget(chartView); window.resize(820, 600); window.show(); //![6] return a.exec(); }
but when I try to do the same (only with one series first for test) I have the result:
I'm not able to see where is my mistake after 3 hours searching, could you help me?
here my .h
/**************************************************************************** ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QWinTaskbarButton> #include <QSystemTrayIcon> #include <QDateTime> #include <QChartView> #include <QLineSeries> #include <QValueAxis> #include <QDateTimeAxis> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE QT_CHARTS_USE_NAMESPACE class SettingsDialog; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); //~MainWindow(); qreal temp; QString date; QString loaded; QString line; int line_count; QLineSeries *temperature = new QLineSeries(); QChart *chart = new QChart(); QDateTimeAxis *axisX = new QDateTimeAxis; QValueAxis *axisY = new QValueAxis; QChartView *chartView = new QChartView(chart); void on_actionGraph(); private slots: private: Ui::MainWindow *m_ui = nullptr; }; #endif // MAINWINDOW_H
my .cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QLabel> #include <QMessageBox> #include <QSystemTrayIcon> #include <QIcon> #include <QPixmap> #include <QFileInfo> #include <QDateTime> #include <QByteArray> #include <QKeyEvent> #include <QDateTimeAxis> QT_CHARTS_USE_NAMESPACE //! [0] MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { m_ui->setupUi(this); line_count=0; temp=0; chart->addSeries(temperature); chart->legend()->hide(); axisX->setTickCount(10); axisX->setFormat("hh'h'mm"); axisX->setTitleText("Date"); chart->addAxis(axisX, Qt::AlignBottom); temperature->attachAxis(axisX); axisY->setLabelFormat("%i"); chart->addAxis(axisY, Qt::AlignLeft); temperature->attachAxis(axisY); axisY->setLabelFormat("%i"); chartView->setRenderHint(QPainter::Antialiasing); setCentralWidget(chartView); resize(820, 600); loaded="unloaded"; } void MainWindow::on_actionGraph() { if(loaded=="unloaded") { QFile fileOut; fileOut.setFileName("D:/Temp_Hygro_temp.txt"); if(fileOut.exists()) { fileOut.remove(); } QFile::copy("D:/Magasin_Temp_Hygro.txt","D:/Temp_Hygro_temp.txt"); fileOut.open(QIODevice::ReadOnly | QIODevice::Text); QString format="dd/MM/yy hh'h'mm "; while( !fileOut.atEnd())//loop until end of QTextStrem { line=fileOut.readLine(); if(line.size()>=47) { date=line.split(":")[0]; QString format="dd/MM/yyhh'h'mm"; QDateTime dateTime=QDateTime::fromString(date.replace(" ",""),format).addYears(100); bool ok = false; temp=line.split(":")[2].toDouble(&ok); temperature->append(dateTime.toMSecsSinceEpoch(), temp); qDebug()<<date; } line_count++; } fileOut.close(); fileOut.remove(); loaded="loaded"; } }
my main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; // w.setWindowFlags(Qt::WindowTitleHint); //| Qt::WindowMinimizeButtonHint // | Qt::WindowMaximizeButtonHint // | Qt::WindowCloseButtonHint); w.show(); w.on_actionGraph(); return a.exec(); }
thank you very much for you help!!
-
Did you check that you actually read something from your file?
-
@Christian-Ehrlicher yes and I progress to solve the problem.
with add series and axis only after upload the series it's working.
if(loaded=="unloaded") { QFile fileOut; fileOut.setFileName("D:/Temp_Hygro_temp.txt"); if(fileOut.exists()) { fileOut.remove(); } QFile::copy("D:/Magasin_Temp_Hygro.txt","D:/Temp_Hygro_temp.txt"); fileOut.open(QIODevice::ReadOnly | QIODevice::Text); QString format="dd/MM/yy hh'h'mm "; while( !fileOut.atEnd())//loop until end of QTextStrem { line=fileOut.readLine(); if(line.size()>=47) { date=line.split(":")[0]; QString format="dd/MM/yyhh'h'mm"; QDateTime dateTime=QDateTime::fromString(date.replace(" ",""),format).addYears(100); bool ok = false; temp=line.split(":")[2].toDouble(&ok); hygro=line.split(":")[4].toDouble(&ok); temperature->append(dateTime.toMSecsSinceEpoch(), temp); hygrometrie->append(dateTime.toMSecsSinceEpoch(), hygro); //qDebug()<<date; } line_count++; } chart->addSeries(temperature); chart->addSeries(hygrometrie); chart->addAxis(axisX, Qt::AlignBottom); chart->addAxis(axisYtemp, Qt::AlignLeft); chart->addAxis(axisYhygro, Qt::AlignRight); temperature->attachAxis(axisX); temperature->attachAxis(axisYtemp); temperature->attachAxis(axisYhygro); chartView->repaint(); fileOut.close(); fileOut.remove(); loaded="loaded"; }
but updating value with temperature->append(dateTime.toMSecsSinceEpoch(), temp); doesn't refresh the chart...
I try:
temperature->detachAxis(axisYtemp); temperature->detachAxis(axisX); chart->removeAllSeries(); chart->removeAxis(axisX); chart->removeAxis(axisYtemp); chart->removeAxis(axisYhygro); temperature->append(dateTime.toMSecsSinceEpoch(), temp); hygrometrie->append(dateTime.toMSecsSinceEpoch(), hygro); chart->addSeries(temperature); chart->addSeries(hygrometrie); chart->addAxis(axisX, Qt::AlignBottom); chart->addAxis(axisYtemp, Qt::AlignLeft); chart->addAxis(axisYhygro, Qt::AlignRight); temperature->attachAxis(axisX); temperature->attachAxis(axisYtemp); temperature->attachAxis(axisYhygro); chartView->repaint();
but it make my project crash
-
@filipdns said in QDateTimeAxisexample work but not my project, I don't see why???:
dateTime
Did you check that dateTime has the correct value?
-
@Christian-Ehrlicher Found where is the problem.
chart->addAxis(axisX, Qt::AlignBottom); chart->addAxis(axisYtemp, Qt::AlignLeft); chart->addAxis(axisYhygro, Qt::AlignRight); chart->addSeries(temperature); chart->addSeries(hygrometrie); temperature->attachAxis(axisX); hygrometrie->attachAxis(axisX); temperature->attachAxis(axisYtemp); hygrometrie->attachAxis(axisYhygro); chartView->setChart(chart);
have to be after the first append of series.
And to refresh this type of chart (using QDateTimeAxis as axisX) after each append, we have to set the new record date with axisX->setMax(dateTime);
here my .h/**************************************************************************** ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QWinTaskbarButton> #include <QSystemTrayIcon> #include <QDateTime> #include <QChartView> #include <QLineSeries> #include <QValueAxis> #include <QDateTimeAxis> #include <QtCore/QTimer> #include <QWheelEvent> QT_BEGIN_NAMESPACE class QLabel; namespace Ui { class MainWindow; } QT_END_NAMESPACE QT_CHARTS_USE_NAMESPACE class QSystemTrayIcon; class SettingsDialog; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); struct Settings { }; void openSerialPort(); void closeSerialPort(); void alarmParameter(); void showAlarm(); double deltahygro; double deltatemp; QIcon icon; QString streamOut2; double temp; double hygro; double tempmin; double hygromin; double tempmax; double hygromax; QString date; QString line; QString loaded; int line_count; QDateTime dateTimeOld2; QDateTime record; QDateTime recordOld; QByteArray realTimeData; QString alarmtempMin; QString alarmtempMax; QString alarmhygroMin; QString alarmhygroMax; QByteArray inputData; QDateTime dateTime; QLineSeries *temperature; QLineSeries *hygrometrie; QChart *chart; QDateTimeAxis *axisX; QValueAxis *axisYtemp; QValueAxis *axisYhygro; QChartView *chartView; public slots: private slots: void changeTexte1(); void quitMyApp(); void about(); void readData(const QByteArray &data); void handleError(QSerialPort::SerialPortError error); void on_actionGraph(); private: QTimer m_timer; QLabel* text; void initActionsConnections(); void showStatusMessage(const QString &message); Ui::MainWindow *m_ui = nullptr; QSystemTrayIcon *tray; QLabel *m_status = nullptr; SettingsDialog *m_settings = nullptr; QSerialPort *m_serial = nullptr; protected: //bool viewportEvent(QEvent *event); //void mousePressEvent(QMouseEvent *event); //void mouseMoveEvent(QMouseEvent *event); //void mouseReleaseEvent(QMouseEvent *event); void keyPressEvent(QKeyEvent *event); }; #endif // MAINWINDOW_H
here my full .cpp
/**************************************************************************** ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "mainwindow.h" #include "ui_mainwindow.h" #include "settingsdialog.h" #include <QDebug> #include <QLabel> #include <QMessageBox> #include <QSystemTrayIcon> #include <QIcon> #include <QPixmap> #include <QFileInfo> #include <QDateTime> #include <QByteArray> #include <QKeyEvent> #include <QDateTimeAxis> #include <QCloseEvent> QT_CHARTS_USE_NAMESPACE //! [0] MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), temperature(new QLineSeries), hygrometrie(new QLineSeries), chart(new QChart), axisX(new QDateTimeAxis), axisYtemp(new QValueAxis), axisYhygro(new QValueAxis), chartView(new QChartView(chart)), m_ui(new Ui::MainWindow), m_status(new QLabel), m_settings(new SettingsDialog), m_serial(new QSerialPort(this)) { m_ui->setupUi(this); m_ui->actionConnect->setEnabled(true); m_ui->actionQuit->setEnabled(true); m_ui->actionConfigure->setEnabled(true); m_ui->statusBar->addWidget(m_status); line_count=0; hygro=0; temp=0; tempmin=100; hygromin=100; tempmax=0; hygromax=0; tray= new QSystemTrayIcon(this); icon.addFile(":/images/thermometerwidget.png"); QMenu* menu = new QMenu(this); QAction* actTexte1 = new QAction("Open",this); menu->addAction(actTexte1); tray->setIcon(icon); tray->setVisible(true); tray->setContextMenu(menu); tray->show(); chartView->setRenderHint(QPainter::Antialiasing); setCentralWidget(chartView); initActionsConnections(); loaded="unloaded"; connect(m_serial, &QSerialPort::errorOccurred, this, &MainWindow::handleError); connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::on_actionGraph); connect(actTexte1, SIGNAL(triggered()), this, SLOT(changeTexte1())); QFile file("D:/Magasin_Temp_Hygro.txt"); if(!file.exists()) { file.setFileName("D:/Magasin_Temp_Hygro.txt"); file.open(QIODevice::ReadOnly | QIODevice::Text); file.close(); } alarmParameter(); } MainWindow::~MainWindow() { delete m_settings; delete m_ui; } void MainWindow::changeTexte1() { // chart->show(); // chartView->show(); showMaximized(); //loaded="unloaded"; } void MainWindow::openSerialPort() { const SettingsDialog::Settings p = m_settings->settings(); m_serial->setPortName(p.name); m_serial->setBaudRate(p.baudRate); m_serial->setDataBits(QSerialPort::Data8); m_serial->setParity(QSerialPort::NoParity); m_serial->setStopBits(QSerialPort::OneStop); m_serial->setFlowControl(QSerialPort::NoFlowControl); if (m_serial->open(QIODevice::ReadOnly)) { m_ui->actionConnect->setEnabled(false); m_ui->actionConfigure->setEnabled(true); showStatusMessage(tr("Connected to %1 : %2, %3") .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)); } else { QMessageBox::critical(this, tr("Error"), m_serial->errorString()); showStatusMessage(tr("Open error")); } } void MainWindow::closeSerialPort() { if (m_serial->isOpen()) m_serial->close(); m_ui->actionConfigure->setEnabled(true); m_ui->actionConnect->setEnabled(true); showStatusMessage(tr("Disconnected")); } void MainWindow::about() { QMessageBox::about(this, tr("About Simple Terminal"), tr("The <b>Simple Terminal</b> example demonstrates how to " "use the Qt Serial Port module in modern GUI applications " "using Qt, with a menu bar, toolbars, and a status bar.")); } void MainWindow::keyPressEvent (QKeyEvent *event) { if(event->key()==Qt::Key_Plus) {chartView->chart()->zoomIn();} if(event->key()==Qt::Key_Minus) {chartView->chart()->zoomOut();} if(event->key()==Qt::Key_4) {chartView->chart()->scroll(-10, 0);} if(event->key()==Qt::Key_6) {chartView->chart()->scroll(10, 0);} if(event->key()==Qt::Key_8) {chartView->chart()->scroll(0, 10);} if(event->key()==Qt::Key_2) {chartView->chart()->scroll(0, -10);} if(event->key()==Qt::Key_Escape) {quitMyApp();} } void MainWindow::readData(const QByteArray &data) { QFile fileIn("D:/Magasin_Temp_Hygro.txt"); fileIn.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream streamIn(&fileIn); QByteArray inputData = fileIn.readAll(); QByteArray dataOut; QString delimiter=QString(data.at(data.size()-1)); QDateTime dateTimeOld1; qDebug()<<"data "<<data; QString items=data; QStringList nbitems=items.replace(" ","").split(":"); if(data.size()>30 && nbitems.count()>0) { if(nbitems[1]=="Temperature"&&nbitems[3]=="Humidity") { QString list = streamIn.readLine(); QFile::copy("D:/Magasin_Temp_Hygro.txt","D:/Magasin_Temp_Hygro_temp.txt"); QFile fileOut("D:/Magasin_Temp_Hygro_temp.txt"); if(!fileOut.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug()<<"Error Magasin_Temp_Hygro_temp.txt not open"; } QTextStream streamOut(&fileOut); QStringList inputDataList=QString(inputData).split("\n"); QByteArray inputDataOneYear; for(int i=0;i<inputDataList.count();i++) { QString inputDataString=inputDataList[i]; QStringList lineList= inputDataString.split(":"); QString format="dd/MM/yyhh'h'mm"; QDateTime dateTime=QDateTime::fromString(lineList[0].replace(" ",""),format).addYears(100); QDateTime current=QDateTime::currentDateTime(); qint64 numberofDays= dateTime.daysTo(current); if(numberofDays<365)// && dateTime>=dateTimeOld2.addSecs(120)) { inputDataOneYear.append(inputDataString+"\n"); } dateTimeOld1=QDateTime::fromString(lineList[0],format).addYears(100); } streamOut <<inputDataOneYear; // QStringList result=streamOut2.split(";"); QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); QString format="dd/MM/yy hh'h'mm"; QDateTime dateTime=QDateTime::fromString(time,format).addYears(100); streamOut2=data; streamOut <<time+" "+streamOut2.split(";")[0]+"\n"; line=time+" "+streamOut2.split(";")[0]; qDebug()<<"line "<<line; fileOut.close(); fileIn.close(); QFile::remove("D:/Magasin_Temp_Hygro.txt"); QFile::rename("D:/Magasin_Temp_Hygro_temp.txt","D:/Magasin_Temp_Hygro.txt"); dateTimeOld2=QDateTime::fromString(time,format).addYears(100); date=line.split(":")[0]; QString format2="dd/MM/yyhh'h'mm"; QDateTime dateTime2=QDateTime::fromString(date.replace(" ",""),format2).addYears(100); bool ok = false; temp=line.split(":")[2].toDouble(&ok); hygro=line.split(":")[4].toDouble(&ok); temperature->append(dateTime2.toMSecsSinceEpoch(), temp); hygrometrie->append(dateTime2.toMSecsSinceEpoch(), hygro); axisX->setMax(dateTime2); line_count++; streamOut2=""; streamOut.flush(); } } tray->setToolTip(QString::number(temp)+"°C\n"+QString::number(hygro)+"%"); streamIn.flush(); } void MainWindow::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError) { QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString()); closeSerialPort(); } } void MainWindow::initActionsConnections() { SettingsDialog set; connect(m_ui->actionQuit, &QAction::triggered, this, &MainWindow::close); connect(m_ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort); connect(m_ui->actionConfigure, &QAction::triggered, this, &MainWindow::closeSerialPort); connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show); //connect(set.applyButton, SIGNAL(&QPushButton::clicked),this, SLOT(&MainWindow::openSerialPort)); connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::about); connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt); connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(quitMyApp())); } void MainWindow::showStatusMessage(const QString &message) { m_status->setText(message); } void MainWindow::on_actionGraph() { temperature->clear(); hygrometrie->clear(); QFile fileOut; fileOut.setFileName("D:/Temp_Hygro_temp.txt"); if(fileOut.exists()) { fileOut.remove(); } QFile::copy("D:/Magasin_Temp_Hygro.txt","D:/Temp_Hygro_temp.txt"); fileOut.open(QIODevice::ReadOnly | QIODevice::Text); QString format="dd/MM/yy hh'h'mm "; while( !fileOut.atEnd())//loop until end of QTextStrem { line=fileOut.readLine(); if(line.size()>=47) { date=line.split(":")[0]; QString format="dd/MM/yyhh'h'mm"; QDateTime dateTime=QDateTime::fromString(date.replace(" ",""),format).addYears(100); bool ok = false; temp=line.split(":")[2].toDouble(&ok); hygro=line.split(":")[4].toDouble(&ok); temperature->append(dateTime.toMSecsSinceEpoch(), temp); hygrometrie->append(dateTime.toMSecsSinceEpoch(), hygro); dateTimeOld2=dateTime; } line_count++; chartView->update(); } fileOut.close(); fileOut.remove(); if(loaded=="unloaded") { chart->legend()->hide(); chartView->setMouseTracking(true); chartView->setRubberBand(QChartView::RectangleRubberBand); axisX->setTickCount(10); axisX->setFormat("dd/MM/yy \n hh'h'mm"); axisX->setTitleText("Date"); chart->addAxis(axisX, Qt::AlignBottom); chart->addAxis(axisYtemp, Qt::AlignLeft); chart->addAxis(axisYhygro, Qt::AlignRight); chart->addSeries(temperature); chart->addSeries(hygrometrie); temperature->attachAxis(axisX); hygrometrie->attachAxis(axisX); temperature->attachAxis(axisYtemp); hygrometrie->attachAxis(axisYhygro); chartView->setChart(chart); loaded="loaded"; }else { if(temp>alarmtempMax.toFloat()) { showAlarm(); } line=""; } QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); QString format2="dd/MM/yy hh'h'mm"; QDateTime dateTime=QDateTime::fromString(time,format2).addYears(100); const QByteArray data= m_serial->readAll(); if(dateTime>=dateTimeOld2.addSecs(120)&& data.size()>30) { readData(data); } } void MainWindow::alarmParameter() { QFile fileIn("D:/Config.txt"); fileIn.open(QIODevice::ReadOnly | QIODevice::Text); inputData = fileIn.readAll(); if(inputData.count()>0) { alarmtempMin=QString(inputData).split(":")[0]; alarmtempMax=QString(inputData).split(":")[1]; alarmhygroMin=QString(inputData).split(":")[2]; alarmhygroMax=QString(inputData).split(":")[3]; } } void MainWindow::showAlarm() { //tray->showMessage("Temp",QString::number(temp),icon,5000); } void MainWindow::quitMyApp() { //delete chart; hide(); }