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. QTimeEdit to Double
Qt 6.11 is out! See what's new in the release blog

QTimeEdit to Double

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 2.0k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    EUduardo
    wrote on last edited by EUduardo
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

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

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

          @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.

          E 1 Reply Last reply
          3
          • nageshN nagesh

            @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.

            E Offline
            E Offline
            EUduardo
            wrote on last edited by
            #5

            @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();
            }
            
            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