我希望有人可以帮助我解决我一直在努力解决的这个烦人的问题。我已经使用附加的代码在委托列的表格视图中插入按钮进行了管理。
问题是要按下按钮,我需要双击“激活”包含单元格。一旦单元格处于活动状态,我就可以按下按钮,所以我总共需要 3 次点击才能按下它。这可能会让您的普通用户感到困惑。
我将这个问题发布到 pyqt 邮件列表,得到的答案是:
“发生的情况是,当 TableWidget 收到点击时,它会创建一个编辑器,但编辑器还没有收到点击。这在大多数情况下是完美的,但如果你绘制一个按钮,它就不是了。”
有人来过这里吗?
在此先感谢,克里斯
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)