ฉันพยายามทำงานนี้มาหลายวันแล้ว แต่ก็ไม่มีประโยชน์ หวังว่าพวกคุณจะช่วยฉันได้
ฉันมี QSqlTableModel
แบบกำหนดเองที่มีสิ่งนี้เนื่องจากเป็นฟังก์ชันข้อมูล:
def data(self, index, role):
#Formats Cells according to data in them
if role == QtCore.Qt.TextAlignmentRole:
if index.column() == 2:
text = QtSql.QSqlTableModel.data(self, self.index(index.row(), 2), QtCore.Qt.DisplayRole)
if text.split():
if not re.search(r'start|Z[NARXTFEISL]|gv', text.split()[0], re.I):
return QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop
else: return QtCore.Qt.AlignLeft
else: pass
else: return QtCore.Qt.AlignLeft
if role == QtCore.Qt.BackgroundRole:
if 'MILESTONE' in QtSql.QSqlTableModel.data(self, self.index(index.row(), 2), QtCore.Qt.DisplayRole):
return QtGui.QBrush(QtCore.Qt.yellow)
return QtSql.QSqlTableModel.data(self, index, role)
ไม่มีสิ่งใดที่เกี่ยวข้องกับคำถามจริงๆ ยกเว้นบล็อก BackgroundRole
สิ่งที่ฉันพยายามทำคือเมื่อฉันคลิกขวาที่ tableview
เมนูบริบทจะปรากฏขึ้น:
menu = QMenu()
stepx = menu.addAction('Mark Step as Executed')
stepdx = menu.addAction('Clear Step Execution')
action = menu.exec_(self.tableView.mapToGlobal(pos))
index = self.model.index(self.tableView.rowAt(pos.y()), 2)
if action == stepx:
logging.info(' Executed Step: ' + str(self.tableView.rowAt(pos.y()) + 1))
self.model.setData(index, QtCore.QVariant(QtGui.QBrush(QtCore.Qt.red)), QtCore.Qt.BackgroundRole)
ทั้งหมดที่ฉันพยายามทำคือเมื่อฉันคลิก "Mark Step as Executed"
บนเมนูตามบริบท ฉันต้องการให้แถว (หรือแม้แต่เซลล์ ตอนนี้ฉันจะทำอะไรก็ได้) เปลี่ยนเป็นสีแดง
สิ่งที่ฉันทำอยู่ไม่ได้ผลและฉันหวังว่าจะมีคนสามารถช่วยฉันได้