0

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_())
alaintk
  • 1
  • 1
  • 4

1 Answers1

0

QTableView (which inherits from QAbstractItemView) has the signal doubleClicked, which is emitted when a cell in the table is double clicked, and passes the QModelIndex that was clicked. https://doc.qt.io/qt-5/qabstractitemview.html#doubleClicked

You should connect this signal of your tableView to a function in your model, and if the column with your boolean field matches the index, toggle it for the corresponding row in your models' data.

TheKewlStore
  • 304
  • 1
  • 6
  • Thank you ! I used the following within my view self.doubleClicked.connect(self.slotDoubleClicked). When I double click on a cell the function slotDoubleClicked is launched and it prints "Double clicked on the console". But how can I now retrieve the object clicked and amend its field ? – alaintk Aug 20 '19 at 16:24
  • How can I retrieve the QModelIndex of my double click ? – alaintk Aug 20 '19 at 16:27
  • add a parameter to your function slotDoubleClicked, and it will be equal to the QModelIndex that was clicked when called. so something like this: ```python def slotDoubleClicked(self, index): if index.isValid(): do_something() ``` Signals/slots in pyqt are connected by looking at the signature of the method you connect to. It will pass the parameters of the signal based on what parameters your function accepts, and there are always several overloaded options, i.e. no parameters, one parameters, all parameters, etc. – TheKewlStore Aug 20 '19 at 17:12
  • Thank you ! Last thing now that I have retrieved my index within my view, how can I amend the content of the corresponding object? How can I use the model method? – alaintk Aug 21 '19 at 07:48
  • you can call self.model() in your table view class in order to get a reference to the connected model, which you can then call functions on. – TheKewlStore Aug 21 '19 at 14:20