May-25-2022, 12:54 AM
Hello again,
I'm trying to figure out how to make my SellPrice, ID, Quantity, & Date columns Un-editable/read-only when you click on their columns (Because right now you can click on them, modify the values, and it saves t the database. I would like to make it so you can't modify those specific columns ).
I know in the InitializeModel() it makes the Table editable, but how to I filter out certain columns so they can't be changed when the user clicks on the column/cell to try and modify the values?
(This way I force the user to go through the proper way of updating the values. Let's say the Quantity needs updating for example, Instead of having the user manually upadte it in the table, they must actually checkout/return the item by scanning it's barcode. Therefore, that cell/column needs to be read-only/Non-editable to prevent this manual/accidental changes)
So Is there a way to set those certain indexes/columns in the InventoryDisplay so they have editable = false or something?
Any help is much appreciated.
Thanks in advance.
Snippet:
Will my sellPrice update if the Price was updated? If not, how could I get that to work?
[My sellPrice is just a formula (Price * Markup -> Where Markup = 1.10 and Price is the number the user inputted in the database from the AddItems Function)]
I'm trying to figure out how to make my SellPrice, ID, Quantity, & Date columns Un-editable/read-only when you click on their columns (Because right now you can click on them, modify the values, and it saves t the database. I would like to make it so you can't modify those specific columns ).
I know in the InitializeModel() it makes the Table editable, but how to I filter out certain columns so they can't be changed when the user clicks on the column/cell to try and modify the values?
(This way I force the user to go through the proper way of updating the values. Let's say the Quantity needs updating for example, Instead of having the user manually upadte it in the table, they must actually checkout/return the item by scanning it's barcode. Therefore, that cell/column needs to be read-only/Non-editable to prevent this manual/accidental changes)
So Is there a way to set those certain indexes/columns in the InventoryDisplay so they have editable = false or something?
Any help is much appreciated.
Thanks in advance.
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) def initializeModel(self): self.model.setTable('items') self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.select() #---------------------------------- # Update Inventory #---------------------------------- #TODO: Make SellPrice, ID, Quantity, & Date Uneditable #Have SellPrice update if Price is updated 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) #---------------------------------- #----------------------------------------------------------------------------------------------------The full code:
import sys from PyQt5 import QtWidgets from PyQt5.QtSql import QSqlDatabase, QSqlTableModel from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow, QLabel, QLineEdit, QTableWidget, QTableWidgetItem, QGridLayout, QVBoxLayout, QSizePolicy, QSpacerItem, QMessageBox,QSpinBox, QTableView) from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication from PyQt5.QtGui import QFont import sqlite3 from AddItemScreen import Ui_AddItemMenu from Constants import MainDatabase class Ui_MainDisplay(QMainWindow): def __init__(self, parent = None): super(Ui_MainDisplay, self).__init__(parent) self.setObjectName("MainDisplay") self.setGeometry(0, 0, 1123, 903) self.setStyleSheet("background-color: rgb(0, 170, 255);") self.centralwidget = QWidget(self) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") self.verticalLayout = QVBoxLayout() self.verticalLayout.setContentsMargins(-1, 0, -1, 0) self.verticalLayout.setSpacing(6) self.verticalLayout.setObjectName("verticalLayout") self.AddItemButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.AddItemButton.setFont(font) self.AddItemButton.setStyleSheet("background-color: rgb(85, 255, 0);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "\n" "") self.AddItemButton.setObjectName("AddItemButton") self.verticalLayout.addWidget(self.AddItemButton) self.DeleteItemButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.DeleteItemButton.setFont(font) self.DeleteItemButton.setStyleSheet("background-color: rgb(255, 65, 68);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.DeleteItemButton.setObjectName("DeleteItemButton") self.verticalLayout.addWidget(self.DeleteItemButton) self.CheckoutButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.CheckoutButton.setFont(font) self.CheckoutButton.setStyleSheet("background-color: rgb(255, 255, 0);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.CheckoutButton.setObjectName("CheckoutButton") self.verticalLayout.addWidget(self.CheckoutButton) self.ReturnButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.ReturnButton.setFont(font) self.ReturnButton.setStyleSheet("background-color: rgb(255, 170, 32);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.ReturnButton.setObjectName("ReturnButton") self.verticalLayout.addWidget(self.ReturnButton) self.ScanBarcodeButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.ScanBarcodeButton.setFont(font) self.ScanBarcodeButton.setStyleSheet("background-color: rgb(211, 211, 211);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.ScanBarcodeButton.setObjectName("ScanBarcodeButton") self.verticalLayout.addWidget(self.ScanBarcodeButton) self.MoreInfoButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.MoreInfoButton.setFont(font) self.MoreInfoButton.setStyleSheet("background-color: rgb(196, 17, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;") self.MoreInfoButton.setObjectName("MoreInfoButton") self.verticalLayout.addWidget(self.MoreInfoButton) self.RefreshButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.RefreshButton.setFont(font) self.RefreshButton.setStyleSheet("background-color: rgb(0, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "\n" "") self.RefreshButton.setObjectName("RefreshButton") self.verticalLayout.addWidget(self.RefreshButton) spacerItem = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding) self.verticalLayout.addItem(spacerItem) self.LogoutButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.LogoutButton.setFont(font) self.LogoutButton.setStyleSheet("background-color: rgb(255, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.LogoutButton.setObjectName("LogoutButton") self.verticalLayout.addWidget(self.LogoutButton) self.gridLayout.addLayout(self.verticalLayout, 4, 3, 1, 1) self.Header = QLabel(self.centralwidget) font = QFont() font.setPointSize(15) font.setBold(True) font.setWeight(75) self.Header.setFont(font) self.Header.setStyleSheet("background-color: rgb(0, 0, 0);\n" "color: rgb(255, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;") self.Header.setAlignment(Qt.AlignmentFlag.AlignCenter) self.Header.setObjectName("Header") self.gridLayout.addWidget(self.Header, 0, 1, 1, 3) self.SearchButton = QPushButton(self.centralwidget) font = QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.SearchButton.setFont(font) self.SearchButton.setStyleSheet("background-color: rgb(211, 211, 211);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self.SearchButton.setObjectName("SearchButton") self.gridLayout.addWidget(self.SearchButton, 2, 1, 1, 1) self.SearchBar = QLineEdit(self.centralwidget) self.SearchBar.setStyleSheet("background-color: rgb(255, 255, 255);") self.SearchBar.setObjectName("SearchBar") self.gridLayout.addWidget(self.SearchBar, 2, 2, 1, 1) self.retranslateUi(self) QMetaObject.connectSlotsByName(self) #---------------------------------------------------------------------------------------------------- # 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) def initializeModel(self): self.model.setTable('items') self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.select() #---------------------------------- # Update Inventory #---------------------------------- #TODO: Make SellPrice, ID, Quantity, & Date Uneditable #Have SellPrice update is Price is updated 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) #---------------------------------- #---------------------------------------------------------------------------------------------------- def retranslateUi(self, MainDisplay): _translate = QCoreApplication.translate self.setWindowTitle(_translate("MainDisplay", "AdminMenu")) self.AddItemButton.setText(_translate("MainDisplay", "Add Item")) self.DeleteItemButton.setText(_translate("MainDisplay", "Delete Item")) self.CheckoutButton.setText(_translate("MainDisplay", "Check Out")) self.ReturnButton.setText(_translate("MainDisplay", "Return")) self.ScanBarcodeButton.setText(_translate("MainDisplay", "Scan Barcode")) self.MoreInfoButton.setText(_translate("MainDisplay", "More Info")) self.RefreshButton.setText(_translate("MainDisplay", "Refresh")) self.LogoutButton.setText(_translate("MainDisplay", "Log Out")) self.Header.setText(_translate("MainDisplay", "Admin Menu")) self.SearchButton.setText(_translate("MainDisplay", "Search")) #---------------------------------------------------------------------------------------------------- # Button Actions #---------------------------------------------------------------------------------------------------- #------------------------------------------ #Logout Button #------------------------------------------ #When the Logout button is clicked -> LogoutClicked Function LogoutButton = self.LogoutButton LogoutButton.clicked.connect(self.LogoutClicked) #------------------------------------------ #------------------------------------------ #Add Item Button #------------------------------------------ #When the AddItem button is clicked -> AddItem Function AddItemButton = self.AddItemButton AddItemButton.clicked.connect(self.AddItemClicked) #------------------------------------------ #------------------------------------------ #Remove Item Button #------------------------------------------ #When the RemoveItem button is clicked -> RemoveItem Function RemoveItemButton = self.DeleteItemButton RemoveItemButton.clicked.connect(self.RemoveItemClicked) #------------------------------------------ #------------------------------------------ #Checkout Item Button #------------------------------------------ #When the Checkout button is clicked -> Checkout Function CheckoutButton = self.CheckoutButton CheckoutButton.clicked.connect(self.CheckoutClicked) #------------------------------------------ #------------------------------------------ #Return Item Button #------------------------------------------ #When the Return button is clicked -> Return Function ReturnButton = self.ReturnButton ReturnButton.clicked.connect(self.ReturnClicked) #------------------------------------------ #------------------------------------------ #Scan Barcode Button #------------------------------------------ #When the Scan Barcode button is clicked -> ScanBarcode Function ScanBarcodeButton = self.ScanBarcodeButton ScanBarcodeButton.clicked.connect(self.ScanBarcodeClicked) #------------------------------------------ #------------------------------------------ #More Info Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function MoreInfoButton = self.MoreInfoButton MoreInfoButton.clicked.connect(self.MoreInfoClicked) #------------------------------------------ #------------------------------------------ #Refresh Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function RefreshButton = self.RefreshButton RefreshButton.clicked.connect(self.RefreshClicked) #------------------------------------------ #------------------------------------------ #Search Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function SearchButton = self.SearchButton SearchButton.clicked.connect(self.SearchClicked) #------------------------------------------ #---------------------------------- #Logout Function def LogoutClicked(self): #Print in terminal for testing: print("The Logout Button was clicked") #Switch from this screen to the LoginScreen #(Import LoginScreen here to prevent circular import error) from LoginScreen import Ui_Loginscreen self.win = Ui_Loginscreen() #Define LoginScreen self.win.show() #Show Login Screen self.close() #Close this screen (AdminMenu) #---------------------------------- #---------------------------------- #AddItem Function def AddItemClicked(self): #Print in terminal for testing: print("The Add Item Button was clicked") #Switch from this screen to the AddItems Screen (Scene Swap): self.win = Ui_AddItemMenu() self.win.show() self.close() #---------------------------------- #---------------------------------- #RemoveItem Function def RemoveItemClicked(self): #Print in terminal for testing: print("The Delete Item Button was clicked") self.DeleteConfirmation() #---------------------------------- # Delete Item Confirmation #---------------------------------- def DeleteConfirmation(self): msgBox = QMessageBox() msgBox.setIcon(QMessageBox.Warning) msgBox.setText("Are you sure you want to delete this item?") msgBox.setInformativeText("*This cannot be undone") msgBox.setWindowTitle("Delete Item Confirmation") msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No) returnValue = msgBox.exec() if returnValue == QMessageBox.Yes: row = self.InventoryDisplay.currentIndex().row() self.model.removeRow(row) self.initializeModel() self.InventoryDisplay.selectRow(row) #---------------------------------- #---------------------------------- #Checkout Function def CheckoutClicked(self): #Print in terminal for testing: print("The Checkout Button was clicked") #---------------------------------- #---------------------------------- #Return Function def ReturnClicked(self): #Print in terminal for testing: print("The Return Button was clicked") #---------------------------------- #---------------------------------- #ScanBarcode Function def ScanBarcodeClicked(self): #Print in terminal for testing: print("The Scan Barcode Button was clicked") #---------------------------------- #---------------------------------- #MoreInfo Function def MoreInfoClicked(self): #Print in terminal for testing: print("The More Info Button was clicked") #---------------------------------- #---------------------------------- #Refresh Function def RefreshClicked(self): #Print in terminal for testing: print("The Refresh Button was clicked") #Close and reopen the app (Refresh) self.win = Ui_MainDisplay() self.win.show() self.close() #---------------------------------- #---------------------------------- #Search Function def SearchClicked(self): #Print in terminal for testing: print("The Search Button was clicked") userQuery = self.SearchBar.text() #Connect to the inventory database (inventory.db) connection = sqlite3.connect(MainDatabase) cursor = connection.cursor() cursor.execute("SELECT * FROM items WHERE Name LIKE'%'||?||'%'",(userQuery,)) connection.commit() #Get all info from the Items Table result = cursor.fetchall() print(result) #Close the connection to the database connection.close() #---------------------------------- #---------------------------------------------------------------------------------------------------- if __name__ == '__main__': app = QApplication(sys.argv) win = Ui_MainDisplay() win.show() sys.exit(app.exec_())Side Question:
Will my sellPrice update if the Price was updated? If not, how could I get that to work?
[My sellPrice is just a formula (Price * Markup -> Where Markup = 1.10 and Price is the number the user inputted in the database from the AddItems Function)]