QChartView scroll or zoom work on chart but axis dosen't change?



  • hello, my code make zoom or scroll fine of my chart but axisY or axisX doesn't change..

    How can I solve that?

    thank you very much

    my Mainwindow.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
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        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=0;
    tempmax=0;
    hygromin=0;
    hygromax=0;
    
    temperature->attachAxis(axisX);
    temperature->attachAxis(axisYtemp);
    hygrometrie->attachAxis(axisX);
    hygrometrie->attachAxis(axisYhygro);
        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);
        resize(820, 600);
        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()
    {
        showMaximized();
        loaded="unloaded";
        chart->show();
        chartView->show();
    
    }
    
    
    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::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->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show);
        connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::about);
        connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt);
    
    }
    void MainWindow::keyPressEvent (QKeyEvent *event)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(keyEvent);
    
        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::on_actionGraph()
    {
    
        if(loaded=="unloaded")
        {
            chart=nullptr;
            temperature->clear();
            hygrometrie->clear();
            chart= new QChart();
            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";
                    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);
                    if(temp<tempmin)
                    {
                        tempmin=temp+5;
                    }
                    if(hygro<hygromin)
                    {
                        hygromin=hygro;
                    }
                    if(temp>tempmax)
                    {
                        tempmax=temp+5;
                    }
                    if(hygro>hygromax)
                    {
                         hygromax=hygro+5;
                    }
                    qDebug()<<tempmin<<" "<<tempmax<<" "<<hygromin<<" "<<hygromax;
                   // axisYtemp->setRange(tempmin,tempmax);
                   // axisYhygro->setRange(hygromin,hygromax);
                }
                line_count++;
    
            }
            chart->addSeries(temperature);
            chart->addSeries(hygrometrie);
            chart->addAxis(axisX, Qt::AlignBottom);
            chart->addAxis(axisYtemp, Qt::AlignLeft);
            chart->addAxis(axisYhygro, Qt::AlignRight);
            axisX->setMax(dateTime);
            chart->legend()->hide();
            axisX->setTickCount(10);
            axisX->setFormat("dd/MM/yy \n hh'h'mm");
            axisX->setTitleText("Date");
            chartView->setChart(chart);
            fileOut.close();
            fileOut.remove();
            loaded="loaded";
    
        }
        else{
    
            if(temp>alarmtempMax.toFloat())
            {
                showAlarm();
            }
    
            line="";
        }
    
        const QByteArray data= m_serial->readAll();
        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()
    {
        chart->close();
        chartView->close();
        hide();
    
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.