Results 1 to 5 of 5

Thread: Help with push buttons and date/time edit

  1. #1
    Join Date
    May 2014
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Help with push buttons and date/time edit

    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!!!
    Last edited by lupo5; 9th May 2014 at 21:52.

  2. #2
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Help with push buttons and date/time edit

    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 and here for Windows).

    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:
    Qt Code:
    1. QDateTime yesterdayMorning;
    2. QDateTime whatever;
    To copy to clipboard, switch view to plain text mode 

    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

  3. #3
    Join Date
    May 2014
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with push buttons and date/time edit

    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. public slots:
    19. void setDate(const QDate &date);
    20. void setTime(const QTime &time);
    21.  
    22. private slots:
    23. void showTime();
    24. void setAlarm();
    25. void cancelAlarm();
    26.  
    27. void on_setAlarm_clicked();
    28.  
    29. void on_cancelAlarm_clicked();
    30.  
    31.  
    32. // void on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime);
    33.  
    34. // void on_dateTimeEdit_editingFinished(const QDateTime &dateTime);
    35.  
    36. private:
    37. Ui::MainWindow *ui;
    38.  
    39. };
    40.  
    41. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QTimer>
    4. #include <QDateTime>
    5. #include <QDateTimeEdit>
    6. #include <QTimeEdit>
    7. #include <QDateEdit>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13.  
    14. QDateTime *dateTime = new QDateTime;
    15. ui->setupUi(this);
    16. showTime();
    17. QTimer * timer = new QTimer(this);
    18. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    19. timer->start();
    20. ui->timeEdit->setTime(QTime::currentTime());
    21. ui->dateEdit->setDate(QDate::currentDate());
    22.  
    23. }
    24.  
    25. void MainWindow::showTime()
    26. {
    27. // QDate date =
    28. QTime time = QTime::currentTime();
    29. QString time_text = time.toString("hh : mm");
    30. ui->Digital_Clock->setText(time_text);
    31. }
    32.  
    33. void MainWindow::on_setAlarm_clicked()
    34. {
    35. setTime(time);
    36. setDate(QDate &date);
    37. }
    38.  
    39. void MainWindow::on_cancelAlarm_clicked()
    40. {
    41. cancelAlarm();
    42. }
    43.  
    44. void MainWindow::setDate(const QDate &date)
    45. {
    46.  
    47. }
    48.  
    49. void MainWindow::setTime(const QTime &time)
    50. {
    51.  
    52. }
    53.  
    54. void MainWindow::setAlarm()
    55. {
    56.  
    57.  
    58. }
    59. void MainWindow::cancelAlarm()
    60. {
    61.  
    62. }
    63.  
    64.  
    65. MainWindow::~MainWindow()
    66. {
    67. delete ui;
    68. }
    69.  
    70. //void MainWindow::on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
    71. //{
    72. //bool flag = 1;
    73.  
    74. //}
    75.  
    76. //void MainWindow::on_dateTimeEdit_editingFinished(const QDateTime &dateTime)
    77. //{
    78. // QDateTime dateTimeFromText(dateTime);
    79. //}
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with push buttons and date/time edit

    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 :
    Qt Code:
    1. QDateTime now = QDateTime::currentDatTime();
    2. ui->timeEdit->setTime(now.Time());
    3. ui->dateEdit->setDate(now.Date());
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with push buttons and date/time edit

    Quote Originally Posted by lupo5 View Post
    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,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    lupo5 (11th May 2014)

Similar Threads

  1. Replies: 1
    Last Post: 17th April 2014, 06:09
  2. Replies: 0
    Last Post: 5th January 2013, 13:29
  3. Line edit and push button
    By dela in forum Newbie
    Replies: 1
    Last Post: 10th December 2008, 16:10
  4. Replies: 1
    Last Post: 19th May 2007, 05:19
  5. How to default Date-Edit Widget to system date
    By JohnToddSr in forum Qt Tools
    Replies: 4
    Last Post: 17th January 2007, 19:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.