Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTimeEdit to Double



  • Hello guys, this is my first post in this forum, so sorry if i did something wrong
    bascly i trying to learn QCustomPlot and i want to make a xAxis in time format (0 to 20 hh:mm:ss) receive a value from QTimeEdit, but QCustomPlot use double and QTimeEdit use QTime.
    i can't figure out how to convert QTime to double.

    i dont know how this gona help, but here is the code

    thanks in advance

    Header

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        void addpoint(double x, double y);
        void clearData();
        void plot();
    
    private slots:
        //void on_timeEdit_2_userTimeChanged(const QTime &time);
    
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
        void on_bx_x_userTimeChanged(const QTime &time);
    
    private:
        Ui::MainWindow *ui;
    
        QVector<double> qv_x, qv_y;
    };
    #endif // MAINWINDOW_H
    
    

    CPP

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    //std::string bxval;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->plot->addGraph();
        ui->plot->graph(0)->setScatterStyle((QCPScatterStyle::ssCircle));
        ui->plot->graph(0)->setLineStyle(QCPGraph::lsLine);
        ui->plot->graph()->setPen(QPen(Qt::blue));
        ui->plot->graph()->setBrush(QBrush(QColor(0, 0, 255, 20)));
        QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
        ui->plot->xAxis->setTicker(timeTicker);
        ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::addpoint(double x, double y)
    {
        qv_x.append(x);
        qv_y.append(y);
    }
    
    void MainWindow::clearData()
    {
        qv_x.clear();
        qv_y.clear();
    }
    
    void MainWindow::plot()
    {
        ui->plot->graph(0)->setData(qv_x, qv_y);
        ui->plot->replot();
        ui->plot->update();
    }
    
    
    //void MainWindow::on_timeEdit_2_userTimeChanged(const QTime &time)
    //{
    
    //}
    
    void MainWindow::on_pushButton_clicked()
    {
    //    addpoint(bxval,ui->bx_y->value());
        addpoint(ui->bx_y->value(),ui->bx_y->value());
        plot();
    }
    
    void MainWindow::on_pushButton_2_clicked()
    {
        clearData();
        plot();
    }
    
    //void MainWindow::on_bx_x_userTimeChanged(const QTime &time)
    //{
    //    bxval = std::to_string(time);
    //}
    
    
    

    edit: "typo" QTimeEdit to double


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @EUduardo said in QTimeEdit to Double:

    i can't figure out how to convert QTime to QTimeEdit.

    Do you mean set a QTime value on QTimeEdit ?



  • @SGaist
    I think he means to convert QTime to double as x/y value.



  • @EUduardo QTime class provides function to get the "delta" time between the two "Qtimes".
    You can convert the time to elapsed count, having origin Time as reference time.
    eg:
    QTime timeOrigin(5,30,0);
    QTime timeXVal1(5,45,0);
    int elapsedSec =timeOrigin.secsTo(timeXVal1);
    this elapsedSec value can be used for plot the value in X-Axis.



  • @nagesh said in QTimeEdit to Double:

    @EUduardo QTime class provides function to get the "delta" time between the two "Qtimes".
    You can convert the time to elapsed count, having origin Time as reference time.
    eg:
    QTime timeOrigin(5,30,0);
    QTime timeXVal1(5,45,0);
    int elapsedSec =timeOrigin.secsTo(timeXVal1);
    this elapsedSec value can be used for plot the value in X-Axis.

    it worked, thanks, this is what i did:

    const QTime timeOrigin(0,0,0);
    int xval;
    
    void MainWindow::on_QTimeEdit_userTimeChanged(const QTime &time)
    {
        xval = timeOrigin.secsTo(time);
    }
    
    void MainWindow::on_confirmBTN_clicked()
    {
        addpoint(xval,ui->bx_y->value());
        plot();
    }
    

Log in to reply