简体   繁体   中英

How to get data from hidden 'id' column in QtableWidget

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()

If the data is in an invisible column, the items in that column cannot be selected. You can access those indexes (and their data) by using the sibling(row, column) function (or siblingAtColumn(column) for Qt>=5.11):

    def delete(self):
        rows = set()
        for index in self.tableSnelleKijk.selectedItems():
            id = index.sibling(index.row(), 0).text()
            rows.add(id)
        print('IDs to delete: {}'.format(sorted(rows)))

Do note that if you need to write data to the database or edit its layout you should consider using a basic QTableView with a QtSql.QSqlTableModel instead, otherwise you might face some inconsistencies, bugs or even data corruption if you're not really careful with your implementation.

To get the value of a hidden column in a QTableWidget (PyQt5):

row = self.tblWidget.currentRow()
col = position of hidden column - usually 0
ID  = self.tblWidget.item(row,col).text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM