如何滚动到 QTableView 中的特定行?
How can i scroll to specific row in QTableView?
我想滚动 table 中的特定行。我的 QTableView 模型代码如下。
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 = 1000
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 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()
elif role == QtCore.Qt.TextAlignmentRole:
return QtCore.Qt.AlignCenter
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))
我找到了解决这个问题的函数,但它适用于 QTableWidget。我找到的代码如下。
self.QtTableWidget.scrollToItem(item)
我无法将此功能用于 QTableView。我该怎么做?还有其他功能吗?
一般来说,对于所有继承自 QAbstractItemView(如 QTableView、QTableWidget、QListView 等)的 类,有 scrollTo
method that is used internally to scrollToItem
:
row = 4
column = 0
index = table_view.model().index(row, column)
table_view.scrollTo(index)
我想滚动 table 中的特定行。我的 QTableView 模型代码如下。
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 = 1000
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 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()
elif role == QtCore.Qt.TextAlignmentRole:
return QtCore.Qt.AlignCenter
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))
我找到了解决这个问题的函数,但它适用于 QTableWidget。我找到的代码如下。
self.QtTableWidget.scrollToItem(item)
我无法将此功能用于 QTableView。我该怎么做?还有其他功能吗?
一般来说,对于所有继承自 QAbstractItemView(如 QTableView、QTableWidget、QListView 等)的 类,有 scrollTo
method that is used internally to scrollToItem
:
row = 4
column = 0
index = table_view.model().index(row, column)
table_view.scrollTo(index)