Hiperfly
Hiperfly

Reputation: 178

Make a cell editable (after creating the table with non-editable cells) with ctrl+click

I have a QTableWidget that is created when the program starts with non editable and non selectable cells. When I do right click on them the number on the cell increases by 1, which is fine.

What I want to do now is that if I ctrl+click in a cell, this one turns into an editable cell and I can write a number in it, and when the number is entered the cell becomes non editable again.

I have no problem with the modifiers so far and they work, but I can't manage to change the flags of the cell

def mousePressEvent(self,event):

    modifiers = event.modifiers()
    it = self.itemAt(event.pos())

    if modifiers & QtCore.Qt.ControlModifier:
          item.setFlags(itemIsEditable)
    else:                        
          if event.button() == QtCore.Qt.LeftButton:
              it.setText(str(round(float(it.text())+1)))
          elif event.button() == QtCore.Qt.RightButton:
              it.setText(str(round(float(it.text()) - 1)))

Upvotes: 1

Views: 1446

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

You have to use the editItem() method

def mousePressEvent(self,event):
    modifiers = event.modifiers()
    it = self.itemAt(event.pos())

    if modifiers & QtCore.Qt.ControlModifier:
          it.setFlags(it.flags() | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable)
          self.editItem(it)
    else:            
        if event.button() == QtCore.Qt.LeftButton:
              it.setText(str(round(float(it.text())+1)))
        elif event.button() == QtCore.Qt.RightButton:
              it.setText(str(round(float(it.text()) - 1)))

Upvotes: 2

Related Questions