我想在模型中对齐我的所有行。我在 "QtDesigner "中编译.ui到.py,并创建form.py.Ui_MainWindow来自form.py。Ui_MainWindow来自form.py。你能用例子解释一下解决方法吗?
回复我想在模型中对齐我的所有行。我在 "QtDesigner "中把.ui编译成.py,并创建form.py.Ui_MainWindow。Ui_MainWindow来自form.py。你能举例说明解决方法吗?
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...
这是我的QtTableView模型。
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. : 对不起,我的英语:-)
没有得到你的方式来创建一个表的QTableView,但对于每个项目设置这段代码的工作,以对齐。
data = QTableWidget(["17776661122"])
table.setItem(x,y,data).setTextAlignment(Qt.AlignHCenter)
我找到了解决方案。我添加了这一行...
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
###