PDA

View Full Version : Help with push buttons and date/time edit



lupo5
9th May 2014, 21:23
Hi everyone,

I am completely new to this forum and QT Creator, and it's great to see there's a place for new people like me :)
Please feel free to correct me if I use terms incorrectly!!

Im creating an application where I want to edit the date and time (date/time edit widget) and "set" or "cancel" that date and time using two push buttons. In the UI window I've selected the "clicked()" signal for the two buttons and on the date/time edit widget I selected the "editingFinished()" slot.

Question 1)
Will the dateTime property always be whatever the user has entered for a date and time? If so, is there a way to override that update so that it only changes when the user selects the "Set Alarm" button?

Question 2)
Is there a way to create a new instance (I think thats the right term) of dateTime?
For instance, I have a label that displays the current time on the application using the code: QTime time = QTime::currentTime();
Is there a way to do this for dateTime?

Thank you!!!

sedi
9th May 2014, 22:16
Hi lupo5,

I am not sure what you actually want to do. Do you want to set the computer's system time and date? That would be not trivial at all, according to a quick search (see here for Linux (http://www.qtcentre.org/threads/44384-How-to-change-Linux-system-clock-by-Qt) and here for Windows (http://stackoverflow.com/questions/3742880/change-system-date-on-windows-using-qt)).

The dateTime property of a QDateTimeEdit reflects the date and time that is valid for *this* widget. It can hold (almost) every possible date.

If you want to stop the Widget from being editable, try setEnabled(false) and only allow editing by the Slot that's connected with the button's signal.

You can have as many QDateTime objects as you want:


QDateTime yesterdayMorning;
QDateTime now;
QDateTime whatever;


As for the label: look at QDateTime::currentDateTime().toString([refer to the docs for what goes into here])

Does this answer any of your questions? If not, please clarify :-)

Sebastian

lupo5
9th May 2014, 23:13
Thank you Sebastian!

Ive actually switched to using separate QDate and QTime edit fields now.

As I have it, the user launches the app and the QDate and QTime edit fields will show their respective current date and time until the user changes them. These fields do not update like the current time clock I have running in a label.

I want the edited date and time to only be updated/reflected in the rest of the program when the user presses the "Set Alarm" button, NOT when they edit the fields.

Do I put setEnabled(false) in the "editingFinished(etc...) function of the date/ time edit fields?

Maybe it would be more helpful if I post my code:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void setDate(const QDate &date);
void setTime(const QTime &time);

private slots:
void showTime();
void setAlarm();
void cancelAlarm();

void on_setAlarm_clicked();

void on_cancelAlarm_clicked();


// void on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime);

// void on_dateTimeEdit_editingFinished(const QDateTime &dateTime);

private:
Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H


and




#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDateTime>
#include <QDateTimeEdit>
#include <QTimeEdit>
#include <QDateEdit>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

QDateTime *dateTime = new QDateTime;
ui->setupUi(this);
showTime();
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start();
ui->timeEdit->setTime(QTime::currentTime());
ui->dateEdit->setDate(QDate::currentDate());

}

void MainWindow::showTime()
{
// QDate date =
QTime time = QTime::currentTime();
QString time_text = time.toString("hh : mm");
ui->Digital_Clock->setText(time_text);
}

void MainWindow::on_setAlarm_clicked()
{
setTime(time);
setDate(QDate &date);
}

void MainWindow::on_cancelAlarm_clicked()
{
cancelAlarm();
}

void MainWindow::setDate(const QDate &date)
{

}

void MainWindow::setTime(const QTime &time)
{

}

void MainWindow::setAlarm()
{


}
void MainWindow::cancelAlarm()
{

}


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

//void MainWindow::on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
//{
//bool flag = 1;

//}

//void MainWindow::on_dateTimeEdit_editingFinished(const QDateTime &dateTime)
//{
// QDateTime dateTimeFromText(dateTime);
//}

Lesiok
10th May 2014, 08:29
Just a little note. Using separately QTime::currentTime() and QDate::currentDate() around midnight you get an error when one method will start before midnight and the second after midnight. Code should looks like :
QDateTime now = QDateTime::currentDatTime();
ui->timeEdit->setTime(now.Time());
ui->dateEdit->setDate(now.Date());

anda_skoa
10th May 2014, 11:03
I want the edited date and time to only be updated/reflected in the rest of the program when the user presses the "Set Alarm" button, NOT when they edit the fields.

Then just ignore the date/time widgets' change signals and read their values in the slot connected to the button.

Cheers,
_