如何在 Python PyQt5 QTableView 中对齐(居中)行数据?
How can i align (center) row data in Python PyQt5 QTableView?
我想对齐模型中的所有行。我在 'QtDesigner' 中将 .ui 编译为 .py 并创建 form.py。 Ui_MainWindow 来自 form.py 。你能用例子解释一下吗?
回复:我想对齐模型中的所有行。我在 'QtDesigner' 中将 .ui 编译为 .py 并创建 form.py。 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 上创建 table 的方法,但是对于每个项目设置,此代码都可以对齐;
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
###
我想对齐模型中的所有行。我在 'QtDesigner' 中将 .ui 编译为 .py 并创建 form.py。 Ui_MainWindow 来自 form.py 。你能用例子解释一下吗?
回复:我想对齐模型中的所有行。我在 'QtDesigner' 中将 .ui 编译为 .py 并创建 form.py。 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 上创建 table 的方法,但是对于每个项目设置,此代码都可以对齐;
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
###