How to set reminder in qt



  • hello everyone i want to make reminder in qt...so first of all i set current time on a label and now i want to set alarm after 2 hours and when 2 hours will be complete there will be a message box open and that will remind us that your time have been completed. please give me some suggesion i know what will be logic use here but dont know how to implement in qt.



  • this is my code what i used but i am getting error error: no match for 'operator-' in 'settime - CurrentTime'
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QTimer>
    #include<QTime>
    #include<qDebug>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);;
    timer->setInterval(60);
    connect(timer, SIGNAL(timeout()),this,SLOT(update_time()));
    timer->start();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::update_time()
    {
    ui->label_time->setText(QTime::currentTime().toString());

    QString CurrentTime = QTime::currentTime().toString();
    
    if ( ui->lineEdit->text() != "")
    {
        QString settime = ui->lineEdit->text();
        qDebug()<<settime;
    
        QString Diff = settime - CurrentTime;
    
        if (Diff == "00:00:00")
        {
            QMessageBox msgBox;
            msgBox.setText("Your time has been finished.");
        }
    }
    

    }
    @
    error: no match for 'operator-' in 'settime - CurrentTime'


  • Moderators

    You are trying to substract string... this will not work! You need to use QTime/ QDateTime classes here.



  • for current time i can use QTime currtime = QTime::currentTime() but what i should use for the time when i will set alarm ...let i want to set alarm after 2 hours then where i will store these time...please give me some solution ...thanks in advance.


  • Moderators

    @
    QTime currtime = QTime::currentTime();
    QTime alarmTime = currtime.addSecs(2 * 3600);
    @

    I would personally prefer to use QDateTime, which has addHours() method. Plus, you can actually set the timer to 2 hours and skip the whole QTime hassle.


Log in to reply
 

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