def createOrdersTab(self): """Create the page to view the Orders table from the database.""" ord_sql_model = QSqlRelationalTableModel() ord_sql_model.setTable("Orders") ord_sql_model.setRelation( ord_sql_model.fieldIndex("product_id"), QSqlRelation("Products", "product_id", "product_name")) ord_sql_model.setRelation( ord_sql_model.fieldIndex("customer_id"), QSqlRelation("Customers", "customer_id", "first_name")) ord_sql_model.setHeaderData(ord_sql_model.fieldIndex("customer_id"), Qt.Orientation.Horizontal, "customer_name") ord_sql_model.select() # Populate the model with data ord_proxy_model = QSortFilterProxyModel() ord_proxy_model.setSourceModel(ord_sql_model) ord_table = QTableView() ord_table.setSortingEnabled(True) ord_table.setModel(ord_proxy_model) ord_table.setItemDelegate(SqlProxyDelegate(ord_table)) ord_table.setItemDelegateForColumn( ord_sql_model.fieldIndex("date_of_order"), DateDelegate()) ord_table.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeMode.Stretch) ord_h_box = QHBoxLayout() ord_h_box.addWidget(ord_table) self.orders_tab.setLayout(ord_h_box)
def createCustomersTab(self): """Create the page to view the Customers table from the database.""" cust_sql_model = QSqlRelationalTableModel() cust_sql_model.setTable("Customers") cust_sql_model.setRelation( cust_sql_model.fieldIndex("staff_id"), QSqlRelation("Staff", "staff_id", "username")) cust_sql_model.setHeaderData(cust_sql_model.fieldIndex("staff_id"), Qt.Orientation.Horizontal, "staff_username") cust_sql_model.select() # Populate the model with data cust_proxy_model = QSortFilterProxyModel() cust_proxy_model.setSourceModel(cust_sql_model) cust_table = QTableView() cust_table.setSortingEnabled(True) cust_table.setModel(cust_proxy_model) cust_table.setItemDelegate(SqlProxyDelegate(cust_table)) cust_table.setItemDelegateForColumn(cust_sql_model.fieldIndex("phone"), PhoneDelegate()) cust_table.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeMode.Stretch) cust_h_box = QHBoxLayout() cust_h_box.addWidget(cust_table) self.customer_tab.setLayout(cust_h_box)
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 createCategoriesTab(self): """Create the page to view the Categories table from the database.""" cat_sql_model = QSqlRelationalTableModel() cat_sql_model.setTable("Categories") cat_sql_model.select() # Populate the model with data cat_proxy_model = QSortFilterProxyModel() cat_proxy_model.setSourceModel(cat_sql_model) cat_table = QTableView() cat_table.setSortingEnabled(True) cat_table.setModel(cat_proxy_model) cat_table.setItemDelegateForColumn( cat_sql_model.fieldIndex("category_id"), ReadOnlyDelegate()) cat_table.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeMode.Stretch) cat_h_box = QHBoxLayout() cat_h_box.addWidget(cat_table) self.category_tab.setLayout(cat_h_box)
def createStaffTab(self): """Create the page to view the Staff table from the database.""" staff_sql_model = QSqlRelationalTableModel() staff_sql_model.setTable("Staff") staff_sql_model.select() # Populate the model with data staff_proxy_model = QSortFilterProxyModel() staff_proxy_model.setSourceModel(staff_sql_model) staff_table = QTableView() staff_table.setSortingEnabled(True) staff_table.setModel(staff_proxy_model) staff_table.setItemDelegateForColumn( staff_sql_model.fieldIndex("staff_id"), ReadOnlyDelegate()) staff_table.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeMode.Stretch) staff_h_box = QHBoxLayout() staff_h_box.addWidget(staff_table) self.staff_tab.setLayout(staff_h_box)