Results 1 to 8 of 8

Thread: How to use keyboard with QDateEdit and QTimeEdit?

  1. #1
    Join Date
    May 2019
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question How to use keyboard with QDateEdit and QTimeEdit?

    I have starting date and time and ending date and time. They work well with the mouse. When I go to use the keyboard, nothing happens.
    Qt Code:
    1. ui_duration_start_dateEdit->setDate(QDate::currentDate().addMonths(-1));
    2. ui_duration_start_timeEdit->setTime(0,0,0));
    3. ui_duration_end_dateEdit->setDate(QDate::currentDate());
    4. ui_duration_end_timeEdit->setTime(23,59,59));
    5. connect(ui_duration_start_dateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(SlotSetFromDateTime()));
    6. connect(ui_duration_start_timeEdit, SIGNAL(timeChanged(QTime)), this, SLOT(SlotSetFromDateTime()));
    7. connect(ui_duration_end_dateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(SlotSetToDateTime()));
    8. connect(ui_duration_end_timeEdit, SIGNAL(timeChanged(QTime)), this, SLOT(SlotSetToDateTime()));
    9. connect(ui_duration_start_dateEdit->calendarWidget(), SIGNAL(selectionChanged()), this, SLOT(SlotUpdateDates()));
    10. connect(ui_duration_end_dateEdit->calendarWidget(), SIGNAL(selectionChanged()), this, SLOT(SlotUpdateDates()));
    To copy to clipboard, switch view to plain text mode 
    Is there something I need to do to allow for keyboard input? Is there something that I did that prevents keyboard entry? I am looking forward to your help.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,067
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    You haven't really posted any code that illustrates whatever problem you are having. Showing a bunch of signal-slot connections doesn't tell us anything.

    It isn't clear why you are connecting to dateChanged() and timeChanged() signals with slots that ignore the new date or time sent as part of those signals. If you don't care what the user has chosen as the new date or time, why bother connecting to those signals at all?

    Posting a minimum example (a QDialog containing the two widgets, plus the code you have posted above and the code implementing the three slots) might help in diagnosing the problem.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2019
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    Thank you for responding.
    The ui_ prefixed variable are widgets made in Creator. The end of the name indicates what type of widget it is.
    I am trying to set the start date and time as well as the end date and time of a range. I then use that range to restrict the query to values in the range. These values are sent to a QTableView.
    There are checks on the dates to make sure that the start date and time is before the end date and time.
    I can change the QDateEdits and the QTimeEdits with the mouse. However, I am unable to change them with the keyboard.
    I believe this is the code you were asking for. Please let me know if you need more.
    Qt Code:
    1. void FindBagDuration::Init(void)
    2. {
    3. ...
    4.  
    5. // Start and end dates and times
    6. ui_duration_start_dateEdit->setDate(QDate::currentDate().addMonths(-1));
    7. ui_duration_start_timeEdit->setTime(QTime(0,0,0));
    8. ui_duration_end_dateEdit->setDate(QDate::currentDate());
    9. ui_duration_end_timeEdit->setTime(QTime(23,59,59));
    10. connect(ui_duration_start_dateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(SlotSetFromDateTime()));
    11. connect(ui_duration_start_timeEdit, SIGNAL(timeChanged(QTime)), this, SLOT(SlotSetFromDateTime()));
    12. connect(ui_duration_end_dateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(SlotSetToDateTime()));
    13. connect(ui_duration_end_timeEdit, SIGNAL(timeChanged(QTime)), this, SLOT(SlotSetToDateTime()));
    14. connect(ui_duration_start_dateEdit->calendarWidget(), SIGNAL(selectionChanged()), this, SLOT(SlotUpdateDates()));
    15. connect(ui_duration_end_dateEdit->calendarWidget(), SIGNAL(selectionChanged()), this, SLOT(SlotUpdateDates()));
    16. connect(this, SIGNAL(SignalSynchDateChanged(const QDate, bool)), this, SLOT(SlotSynchDateChanged(const QDate, bool)));
    17. connect(this, SIGNAL(SignalSynchTimeChanged(const QTime, bool)), this, SLOT(SlotSynchTimeChanged(const QTime, bool)));
    18.  
    19. ...
    20. }
    21.  
    22. void FindBagDuration::SlotSetFromDateTime(void)
    23. {
    24. QDate start_date = ui_duration_start_dateEdit->date();// Used to make comparisons easier to read.
    25. QTime start_time = ui_duration_start_timeEdit->time();
    26. QDate end_date = ui_duration_end_dateEdit->date();
    27. QTime end_time = ui_duration_end_timeEdit->time();
    28.  
    29. ui_duration_stackedWidget->setCurrentWidget(ui_duration_blank_page);
    30. // Clear \todo do we need to clear the widget first?
    31.  
    32. if(end_date < start_date)
    33. {
    34. ui_duration_end_dateEdit->setDate(start_date);
    35. emit SignalSynchDateChanged(start_date, false);
    36. }
    37.  
    38. emit SignalSynchDateChanged(start_date, true);
    39.  
    40. if(end_date == start_date)
    41. {
    42. if(end_time < start_time)
    43. {
    44. ui_duration_end_timeEdit->setTime(start_time);
    45. emit SignalSynchTimeChanged(start_time, false);
    46. }
    47. }
    48. m_duration_model->SlotSetFromDateTime(QDateTime(start_date, start_time));
    49. emit SignalSynchTimeChanged(start_time, true);
    50. emit SignalSynchDateChanged(start_date, true);
    51. }
    52.  
    53. void FindBagDuration::SlotSetToDateTime(void)
    54. {
    55. QDate start_date = ui_duration_start_dateEdit->date(); // Used to make comparisons easier to read.
    56. QTime start_time = ui_duration_start_timeEdit->time();
    57. QDate end_date = ui_duration_end_dateEdit->date();
    58. QTime end_time = ui_duration_end_timeEdit->time();
    59.  
    60. ui_duration_stackedWidget->setCurrentWidget(ui_duration_blank_page);
    61. // Clear \todo do we need to clear the widget?
    62.  
    63. // don't allow the start and end date to be the same
    64. if(start_date > end_date)
    65. {
    66. ui_duration_start_dateEdit->setDate(end_date);
    67. emit SignalSynchDateChanged(end_date, true);
    68. }
    69.  
    70. if(end_date == start_date)
    71. {
    72. // don't allow the start and end time to be the same
    73. if(start_time >= end_time)
    74. {
    75. if(start_time.hour() >= end_time.hour())
    76. {
    77. ui_duration_start_timeEdit->setTime(end_time.addSecs(-360));
    78. emit SignalSynchTimeChanged(end_time.addSecs(-360), true);
    79. }
    80. else if(start_time.minute() >= end_time.minute())
    81. {
    82. ui_duration_start_timeEdit->setTime(end_time.addSecs(-60));
    83. emit SignalSynchTimeChanged(end_time.addSecs(-60), true);
    84. }
    85. else
    86. {
    87. ui_duration_start_timeEdit->setTime(end_time.addSecs(-1));
    88. emit SignalSynchTimeChanged(end_time.addSecs(-1), true);
    89. }
    90. }
    91. }
    92. emit SignalSynchTimeChanged(end_time, false);
    93. emit SignalSynchDateChanged(end_date, false);
    94. m_duration_model->SlotSetToDateTime(QDateTime(end_date, end_time));
    95. }
    96.  
    97. void FindBagDuration::SlotSynchDateChanged(const QDate& date, bool start_date )
    98. {
    99. // Clear(); \todo ?
    100.  
    101. if(start_date == true)
    102. {
    103. if(ui_duration_end_dateEdit->date() < date)
    104. {
    105. ui_duration_end_dateEdit->setDate(date);
    106. }
    107.  
    108. ui_duration_start_dateEdit->setDate(date);
    109. }
    110. else
    111. {
    112. // don't allow the start and end date to be the same
    113. if(ui_duration_start_dateEdit->date() >= date)
    114. {
    115. ui_duration_start_dateEdit->setDate(date);
    116. }
    117.  
    118. ui_duration_end_dateEdit->setDate(date);
    119. }
    120.  
    121. if(ui_duration_start_dateEdit->date() == date)
    122. {
    123. if(ui_duration_start_timeEdit->time() > ui_duration_end_timeEdit->time())
    124. {
    125. ui_duration_start_timeEdit->setTime(QTime(0, 0, 0));
    126. }
    127. }
    128. }
    129.  
    130. void FindBagDuration::SlotSynchTimeChanged(const QTime& time, bool start_time )
    131. {
    132. // Clear(); \todo ?
    133.  
    134. if(start_time == true)
    135. {
    136. if(ui_duration_end_dateEdit->date() == ui_duration_start_dateEdit->date())
    137. {
    138. if(ui_duration_end_timeEdit->time() < time)
    139. {
    140. ui_duration_end_timeEdit->setTime(time);
    141. }
    142. }
    143.  
    144. ui_duration_start_timeEdit->setTime(time);
    145. }
    146. else
    147. {
    148. if(ui_duration_end_dateEdit->date() == ui_duration_start_dateEdit->date())
    149. {
    150. // don't allow the start and end time to be the same
    151. if(ui_duration_start_timeEdit->time() >= time)
    152. {
    153. ui_duration_start_timeEdit->setTime(QTime(0, 0, 0));
    154. }
    155. }
    156.  
    157. ui_duration_end_timeEdit->setTime(time);
    158. }
    159. }
    160.  
    161. void FindBagDuration::SlotUpdateDates(void)
    162. {
    163. if(ui_duration_start_dateEdit->date() == ui_duration_end_dateEdit->date())
    164. {
    165. // don't allow the start and end time to be the same
    166. if(ui_duration_start_timeEdit->time() >= ui_duration_end_timeEdit->time())
    167. {
    168. ui_duration_start_timeEdit->setTime(QTime(0, 0, 0));
    169. }
    170. }
    171. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Debra; 3rd July 2019 at 19:27.

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

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    I think you have to check whether the start / end date / time is valid in both slots and do something further only when all four elements are valid.

  5. The following user says thank you to Lesiok for this useful post:

    Debra (3rd July 2019)

  6. #5
    Join Date
    May 2019
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    Validating the dateEdits and timeEdits is a good recommendation. I'll try that. I don't think that fixes the problem though, since the problem occurs with the initialized values also. It is very frustrating that it will work with the mouse, but not the keyboard. Thank you for your recommendation.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,067
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    Possibly your problem is due to the way you have implemented your slots to handle date / time changes. From my reading of the QDateTime docs, a call to setTime() or setDate() on one of these widgets will result in a dateChanged() / timeChanged() signal, so you have possibly set up some recursion by calling these from within your slots.

    Also, when you emit a signal from within a slot, any slots connected to that signal will get executed immediately, with control returning to the original slot only after all of the connected slots have finished - thus more chance of unintended recursion.

    In addition, it is possible that every keystroke will result in one of the ...Changed() signals being emitted, and if the partially-edited value is incorrect, your slot might simply reset it back to the original making it appear as though keyboard editing isn't doing anything. The definitive signal to watch for keystroke editing is QAbstractSpinBox::editingFinished(), which is emitted only after a return keypress or focus has left the widget.

    You can verify this with the debugger, setting a breakpoint in one of your slots and checking the call stack to see where it is being invoked. You could also sprinkle some qDebug() calls around to help better track how and when your signals / slot connections are being executed.

    Monitoring interactions between two or more widgets is tricky, especially when a change in one widget's value can affect another widget's value - you may inadvertently set up a ping-pong effect. Sometimes the only way around this is to disable signals on the linked widget, set the new value, then re-enable signals, thereby preventing the change in value from triggering the associated signal.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    Debra (3rd July 2019)

  9. #7
    Join Date
    May 2019
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    Tried a lot of things, but the problem wasn't in the code. Thank you for your recommendations; they are good ideas regardless of my problem.
    The problem was a result of a Linux environment variable QT_XKB_CONFIG_ROOT not being set.
    Thank you both, Lesiok and d_stranz!

  10. The following user says thank you to Debra for this useful post:

    d_stranz (4th July 2019)

  11. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,067
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to use keyboard with QDateEdit and QTimeEdit?

    Hah, so much for my big ideas
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 24th January 2017, 09:13
  2. QDateTimeEdit (QTimeEdit, QDateEdit) invalid input
    By Kumosan in forum Qt Programming
    Replies: 10
    Last Post: 15th June 2012, 12:21
  3. QTimeEdit Problem
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2010, 17:46
  4. QTimeEdit / QDateEdit doubleclick behavior
    By osiris81 in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 17:28
  5. QtimeEdit
    By mickey in forum Qt Programming
    Replies: 4
    Last Post: 11th July 2006, 18:04

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.