def _getCalendar(self, display_format, value = None): ''' Get a combobox filled with the given values :param values: The values as key = value, value = description or text :type values: Dict :returns: A combobox :rtype: QWidget ''' widget = QWidget() calendar = QDateTimeEdit() calendar.setCalendarPopup(True) calendar.setDisplayFormat(display_format) if value is not None: calendar.setDate(QDate.fromString(value, display_format)) else: calendar.setDate(QDate.currentDate()) layout = QHBoxLayout(widget) layout.addWidget(calendar, 1); layout.setAlignment(Qt.AlignCenter); layout.setContentsMargins(5,0,5,0); widget.setLayout(layout); return widget
def createEditor(self, parent, option, index): """QWidget * QStyledItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index )""" editor = QDateTimeEdit(parent) editor.setCalendarPopup(True) editor.setDisplayFormat(self.edit_pattern) editor.dateTimeChanged.connect(self._commitAndClose) return editor
def _getCalendar(self, display_format, value=None): ''' Get a combobox filled with the given values :param values: The values as key = value, value = description or text :type values: Dict :returns: A combobox :rtype: QWidget ''' widget = QWidget() calendar = QDateTimeEdit() calendar.setCalendarPopup(True) calendar.setDisplayFormat(display_format) if value is not None: calendar.setDate(QDate.fromString(value, display_format)) else: calendar.setDate(QDate.currentDate()) layout = QHBoxLayout(widget) layout.addWidget(calendar, 1) layout.setAlignment(Qt.AlignCenter) layout.setContentsMargins(5, 0, 5, 0) widget.setLayout(layout) return widget
class DateRequest(Request): def __init__(self, parent): Request.__init__(self, parent) self.setOperators() self.setDateTime() def setOperators(self): self.operatorCombo = OperatorCombo(self) self.hlayout.addWidget(self.operatorCombo) self.connect(self.operatorCombo, SIGNAL("currentIndexChanged ( int )"), self.updateQuery) self.connect(self.operatorCombo, SIGNAL("currentIndexChanged ( const QString &)"), self.updateQuery) def updateQuery(self, data): self.emit(SIGNAL("queryUpdated")) def setDateTime(self): self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.hlayout.addWidget(self.datetime, 50) self.connect(self.datetime, SIGNAL("dateChanged ( const QDate &)"), self.updateQuery) self.connect(self.datetime, SIGNAL("dateTimeChanged ( const QDateTime &)"), self.updateQuery) self.connect(self.datetime, SIGNAL("timeChanged ( const QTime &)"), self.updateQuery) def request(self): res = "(time " + str(self.operatorCombo.currentText()) date_time = self.datetime.dateTime() res += str(date_time.toString("yyyy-MM-ddThh:mm:ss")) + ")" return res
class DateDialog(QDialog): def __init__(self, parent=None): super(DateDialog, self).__init__(parent) layout = QVBoxLayout(self) # nice widget for editing the date self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.datetime.setDateTime(QDateTime.currentDateTime()) layout.addWidget(self.datetime) # OK and Cancel buttons self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) layout.addWidget(self.buttons) self.buttons.accepted.connect(self.accept) self.buttons.rejected.connect(self.reject) # get current date and time from the dialog def dateTime(self): return self.datetime.dateTime() # static method to create the dialog and return (date, time, accepted) @staticmethod def getDateTime(parent=None): dialog = DateDialog(parent) result = dialog.exec_() date = dialog.dateTime() return (date.date(), date.time(), result == QDialog.Accepted)
def createEditor(self,parent,option,index): """create widgets to edit values""" if index.isValid(): model = index.model() column = index.column() if (column == model.position_for_header('date')) or \ (column == model.position_for_header('expected_date')) : data = model.data(index,Qt.DisplayRole) if data.toString() == "": data = datetime.today() editor = QDateTimeEdit(data) editor.setCalendarPopup(True) self.updateEditorGeometry(editor, option,index ) # by default it's a bit too small editor.setFixedWidth(option.rect.width() + 50) # FIXME: resulting position is still wrong return editor return QStyledItemDelegate.createEditor(self,parent,option,index)
def createEditor(self, parent, styleOption, index): if index.column() == 1: editor = QDateTimeEdit(parent) editor.setDisplayFormat(self.parent().currentDateFormat) editor.setCalendarPopup(True) return editor editor = QLineEdit(parent) # create a completer with the strings in the column as model allStrings = [] for i in range(1, index.model().rowCount()): strItem = index.model().data(index.sibling(i, index.column()), Qt.EditRole) if strItem not in allStrings: allStrings.append(strItem) aS = [str(x.toString()) for x in allStrings] autoComplete = QCompleter(QStringList(",".join(aS))) editor.setCompleter(autoComplete) editor.editingFinished.connect(self.commitAndCloseEditor) return editor
def _create_widget(cls, c, parent, host=None): dtt = QDateTimeEdit(parent) dtt.setObjectName(u'{0}_{1}'.format(cls._TYPE_PREFIX, c.name)) dtt.setCalendarPopup(True) # Set ranges if c.min_use_current_datetime: dtt.setMinimumDateTime(datetime.today()) else: dtt.setMinimumDateTime(c.minimum) if c.max_use_current_datetime: dtt.setMaximumDateTime(datetime.today()) else: dtt.setMaximumDateTime(c.maximum) # Set maximum datetime as current datetime dtt.setDateTime(dtt.maximumDateTime()) return dtt
class DateRequest(Request): def __init__(self, parent): Request.__init__(self, parent) self.setOperators() self.setDateTime() def setOperators(self): self.operatorCombo = OperatorCombo(self) self.hlayout.addWidget(self.operatorCombo) def setDateTime(self): self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.hlayout.addWidget(self.datetime, 50) def request(self): res = "(time " + str(self.operatorCombo.currentText()) date_time = self.datetime.dateTime() res += str(date_time.toString("yyyy-MM-ddThh:mm:ss")) + ")" return res
class DateRequest(Request): def __init__(self, parent): Request.__init__(self, parent) self.setOperators() self.setDateTime() def setOperators(self): self.operatorCombo = OperatorCombo(self) self.hlayout.addWidget(self.operatorCombo) def setDateTime(self): self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.hlayout.addWidget(self.datetime, 50) def request(self): res = "(time " + str(self.operatorCombo.currentText()) date_time = self.datetime.dateTime() res += str(date_time.toString("yyyy-MM-ddThh:mm:ss")) + ")" return res
def createEditor(self, parent, option, index): editor = QDateTimeEdit(parent) editor.setCalendarPopup(True) return editor
class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.agreement1ID = 'AG01' self.agreement2ID = 'AG02' self.dbNameInit = 'test1' self.calculate1 = '+' self.c1 = '1' self.c3 = '1' self.threads = [] self.onInit() def onInit(self): self.resize(450, 300) self.center() self.setMenu() self.setWindowTitle(u'MainWindow') self.setWindowIcon(QIcon('learning.ico')) self.agreement1 = QLabel('Contract1', self) self.agreement11 = QLabel(' Contract1 *', self) self.agreement2 = QLabel('Contract2', self) self.agreement22 = QLabel('Contract2 *', self) self.calculate = QLabel('Formula', self) self.startTime = QLabel('Start Time', self) self.endTime = QLabel('End Time', self) self.agreement1Edit = QLineEdit() self.agreement2Edit = QLineEdit() self.calculate1Edit = QLineEdit() self.calculate2Combo = QComboBox() self.calculate2Combo.addItem('+') self.calculate2Combo.addItem('-') self.calculate2Combo.addItem('*') self.calculate2Combo.addItem('/') self.calculate2Combo.activated[str].connect(self.onActivated) self.calculate2Edit = QLineEdit() self.calculate3Edit = QLineEdit() self.startTimeEdit = QDateTimeEdit( datetime.strptime('20150101 00:00:00', '%Y%m%d %H:%M:%S'), self) self.startTimeEdit.setDisplayFormat('yyyy-MM-dd') self.startTimeEdit.setCalendarPopup(True) self.endTimeEdit = QDateTimeEdit(QDateTime.currentDateTime(), self) self.endTimeEdit.setDisplayFormat('yyyy-MM-dd') self.endTimeEdit.setCalendarPopup(True) self.stateEdit = QTextEdit() self.run = QPushButton('Run', self) self.run.clicked.connect(self.runMain) #self.setAgreementBtn = QPushButton('Setting', self) #self.setAgreementBtn.clicked.connect(self.setAgreement) grid = QGridLayout() #grid.setSpacing(10) grid.addWidget(self.agreement1, 1, 0) grid.addWidget(self.agreement1Edit, 1, 1) grid.addWidget(self.agreement2, 2, 0) grid.addWidget(self.agreement2Edit, 2, 1) #grid.addWidget(self.setAgreementBtn, 2, 2) grid.addWidget(self.startTime, 3, 0) grid.addWidget(self.startTimeEdit, 3, 1) grid.addWidget(self.endTime, 4, 0) grid.addWidget(self.endTimeEdit, 4, 1) grid.addWidget(self.calculate, 5, 0) grid.addWidget(self.agreement11, 5, 1) grid.addWidget(self.calculate1Edit, 5, 2) grid.addWidget(self.calculate2Combo, 5, 3) grid.addWidget(self.agreement22, 5, 4) grid.addWidget(self.calculate3Edit, 5, 5) grid.addWidget(self.stateEdit, 6, 1, 1, 5) grid.addWidget(self.run, 7, 1) gridWidget = QWidget() gridWidget.setLayout(grid) self.agreement11.move(200, 100) self.setCentralWidget(gridWidget) self.show() def onActivated(self, text): self.calculate1 = text def setMenu(self): exitAction = QAction(u'Quit', self) exitAction.triggered.connect(qApp.quit) menubar = self.menuBar() fileMenu = menubar.addMenu(u'Menu') fileMenu.addAction(exitAction) def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def done(self, ls): self.stateEdit.append(u'Maximum:%f' % ls[1]) self.stateEdit.append(u'Minimum:%f' % ls[2]) self.stateEdit.append(u'Average:%f' % ls[3]) show(ls[0]) self.run.setEnabled(True) self.stateEdit.append('End time: %s' % time.ctime()) def runMain(self): self.run.setEnabled(False) agreementt1 = self.agreement1Edit.text() agreementt2 = self.agreement2Edit.text() if agreementt1 == '': self.stateEdit.append('Settings of contract1 have error!') elif agreementt2 == '': self.stateEdit.append('Settings of contract2 have error!') else: self.agreement1ID = agreementt1 self.agreement2ID = agreementt2 startTime = self.startTimeEdit.dateTime() endTime = self.endTimeEdit.dateTime() self.c1 = self.calculate1Edit.text() self.c3 = self.calculate3Edit.text() self.stateEdit.append('Formula: ' + self.c1 + '*' + self.agreement1ID + self.calculate1 + self.c3 + '*' + self.agreement2ID + ' have set') self.stateEdit.append('Start time: %s' % time.ctime()) self.workThread = WorkThread(self.agreement1ID, self.agreement2ID, startTime, endTime, self.c1, self.c3, self.calculate1) self.workThread.finishSignal.connect(self.done) self.workThread.start()
class TplRow(QWidget): def __init__(self, parent = None, _id = 0): super(TplRow, self).__init__(parent) self.id = _id self.setLayout(QHBoxLayout()) self.idLabel = QLabel(self) self.beginEdit = QDateTimeEdit(self) self.beginEdit.setCalendarPopup(True) self.endEdit = QDateTimeEdit(self) self.endEdit.setCalendarPopup(True) self.timeDiff = ClickLabel(self) self.descriptionEdit = QLineEdit(self) self.noteEdit = QLineEdit(self) self.delButton = QPushButton(self) self.delButton.setText('X') self.layout().addWidget(self.idLabel) self.layout().addWidget(self.beginEdit) self.layout().addWidget(self.endEdit) self.layout().addWidget(self.timeDiff) self.layout().addWidget(self.descriptionEdit) self.layout().addWidget(self.noteEdit) self.layout().addWidget(self.delButton) self.layout().setContentsMargins(2,2,2,2) self.connect(self.descriptionEdit, SIGNAL('editingFinished ()'), self.notify) self.connect(self.noteEdit, SIGNAL('editingFinished ()'), self.notify) self.connect(self.beginEdit, SIGNAL('editingFinished ()'), self.notify) self.connect(self.endEdit, SIGNAL('editingFinished ()'), self.notify) self.connect(self.delButton, SIGNAL('clicked()'), self.delete) self.connect(self.timeDiff, SIGNAL('clicked()'), self.onTimeDiff) def set(self, tpl): self.idLabel.setText(str(tpl[0])) self.beginEdit.setDateTime(QDateTime.fromTime_t(tpl[1])) self.endEdit.setDateTime(QDateTime.fromTime_t(tpl[2])) self.timeDiff.setText( self.mkDiff( tpl[1], tpl[2] ) ) self.descriptionEdit.setText(str(tpl[3])) self.noteEdit.setText(str(tpl[4])) def get(self): tpl = [] tpl.append(int(self.idLabel.text())) tpl.append(self.beginEdit.dateTime().toTime_t()) tpl.append(self.endEdit.dateTime().toTime_t()) tpl.append(self.descriptionEdit.text()) tpl.append(self.noteEdit.text()) return tpl def clear(self): self.beginEdit.clear() self.endEdit.clear() self.timeDiff.clear() self.descriptionEdit.clear() self.noteEdit.clear() self.idLabel.clear() def mkDiff(self, begin, end): return '%4d' % ceil( float( end - begin ) / 60 ) @pyqtSlot() def onTimeDiff(self): self.parent().parent().parent().statusBar.showMessage( '%s copied to clipboard.' % self.timeDiff.text() ) self.parent().clipboard.setText( str(self.timeDiff.text()).strip() ) @pyqtSlot() def delete(self): if self.idLabel.text(): if QMessageBox.question(self, 'delete ?', 'really delete id %s ?' % self.idLabel.text(), QMessageBox.Yes|QMessageBox.No, QMessageBox.No) == QMessageBox.Yes: self.emit(SIGNAL('del(int)'), self.id) @pyqtSlot() def notify(self): if self.idLabel.text(): self.timeDiff.setText( self.mkDiff( self.beginEdit.dateTime().toTime_t(), self.endEdit.dateTime().toTime_t() ) ) self.emit(SIGNAL('valueChanged(int)'), self.id)
def createWidget(self, parent): wid = QDateTimeEdit(parent) wid.setCalendarPopup(True) wid.setFrame(False) return wid
class QtDatetimeSelector(QtBoundedDatetime, ProxyDatetimeSelector): """ A Qt implementation of an Enaml ProxyDatetimeSelector. """ #: A reference to the widget created by the proxy. widget = Typed(QDateTimeEdit) #-------------------------------------------------------------------------- # Initialization API #-------------------------------------------------------------------------- def create_widget(self): """ Create the QDateTimeEdit widget. """ self.widget = QDateTimeEdit(self.parent_widget()) def init_widget(self): """ Initialize the widget. """ super(QtDatetimeSelector, self).init_widget() d = self.declaration self.set_datetime_format(d.datetime_format) self.set_calendar_popup(d.calendar_popup) self.widget.dateTimeChanged.connect(self.on_datetime_changed) #-------------------------------------------------------------------------- # Abstract API Implementation #-------------------------------------------------------------------------- def get_datetime(self): """ Return the current datetime in the control. Returns ------- result : datetime The current control datetime as a datetime object. """ return self.widget.dateTime().toPyDateTime() def set_minimum(self, datetime): """ Set the widget's minimum datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the minimum datetime. """ self.widget.setMinimumDateTime(datetime) def set_maximum(self, datetime): """ Set the widget's maximum datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the maximum datetime. """ self.widget.setMaximumDateTime(datetime) def set_datetime(self, datetime): """ Set the widget's current datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the datetime. """ self._guard |= CHANGED_GUARD try: self.widget.setDateTime(datetime) finally: self._guard &= ~CHANGED_GUARD def set_datetime_format(self, format): """ Set the widget's datetime format. Parameters ---------- format : str A Python time formatting string. """ # XXX make sure Python's and Qt's format strings are the # same, or convert between the two. self.widget.setDisplayFormat(format) def set_calendar_popup(self, popup): """ Set whether a calendar popup is available on the widget. Parameters ---------- popup : bool Whether the calendar popup is enabled. """ self.widget.setCalendarPopup(popup)