Search code examples
pythonpyqt5qtableviewqabstracttablemodel

How can i align (center) row data in Python PyQt5 QTableView?


I want to align my all rows in model. I compile .ui to .py in 'QtDesigner' and creates form.py. Ui_MainWindow comes from form.py . Can you explain the solve with examples?

Re:I want to align my all rows in model. I compile .ui to .py in 'QtDesigner' and creates form.py. Ui_MainWindow comes from form.py . Can you explain the solve with examples?

class App(Ui_MainWindow):
    def __init__(self, window):
        self.setupUi(window)
        # code...
        numbers = [["Thomas","17776661122",❌],["Parker","1777666331",❌],["Michael","17775553322",❌]]
        CreateTable()
    def CreateTable(self,fromlist):
        for i in range(0,len(fromlist)):
            self.model.addCustomer(Customer(fromlist[i][0],fromlist[i][1],fromlist[i][2]))
            ### align code
        # code...

This is my QtTableView Model.

class Customer(object):
    def __init__(self,name,number,status):
        self.name = name
        self.number = number
        self.status = status

class CustomerTableModel(QtCore.QAbstractTableModel):

    ROW_BATCH_COUNT = 15

    def __init__(self):
        super(CustomerTableModel,self).__init__()
        self.headers = ['               İsim                     ','  Telefon No (Örn 9053xx..)   ','   Mesaj Durumu   ']
        self.customers  = []
        self.rowsLoaded = CustomerTableModel.ROW_BATCH_COUNT

    def rowCount(self,index=QtCore.QModelIndex()):
        if not self.customers:
            return 0

        if len(self.customers) <= self.rowsLoaded:
            return len(self.customers)
        else:
            return self.rowsLoaded

    def canFetchMore(self,index=QtCore.QModelIndex()):
        if len(self.customers) > self.rowsLoaded:
            return True
        else:
            return False

    def fetchMore(self,index=QtCore.QModelIndex()):
        reminder = len(self.customers) - self.rowsLoaded
        itemsToFetch = min(reminder,CustomerTableModel.ROW_BATCH_COUNT)
        self.beginInsertRows(QtCore.QModelIndex(),self.rowsLoaded,self.rowsLoaded+itemsToFetch-1)
        self.rowsLoaded += itemsToFetch
        self.endInsertRows() 

    def addCustomer(self,customer):
        self.beginResetModel()
        self.customers.append(customer)
        self.endResetModel()

    def columnCount(self,index=QtCore.QModelIndex()):
        return len(self.headers)

    def data(self,index,role=QtCore.Qt.DisplayRole):
        col = index.column()
        customer = self.customers[index.row()]
        if role == QtCore.Qt.DisplayRole:
            if col == 0:
                return QtCore.QVariant(customer.name)
            elif col == 1:
                return QtCore.QVariant(customer.number)
            elif col == 2:
                return QtCore.QVariant(customer.status)
            return QtCore.QVariant()

    def headerData(self,section,orientation,role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if orientation == QtCore.Qt.Horizontal:
            return QtCore.QVariant(self.headers[section])
        return QtCore.QVariant(int(section + 1))

P.S. : Sorry for my English :-)


Solution

  • I found the solution. I added the this line to ...

    def data(self,index,role=QtCore.Qt.DisplayRole):
        col = index.column()
        customer = self.customers[index.row()]
        if role == QtCore.Qt.DisplayRole:
            if col == 0:
                return QtCore.QVariant(customer.name)
            elif col == 1:
                return QtCore.QVariant(customer.number)
            elif col == 2:
                return QtCore.QVariant(customer.status)
            return QtCore.QVariant()
        # ADDED LINES
        elif role == QtCore.Qt.TextAlignmentRole:
            return QtCore.Qt.AlignCenter
        ###