我有一個遇到問題的應用程式。
為了解釋這個問題,我已經將它簡化到最大程度,以便能夠在這里公開它。
我的問題如下:
我有一個
QTableView, 關聯到一個QSortFilterProxyModel.我在表中進行搜索以查找包含搜索文本的記錄,一旦找到,我選擇該行。
如果
QSortFilterProxyModel未排序,則它可以正常作業(選項 1)。

- 如果
QSortFilterProxyModel已排序不起作用(選項 2)。

- 我試圖轉換模型給我的行,
maptoSource但這是不可能的(選項 2)。
錯誤是:
QSortFilterProxyModel:來自錯誤模型的索引傳遞給 mapToSource

我很清楚模型行必須轉換為 QSortFilterProxyModel 行,但我不知道如何。
有任何想法嗎?
非常感謝你。
示例代碼:
from PyQt5.QtWidgets import (QApplication, QPushButton,QComboBox,QMainWindow)
from PyQt5.QtWidgets import (QTableView,QAbstractItemView,QVBoxLayout,QLineEdit,QWidget)
from PyQt5.QtGui import QStandardItemModel,QStandardItem
from PyQt5.QtCore import QSortFilterProxyModel,Qt
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setup_gui()
def setup_gui(self):
widget=QWidget(self)
self.setCentralWidget(widget)
self.qlineedit = QLineEdit()
self.qlineedit.textChanged.connect(self.find_text)
self.table_view = QTableView()
self.main_layout = QVBoxLayout(widget)
self.main_layout.addWidget(self.qlineedit)
self.main_layout.addWidget(self.table_view)
self.table_model = QStandardItemModel()
row=0
for b in ['Fast','Medium','Cool','Double']:
item=QStandardItem(b)
self.table_model.setItem(row, 0, item)
row =1
self.model_filter_proxy = QSortFilterProxyModel()
self.model_filter_proxy.setSourceModel(self.table_model)
self.model_filter_proxy.sort(0)
self.table_view.setModel(self.model_filter_proxy)
# Setup Table view
# Selection one row at same time
self.table_view.setSelectionMode(QAbstractItemView.SingleSelection)
# Seleccionar toda la fila
self.table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table_view.setStyleSheet("QTableView::item:selected{ background-color: green ; selection-color: white; }")
def find_text(self,text):
column=0
self.table_view.clearSelection()
start = self.table_model.index(0, column)
matches = self.table_model.match(
start,
Qt.DisplayRole,
text,
hits=1,
flags=Qt.MatchContains
)
if matches:
# Option 1
index = matches[0]
row=index.row()
# Option 2
mapped_index = self.model_filter_proxy.mapToSource(matches[0])
row=mapped_index.row()
self.table_view.selectRow(row)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyle("fusion")
w=MainWindow()
w.show()
sys.exit(app.exec_())
uj5u.com熱心網友回復:
使用@musicamante 評論,正確的代碼是:
from PyQt5.uic import loadUiType, loadUi
from PyQt5.QtWidgets import (QApplication, QPushButton,QComboBox,QMainWindow)
from PyQt5.QtWidgets import (QTableView,QAbstractItemView,QVBoxLayout,QLineEdit,QWidget)
from PyQt5.QtGui import QStandardItemModel,QStandardItem
from PyQt5.QtCore import QSortFilterProxyModel,Qt
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setup_gui()
def setup_gui(self):
widget=QWidget(self)
self.setCentralWidget(widget)
self.qlineedit = QLineEdit()
self.qlineedit.textChanged.connect(self.find_text)
self.table_view = QTableView()
self.main_layout = QVBoxLayout(widget)
self.main_layout.addWidget(self.qlineedit)
self.main_layout.addWidget(self.table_view)
self.table_model = QStandardItemModel()
row=0
for b in ['Fast','Medium','Cool','Double']:
item=QStandardItem(b)
self.table_model.setItem(row, 0, item)
row =1
self.model_filter_proxy = QSortFilterProxyModel()
self.model_filter_proxy.setSourceModel(self.table_model)
self.model_filter_proxy.sort(0)
self.table_view.setModel(self.model_filter_proxy)
# Setup Table view
# Selection one row at same time
self.table_view.setSelectionMode(QAbstractItemView.SingleSelection)
# Seleccionar toda la fila
self.table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table_view.setStyleSheet("QTableView::item:selected{ background-color: green ; selection-color: white; }")
def find_text(self,text):
column=0
self.table_view.clearSelection()
start = self.table_model.index(0, column)
matches = self.table_model.match(
start,
Qt.DisplayRole,
text,
hits=1,
flags=Qt.MatchContains
)
if matches:
# Option 1
#index = matches[0]
#row=index.row()
# Option 2
#mapped_index = self.model_filter_proxy.mapToSource(matches[0])
mapped_index = self.model_filter_proxy.mapFromSource(matches[0])
row=mapped_index.row()
self.table_view.selectRow(row)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyle("fusion")
w=MainWindow()
w.show()
sys.exit(app.exec_())
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/476830.html
標籤:qt pyqt qtableview qsortfilterproxymodel
上一篇:在PyQt中關閉QMessageBox后對話框的位置發生了變化
下一篇:防止計時器多次更新計數器變數
