我有这段代码:
class ManifestModel(QtSql.QSqlTableModel):
def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
super(ManifestModel, self).__init__(parent, db)
def flags(self, index):
if (index.column() == 4):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 6):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 3):
return QtCore.Qt.AlignHCenter
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
当我运行它时出现错误:
builtins.TypeError: invalid result from ManifestModel.flags(), AlignmentFlag cannot be converted to PyQt5.QtCore.ItemFlags in this context
在使用ManifestModel
的同一例程中,我有代码:
ui.label = QtWidgets.QLabel(ui.page)
ui.label.setGeometry(QtCore.QRect(308, 0, 131, 20))
ui.label.setAlignment(QtCore.Qt.AlignCenter)
那么我需要做些什么才能更改QTableView列中的对齐方式?
答案 0 :(得分:1)
对象在模型的data()
方法中处理。所以尝试类似的事情:
class ManifestModel(QtSql.QSqlTableModel):
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.TextAlignmentRole and index.column() == 3:
return QtCore.Qt.AlignHCenter
return super(ManifestModel, self).data(index, role)