Navigating a QDateEdit

Interacting with a QDateEdit from a Squish script is a bit more involved than just clicking a button or writing some text into a line edit, especially if you want a nice scripting API for it as well. Below is a script function that you can cut-and-paste into your existing test case or shared script, and use without modifications. It requires you to have recorded one interaction with a QDateEdit though, to have the proper names in the object map.

Usage would be:

  1. setDate(":nameOfQDateEdit", "2011", "April", "1")

  1. def setDate(dateedit, year, month, day):
  2.     waitFor("object.exists(dateedit)", 20000)
  3.    
  4.     dateExpandX = findObject(dateedit).width - 10
  5.     sendEvent("QMouseEvent", waitForObject(dateedit), QEvent.MouseButtonPress, dateExpandX, 15, Qt.LeftButton, 0)
  6.     sendEvent("QMouseEvent", waitForObject(dateedit), QEvent.MouseButtonRelease, dateExpandX, 15, Qt.LeftButton, 0)
  7.    
  8.     yearToolButtonName = "{name='qt_calendar_yearbutton' type='QToolButton' visible='1' window=':qt_datetimedit_calendar_QCalendarPopup'}"
  9.     clickButton(waitForObject(yearToolButtonName))
  10.    
  11.     yearEditSpinBoxName = "{name='qt_calendar_yearedit' type='QSpinBox' visible='1' window=':qt_datetimedit_calendar_QCalendarPopup'}"
  12.     type(waitForObject(yearEditSpinBoxName), year)
  13.     type(waitForObject(yearEditSpinBoxName), "<Return>")
  14.  
  15.     monthToolButtonName = "{name='qt_calendar_monthbutton' type='QToolButton' visible='1' window=':qt_datetimedit_calendar_QCalendarPopup'}"
  16.     clickButton(waitForObject(monthToolButtonName))
  17.    
  18.     monthMenuName = "{type='QMenu' unnamed='1' visible='1' window=':qt_datetimedit_calendar_QCalendarPopup'}"
  19.     activateItem(waitForObjectItem(monthMenuName, month))
  20.  
  21.     calendarViewName = "{name='qt_calendar_calendarview' type='QCalendarView' visible='1' window=':qt_datetimedit_calendar_QCalendarPopup'}"
  22.     waitFor("object.exists(calendarViewName)", 20000)
  23.     model = findObject(calendarViewName).model()
  24.     seenOne = False
  25.     for row in range(1, model.rowCount() - 1): # First row contains day names, let's skip it
  26.         for col in range(model.columnCount()):
  27.             dayText = model.data(model.index(row, col)).toString()
  28.            
  29.             if dayText == "1":  # Some dates from last month may be seen, so make sure we've iterated
  30.                 seenOne = True  # past them before we actually click an item
  31.                
  32.             if seenOne and dayText == day:
  33.                 waitForObjectItem(calendarViewName, "%s/%s" % (row, col))
  34.                 clickItem(calendarViewName, "%s/%s" % (row, col), 14, 11, 0, Qt.LeftButton)
  35.                 break

Categories: