Using a qTableWidget to view my sqlite database entries.
The first column of the database is hidden since it's the ID that gets auto assigned to an entry.
Now I want the user to select the row he wants to delete and then be able to press the delete button.
I'm using the following to do that:
class StartScherm(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(StartScherm, self).__init__()
self.setupUi(self)
self.database_laden()
# -- Button presses with .connect -- #
self.tableSnelleKijk.doubleClicked.connect(self.weergave_scherm)
self.tableSnelleKijk.setSelectionBehavior(
QtWidgets.QTableView.SelectRows)
self.tableSnelleKijk.setColumnHidden(0, True)
Now I'm trying to make a function that once the users clicks on the delete button in the GUI, the function reads the ID so I can delete it from the sqlite database. I use this:
def delete(self):
index = self.tableSnelleKijk.selectedItems()
print(index[0].text())
unfortunately, this gives me the first visable column data as text instead of the hidden columnwith the ID.
How do I access the hidden data?
EDIT: changed column to row
The database is hidden on the tableview only, in sqlite it's simply there to have a rowID I can use to access a current row to delete/edit.
Database code:
c.execute("""CREATE TABLE Medailles (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
orde text,
periode text,
locatie text,
beschrijving text
)""")
The column is hidden in the QtableView with .setColumnHidden.
The database gets populated when the users decides to enter a new entry, this is the function that takes care of this:
def add(self):
orde = self.orde_comboBox.currentText()
periode = self.periode_input.text()
locatie = self.locatie_input.text()
beschrijving = self.beschrijving_input.toPlainText()
medaille = Medaille(orde, periode, locatie, beschrijving)
with conn:
c.execute("INSERT INTO Medailles (orde, periode, locatie, beschrijving) VALUES (:orde, :periode, :locatie, :beschrijving)", {
'orde': medaille.orde, 'periode': medaille.periode, 'locatie': medaille.locatie, 'beschrijving': medaille.beschrijving})
self.close()
question from:
https://stackoverflow.com/questions/65599785/how-to-get-data-from-hidden-id-column-in-qtablewidget