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() 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 setUpMainWindow(self): """Set up the GUI's main window.""" headers, data = self.loadCSVData() # Create QTableView object and set up its behavior table_view = QTableView() table_view.setSelectionMode( QAbstractItemView.SelectionMode.ExtendedSelection) # Set up the horizontal header so that cells resize to fit # contents, and so that the last column stretches to take up empty space table_view.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeMode.ResizeToContents) table_view.horizontalHeader().setStretchLastSection(True) self.model = TableModel(headers=headers, data=data) table_view.setModel(self.model) self.setCentralWidget(table_view)
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 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)