0

I currently work on a pyside2 project. I use some QTableWidget and i was wondering if it is possible to, when i edit a QTableWidgetItem, the value during the edit is a part of a calcul and when i stop the editing, then it calcul the new value to display.

i already created an object "MyQTableWidgetItem" inherited from QTableWidgetItem. and "MyQTableWidgetItem" accepts an object as init.

and i imagined something like :

class MyObject:
    def  __init__(self, int_1, int_2):
        self.int_1 = int_1
        self.int_2 = int_2
    def  get_total(self):
        return self.int_1 + self.int_2

class MyQTableWidgetItem(QTableWidgetItem):
    def __init__(self, my_object: MyObject):
        self.my_object = my_object
        super(MyQTableWidgetItem, self).__init__(my_object.get_total())

    def on_edit(self):
        # modify only my_object.int_1 and not the entire calcul 
        # (show only my_object.int_1 for editing)

    def on_stop_editing(self): #if needed
        # will display my_object.get_total()

But i really have no idea how to do it, could you please tell me how to do it?

I can easily understand qt's c++ to translante it on pyside2 so, if you have some ideas with c++ i'll take it too.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
FinnStark
  • 30
  • 4
  • Could you explain me better. According to your code you must add 2 amounts, what are those amounts? Since in theory a QTableWidgetItem provides a single value – eyllanesc May 30 '20 at 14:12
  • Hello, the importante thing is, they do a calcul and when i edit a cell, it display int_1 and allow to modify only int_1, not int_2 then, when i stop editing, it display, in the cell, the calcul my_object.get_total() (self.int_1 + self.int_2). There is always only one single value : during display : the total and during editing : int_1 is displayed. I want when i start editing, the value switch to int_1 and when i stop editing, it switch to the total refreshed with the new value. – FinnStark May 30 '20 at 14:57
  • The solution depends on the context and everything is important, the importance depends on the possible solution, and I think that QTableWidgetItem is not the solution since that class only serves to access or set the model values, and it is not notified when editing a value. Going to the problem, according to what I understand there are 3 elements: input1, input2 and output. 1) I suppose that "input1" and "input2" are 2 different cells, am I correct ?, 2) and I suppose that "output" is the information of a third cell. Am I correct? – eyllanesc May 30 '20 at 15:04
  • 3) And you want that when modifying "input1" and "input2" it should update "output", am I correct? – eyllanesc May 30 '20 at 15:04
  • i do not want input1 and input2 as column. Actually i should have ask for a method like "after_stop_editing" that is called after the user have finish editing – FinnStark May 30 '20 at 15:34
  • It seems that you have remained trivial, the columns serve to filter, for example if you want to track the modification of an item then it is enough with: `if item.row() == foo_value and item.column() == bar_vaue: r = calculate() update_other_item(r)` – eyllanesc May 30 '20 at 15:52
  • The on_itemChanged method is triggered after finishing the edit similar to "after_stop_editing" – eyllanesc May 30 '20 at 15:53
  • As I pointed out, the QTableWidgetItem is like a lock key, it serves to access and modify the information but it is not notified if the information has changed so it does not have an "after_stop_editing" method, "after_stop_editing" is notified through the itemChanged signal – eyllanesc May 30 '20 at 15:56
  • Ok, thank you very much for the time you take to answer and your answers. – FinnStark May 30 '20 at 23:28

0 Answers0