简体   繁体   中英

how to align qtableview row at the right side in pyqt4?

How to align the content of the qtable view in the right? I use this class in making my 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()"))

I would like to align all the currency data type in the right. Thanks:)

Take a look to the Qt::ItemDataRole enum (especially the TextAlignmentRole ).

You can do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM