2011-08-12 48 views
2

如何在右側對齊qtable視圖的內容?我使用這個班級製作我的桌子。如何在pyqt4的右側對齊qtableview行?

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

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