2

qtable视图的内容如何在右侧对齐?我用这个类来制作我的桌子。

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()"))

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

4

1 回答 1

4

看看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() 
于 2011-08-12T07:56:31.700 回答