0

I have inserted data into a cell displayed by QTableView if the cell is beeing clicked it emits a signal to a function that inserts data into that cell

 self.tableview.clicked.connect(self.insertdata_onclick)



def insertdata_onclick(self, data):
        x = self.tableview.selectionModel().currentIndex().row()
        y = self.tableview.selectionModel().currentIndex().column()
        self.datamodel.input_data[x][y] = data
        self.datamodel.layoutChanged.emit()
        # cell selected
        # get position of that cell
        # change data of that cell

How can I replicate this behavior if the cell is currently selected ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • `self.tableview.activated.connect(self.insertdata_onclick)` or `self.tableview.pressed.connect(self.insertdata_onclick)` – S. Nick Jun 12 '20 at 12:04

1 Answers1

0

clicked signal goes with index but not data:

void QAbstractItemView::clicked(const QModelIndex &index)

so instead of

def insertdata_onclick(self, data):

you should make it like:

def insertdata_onclick(self, index):
    data_i_want_to_insert = self.get_my_data_somewhere(index.row(), index.column())
    self.datamodel.input_data[index.row()][index.column()] = data_i_want_to_insert 
    self.datamodel.layoutChanged.emit()

and if you click on cell, it will be selected unless in "NoSelection" mode. You don't need to test "if the cell is currently selected".

ToSimplicity
  • 293
  • 1
  • 8
  • it is just copied from the very original code. you may want to so update "data_i_want_to_insert" into your table model. if data source of your table model is model.input_data as a list of lists(such as [[1, 2], [3, 4], ] ), the line would work, because it puts the data in to the spot. – ToSimplicity Jun 15 '20 at 09:00
  • But, if you want to the data in clicked cell instead, and use that data elsewhere like send it to your friend, you can get the data by index.data(some_qt_data_role) – ToSimplicity Jun 15 '20 at 09:03
  • sorry copied the wrong code I mean this line ` data_i_want_to_insert = self.get_my_data_somere(index.row(), index.column())` , if i have self.data = [1] I need to set an index to it to insert it into the cell ? –  Jun 15 '20 at 09:18
  • this line should find the data you want to insert into given (row, column). so as in your comment, do you want to insert [1] for any row and column? if that is the case, data_i_want_to_insert = self.data – ToSimplicity Jun 15 '20 at 09:23