gpt4 book ai didi

python - 如何在 pyqt4 的右侧对齐 qtableview 行?

转载 作者:行者123 更新时间:2023-11-28 23:04:49 24 4
gpt4 key购买 nike

如何让qtable View 的内容右对齐?我用这个类来制作我的 table 。

class MyTableModel(QAbstractTableModel):

def __init__(self, datain, headerdata, parent=None, *args):
""" datain: a list of lists
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata


def rowCount(self, parent):
return len(self.arraydata)

def columnCount(self, parent):
try:
return len(self.arraydata[0])
except:
return 0
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])

def headerData(self, col, orientation, role):

if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headerdata[col])
return QVariant()

def sort(self, Ncol, order):
"""Sort table by given column number.
"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.emit(SIGNAL("layoutChanged()"))

我想将所有货币数据类型右对齐。谢谢:)

最佳答案

查看 Qt::ItemDataRole enum (尤其是 TextAlignmentRole)。

你可以这样做:

def data(self, index, role): 
if index.isValid():
return QVariant()

if role == Qt.DisplayRole:
return QVariant(self.arraydata[index.row()][index.column()])
elif role == Qt.TextAlignmentRole:
return QVariant(Qt.AlignRight | Qt.AlignVCenter)

return QVariant()

关于python - 如何在 pyqt4 的右侧对齐 qtableview 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com