def createEditor(self, parent, option, index): """Set up the widget used to edit the item data at the selected index.""" editor = QDateEdit(parent) # Create the editor widget editor.setDateRange(QDate(1900, 1, 1), QDate.currentDate()) editor.setDisplayFormat("MM/dd/yyyy") editor.setAlignment( Qt.AlignmentFlag.AlignRight) # Align the text of the editor widget editor.setAutoFillBackground( True) # Prevent the background from showing when editing return editor
def setUpMainWindow(self): """Set up the GUI's main window.""" header_label = QLabel("List of Users") # Create model and table objects model = QStandardItemModel() model.setColumnCount(3) model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"]) table_view = QTableView() table_view.setEditTriggers( QAbstractItemView.EditTrigger.NoEditTriggers) # NOTE: Uncomment for table cells to be unselectable #table_view.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection) table_view.setModel(model) table_view.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.Stretch) names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"] # Add items to each row in the table by looping over # the names_list and adding the date edit and button widgets for row, name in enumerate(names_list): model.setItem(row, QStandardItem(name)) # Setting the widget at an index in a QTableView involves # acquiring the QModelIndex values of the current position. # One way to do this is to use the QAbstractItemModel.sibling() # method to retrieve the QModelIndex index from the specified # row and column (here the column is 1) index = table_view.model().sibling(row, 1, QModelIndex()) date_edit = QDateEdit(QDate.currentDate( )) # Create QDateEdit object that starts at current date date_edit.setDateRange(QDate(1900, 1, 1), QDate.currentDate()) date_edit.setDisplayFormat("MM/dd/yyyy") date_edit.setAlignment( Qt.AlignmentFlag.AlignRight) # Align the text date_edit.setAutoFillBackground(True) table_view.setIndexWidget(index, date_edit) # Set the widgets in the final column for each row index = table_view.model().sibling(row, 2, QModelIndex()) table_view.setIndexWidget(index, EditCellWidget(table_view)) # Set up main layout and container object for main window main_v_box = QVBoxLayout() main_v_box.addWidget(header_label) main_v_box.addWidget(table_view) container = QWidget() container.setLayout(main_v_box) self.setCentralWidget(container)
def setEditorData(self, editor, index): """Populate the editor widget, which is QDateEdit for this example, with data.""" # Get the current item value from the model at the selected index date = index.model().data(index, Qt.ItemDataRole.EditRole) # Convert the string, date, to a QDate object. Using '/' # in the format parameter treats the slashes as text # and not part of the date new_date = QDate().fromString(date, "MM'/'dd'/'yyyy") editor.setDate( new_date ) # Use the QDateTime method setDate() to set the value of the editor
def setUpMainWindow(self): """Set up the GUI's main window.""" header_label = QLabel("List of Users") # Create model and table objects model = QStandardItemModel() model.setColumnCount(3) model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"]) table_view = QTableView() # NOTE: setEditTriggers() is not used so that the user # can double-click and edit cells table_view.setModel(model) table_view.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.Stretch) # Set the item delegate for a specific column, in this case column 1 table_view.setItemDelegateForColumn(1, DateEditDelegate()) names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"] # Add items to each row in the table by looping over # the names_list and adding the date edit and button widgets for row, name in enumerate(names_list): model.setItem(row, QStandardItem(name)) # Create an item and set the initial value for the second column. # Here the QDate values are converted to strings to make it easier # to align the text without having to subclass a model class date_item = QStandardItem( QDate.currentDate().toString("MM/dd/yyyy")) date_item.setTextAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight) model.setItem(row, 1, date_item) # Set the widgets in the final column for each row index = model.index(row, 2, QModelIndex()) table_view.setIndexWidget(index, EditCellWidget(table_view)) # Set up main layout and container object for main window main_v_box = QVBoxLayout() main_v_box.addWidget(header_label) main_v_box.addWidget(table_view) container = QWidget() container.setLayout(main_v_box) self.setCentralWidget(container)
def _formatdAffixes(self) -> None: self._formatted_prefix = "" self._formatted_postfix = "" self._formatted_path = "" if not self._global_stack: return extruder_stack = self._application.getExtruderManager( ).getActiveExtruderStacks()[0] if not extruder_stack: return try: extruder_nr = int( extruder_stack.getProperty("extruder_nr", "value")) except ValueError: return job_path = self._global_stack.getMetaDataEntry("custom_job_path", "") job_prefix = self._global_stack.getMetaDataEntry( "custom_job_prefix", "{printer_type}") job_postfix = self._global_stack.getMetaDataEntry( "custom_job_postfix", "") profile_name = self._global_stack.quality.getName() if self._global_stack.qualityChanges.id != "empty_quality_changes": profile_name = self._global_stack.qualityChanges.getName() material_name = "%s %s" % (extruder_stack.material.getMetaDataEntry( "brand"), extruder_stack.material.getName()) try: date_format = Qt.DateFormat.ISODate except AttributeError: date_format = Qt.ISODate replacements = { "{printer_name}": self._abbreviate_name(self._global_stack.getName()), "{printer_name_full}": self._global_stack.getName(), "{printer_type}": self._abbreviate_name(self._global_stack.definition.getName()), "{printer_type_full}": self._global_stack.definition.getName(), "{layer_height}": self._abbreviate_number( self._global_stack.getProperty("layer_height", "value")), "{machine_nozzle_size}": self._abbreviate_number( extruder_stack.getProperty("machine_nozzle_size", "value")), "{infill_sparse_density}": self._abbreviate_number( extruder_stack.getProperty("infill_sparse_density", "value")), "{speed_print}": self._abbreviate_number( extruder_stack.getProperty("speed_print", "value")), "{material_flow}": self._abbreviate_number( extruder_stack.getProperty("material_flow", "value")), "{profile_name}": self._abbreviate_name(profile_name), "{profile_name_full}": profile_name, "{material_name}": self._abbreviate_name(material_name), "{material_name_full}": material_name, "{material_type}": self._abbreviate_name( extruder_stack.material.getMetaDataEntry("material")), "{material_type_full}": extruder_stack.material.getMetaDataEntry("material"), "{material_weight}": str( round(self._print_information.materialWeights[extruder_nr]) if extruder_nr < len(self._print_information.materialWeights) else 0), "{print_time_hours}": str(self._print_information.currentPrintTime.days * 24 + self._print_information.currentPrintTime.hours), "{print_time_minutes}": str(self._print_information.currentPrintTime.minutes).zfill(2), "{date_iso}": QDate.currentDate().toString(format=date_format), "{date_year}": QDate.currentDate().toString("yy"), "{date_month}": QDate.currentDate().toString("MM"), "{date_day}": QDate.currentDate().toString("dd"), "{time_iso}": QTime.currentTime().toString(format=date_format), "{time_hour}": QTime.currentTime().toString("HH"), "{time_minutes}": QTime.currentTime().toString("mm") } replacements = dict((re.escape(k), v) for k, v in replacements.items()) # escape for re pattern = re.compile("|".join(replacements.keys())) job_prefix = pattern.sub(lambda m: replacements[re.escape(m.group(0))], job_prefix) job_postfix = pattern.sub( lambda m: replacements[re.escape(m.group(0))], job_postfix) job_path = pattern.sub(lambda m: replacements[re.escape(m.group(0))], job_path) if self._preferences.getValue("customjobprefix/sanitise_affixes"): job_prefix = self._stripAccents(job_prefix).replace(" ", "_") job_postfix = self._stripAccents(job_postfix).replace(" ", "_") job_path = self._stripAccents(job_path).replace(" ", "_").strip("/") if job_prefix != self._formatted_prefix or job_postfix != self._formatted_postfix or job_path != self._formatted_path: self._formatted_prefix = job_prefix self._formatted_postfix = job_postfix self._formatted_path = job_path self.jobAffixesChanged.emit()
#!/usr/bin/python from PyQt6.QtCore import QDate, Qt borodino_battle = QDate(1812, 9, 7) slavkov_battle = QDate(1805, 12, 2) now = QDate.currentDate() j_today = now.toJulianDay() j_borodino = borodino_battle.toJulianDay() j_slavkov = slavkov_battle.toJulianDay() d1 = j_today - j_slavkov d2 = j_today - j_borodino print(f'Days since Slavkov battle: {d1}') print(f'Days since Borodino battle: {d2}')
#!/usr/bin/python from PyQt6.QtCore import QDate, Qt now = QDate.currentDate() y = now.year() print(f'today is {now.toString(Qt.DateFormat.ISODate)}') xmas1 = QDate(y-1, 12, 25) xmas2 = QDate(y, 12, 25) dayspassed = xmas1.daysTo(now) print(f'{dayspassed} days have passed since last XMas') nofdays = now.daysTo(xmas2) print(f'There are {nofdays} days until next XMas')
#!/usr/bin/python from PyQt6.QtCore import QDate, Qt now = QDate.currentDate() print('Gregorian date for today:', now.toString(Qt.DateFormat.ISODate)) print('Julian day for today:', now.toJulianDay())
#!/usr/bin/python from PyQt6.QtCore import QDate now = QDate.currentDate() d = QDate(1945, 5, 7) print(f'Days in month: {d.daysInMonth()}') print(f'Days in year: {d.daysInYear()}')