Jun-05-2022, 09:32 PM
So here's what I'm trying to do.
I have two scripts: AdminMenu.py and the CheckoutPoup.py.
When the user selects an entire row(as show in the attached screenshot) the selected item's Name, and ID are carried over from the AdminMenu.py to the CheckoutPopup.py. That way I can use the item's ID in my SQL UPDATE statement and display the item's Name on the Checkout popup window so the user knows what Item they are checking out (The message/label will display something like: "You are taking: 4 Test Item").
So how do I carry over the selected item's Name and ID to the Checkout.py?
(Since I plan on doing it this way I no longer have to carry over the SpinBox value since everything will be done in the CheckoutPopup.py. But I still need to figure out how to carry the Name & ID over)
AdminMenu.py Inventory Display code snippet:
I have two scripts: AdminMenu.py and the CheckoutPoup.py.
When the user selects an entire row(as show in the attached screenshot) the selected item's Name, and ID are carried over from the AdminMenu.py to the CheckoutPopup.py. That way I can use the item's ID in my SQL UPDATE statement and display the item's Name on the Checkout popup window so the user knows what Item they are checking out (The message/label will display something like: "You are taking: 4 Test Item").
So how do I carry over the selected item's Name and ID to the Checkout.py?
(Since I plan on doing it this way I no longer have to carry over the SpinBox value since everything will be done in the CheckoutPopup.py. But I still need to figure out how to carry the Name & ID over)
AdminMenu.py Inventory Display code snippet:
#---------------------------------------------------------------------------------------------------- # Inventory Display #---------------------------------------------------------------------------------------------------- #Connect to Database self.db = QSqlDatabase.addDatabase('QSQLITE') self.db.setDatabaseName('inventory.db') self.model = QSqlTableModel() self.delrow = -1 self.initializeModel() self.sbar = self.statusBar() self.InventoryDisplay = QTableView() self.InventoryDisplay.setStyleSheet("background-color: rgb(255, 255, 255);") self.InventoryDisplay.setModel(self.model) self.InventoryDisplay.clicked.connect(self.findrow) self.InventoryDisplay.selectionModel().selectionChanged.connect(self.getCellText) self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2) self.setCentralWidget(self.centralwidget) #Call the function to calculate the SellPrice self.calculate_sellprice() def initializeModel(self): self.model.setTable('items') self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.select() #------------------------------------------ #Search/Filter #------------------------------------------ #Allows the user to search for items self.SearchFilter.clear() for i in range(self.model.columnCount()): self.SearchFilter.addItem(self.model.headerData(i, QtCore.Qt.Horizontal)) self.SearchFilter.setCurrentIndex(1) self.SearchBar.textChanged.connect(self.filter_table) def filter_table(self, text): userQuery = " {} LIKE '%{}%'".format(self.SearchFilter.currentText(), text.lower()) if text else text self.model.setFilter(userQuery) self.model.select() #------------------------------------------ #---------------------------------- # Update Inventory #---------------------------------- def findrow(self, i): self.delrow = i.row() def getCellText(self): if self.InventoryDisplay.selectedIndexes(): item = self.InventoryDisplay.selectedIndexes()[0] row = self.selectedRow() column = self.selectedColumn() if not item == None: name = item.data() self.sbar.showMessage(str(name)) def selectedRow(self): if self.InventoryDisplay.selectionModel().hasSelection(): row = self.InventoryDisplay.selectionModel().selectedIndexes()[0].row() return int(row) def selectedColumn(self): column = self.InventoryDisplay.selectionModel().selectedIndexes()[0].column() return int(column) #---------------------------------- #------------------------------------------------------------------------ #When Price is Updated Automatically Update SellPrice When Refresh is Hit #------------------------------------------------------------------------ def calculate_sellprice(self): for row in range(self.InventoryDisplay.model().rowCount() - 1): sell_price = str(self.InventoryDisplay.model().index(row, 3).data()) if sell_price: sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}' self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price) #---------------------------------- #Make Specific Columns Un-Editable/ReadOnly #---------------------------------- class ReadOnlyDelegate(QStyledItemDelegate): def createEditor(self, parent, option, index): print('This column is Read-Only') return delegate = ReadOnlyDelegate(self) self.InventoryDisplay.setItemDelegateForColumn(0, delegate) #ID self.InventoryDisplay.setItemDelegateForColumn(2, delegate) #Quantity self.InventoryDisplay.setItemDelegateForColumn(4, delegate) #SellPrice self.InventoryDisplay.setItemDelegateForColumn(10, delegate) #Date Added #---------------------------------- #----------------------------------------------------------------------------------------------------