I have a QTableView displaying a QAbstractTableModel which represent my list of objects. One attribute of my object is a boolean (isExpired), I just want to modify this attribute when double clicking on the specific cell in the table view. So on myTableView If a double click on the cell in the column isExpired of the row corresponding to my object number 1, I want this attribute to go from False to True.
import sys
import os
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt
import pandas as pd
class Test:
def __init__(self):
self.id = ""
self.isExpired = ""
def toDataframe(self):
return{
'Id': self.id,
'isExpired': self.isExpired}
class Tests:
def __init__(self):
self.tests = []
test1 = Test()
test1.id = 1
test1.isExpired = False
self.tests.append(test1)
def toDataframe(self):
tickets_df = pd.DataFrame.from_records([t.toDataframe() for t in self.tests])
tickets_df = tickets_df[['Id', 'isExpired']]
return tickets_df
class MyTableView(QTableView):
def __init__(self, *args):
QTableView.__init__(self, *args)
# Table model
class TicketGUI(QAbstractTableModel):
def __init__(self):
QAbstractTableModel.__init__(self)
self.tickets = Tests()
data = self.tickets.toDataframe()
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
def headerData(self, col, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == 'main':
app = QApplication(sys.argv)
model = TicketGUI()
view = MyTableView()
view.setModel(model)
view.show()
sys.exit(app.exec_())