将PyQt5与QTableView,QStandardItemModel和QSortFilterProxy一起使用。在模型上执行insertRow之后,排序代理以正确的排序顺序在视图表中显示数据。但是,我需要在刚刚插入的记录上发出view.selectRow。如果ItemModel以未排序的顺序存储数据,则不确定如何执行此操作。
类似但简短的示例:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QWidget, QInputDialog
class Ui_Dialog(QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(282, 247)
self.tableView = QtWidgets.QTableView(Dialog)
self.tableView.setGeometry(QtCore.QRect(10, 10, 256, 192))
self.tableView.setObjectName("tableView")
self.tableModel = QStandardItemModel()
self.tableModel.setColumnCount(1)
self.tableModel.setHeaderData(0, Qt.Horizontal, "Category Name")
self.tableProxy = QSortFilterProxyModel()
self.tableProxy.setSourceModel(self.tableModel)
self.tableView.setModel(self.tableProxy)
self.tableProxy.setSortCaseSensitivity(Qt.CaseInsensitive)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(90, 210, 87, 29))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.setup_connections()
self.load_data()
self.tableProxy.sort(0)
self.tableView.selectRow(0)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "New"))
def setup_connections(self):
self.pushButton.clicked.connect(self.add_record)
def load_data(self):
data = ('Alpha', 'Charlie', 'Foxtrot', 'Zulu', 'Bravo', 'Tango')
for ndx, val in enumerate(data):
print(ndx, val)
item = QStandardItem(val)
self.tableModel.insertRow(ndx, item)
def add_record(self):
i, ok = QInputDialog.getText(self, "Value Editor", "Emter A Value:")
if ok:
item = QStandardItem(i)
self.tableModel.insertRow(self.tableModel.rowCount(), item)
### NEED TO TRIGGER TABLE TO SELECT THE ROW JUST INSERTED ###
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
要选择一项,必须使用视图的selectionModel()
,您必须传递传递给视图的模型的索引,即代理和选择类型的标志。要获取代理的索引,您必须首先获取sourceModel的索引,为此,您可以使用indexFromItem()
将QStandardItem
转换为QModelIndex
,然后使用mapFromSource()
进行转换从源到代理:
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.tableView = QtWidgets.QTableView()
self.tableModel = QtGui.QStandardItemModel()
self.tableModel.setColumnCount(1)
self.tableModel.setHeaderData(0, QtCore.Qt.Horizontal, "Category Name")
self.tableProxy = QtCore.QSortFilterProxyModel()
self.tableProxy.setSourceModel(self.tableModel)
self.tableView.setModel(self.tableProxy)
self.tableProxy.setSortCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pushButton = QtWidgets.QPushButton("New")
self.setup_connections()
self.load_data()
self.tableProxy.sort(0)
self.tableView.selectRow(0)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.tableView)
lay.addWidget(self.pushButton)
def setup_connections(self):
self.pushButton.clicked.connect(self.add_record)
def load_data(self):
data = ('Alpha', 'Charlie', 'Foxtrot', 'Zulu', 'Bravo', 'Tango')
for ndx, val in enumerate(data):
item = QtGui.QStandardItem(val)
self.tableModel.insertRow(ndx, item)
@QtCore.pyqtSlot()
def add_record(self):
i, ok = QtWidgets.QInputDialog.getText(self, "Value Editor", "Emter A Value:")
if ok:
item = QtGui.QStandardItem(i)
self.tableModel.insertRow(self.tableModel.rowCount(), item)
ix = self.tableModel.indexFromItem(item)
ix_proxy = self.tableProxy.mapFromSource(ix)
self.tableView.selectionModel().select(ix_proxy, QtCore.QItemSelectionModel.ClearAndSelect)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())