I hope somebody could help me with this annoying issue I've been battling for a while. I have managed with the code attached to insert buttons in a tableview in a delegate column.
The problem is that to press the button, I need to "activate" the containing cell with a double click. Once the cell is active I can press the button, so in total I need 3 click to press it. This could become confusing for your average user.
I posted this problem to the pyqt mailing list, and the answer I got was:
"What happens is, when the TableWidget receives a click, it creates an editor, but the editor has not yet received the click. That is perfect in most cases, but in case you draw a button it isn't."
Has anyone been here before?
Thanks in advance, Cris
class AnimLinkButtons(QtGui.QStyledItemDelegate):
mouse_isPressed = False
def __init__(self, parent = None):
QtGui.QStyledItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
column = index.column()
button = QtGui.QPushButton(parent)
button.setText(self.text(index))
ButtonLoc = self.ButtonLocation(option)
button.setGeometry(ButtonLoc)
Cellvalue = index.model().data(index, QtCore.Qt.EditRole)
row = index.row()
AssetId = index.model().data(index.model().index(row, 0)).toString()
AnimTurntablePath = shotsGetData.getAssetTurntablePath(AssetId)
browsePath = shotsGetData.getAssetPath(AssetId)
#toAvidComp = shotsGetData.gettoAvidComp(ShotId)
# Connect to button
button.clicked.connect(lambda: self.mousePressEvent(index, browsePath, AssetTurntablePath))
return button
def setEditorData(self, editor, index):
button = editor
if not button:
return
def setModelData(self, editor, model, index):
button = editor
if not button:
return
def updateEditorGeometry(self, editor, option, index):
ButtonLoc = self.ButtonLocation(option)
editor.setGeometry(ButtonLoc)
def paint(self, painter, option, index):
opt = QtGui.QStyleOptionButton()
#opt.icon = self.icon()
opt.text = self.text(index)
opt.rect = option.rect
opt.palette = option.palette
opt.rect = self.ButtonLocation(opt)
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_PushButton, opt, painter)
def ButtonLocation(self, option):
r = option.rect
x = r.left() + 10
y = r.top() + 10
w = 30;
h = 30
return QRect(x,y,w,h);
def text(self, index):
#print self.column
column = index.column()
if column == 7:
return QtCore.QString("Mov")
def mousePressEvent(self, index, browsePath , AssetTurntablePath):
column = index.column()
print "PRESSSED"
if column == 7:
subprocess.Popen(AnimTurntablePath, shell=True)