我有一个QWidget,其中包含一个QTableView ..按下Ctrl + F后,查找对话框被加载。 我想在第一列中选择包含在查找对话框中输入的文本的行(行)。
我有这个代码,但问题是它没有选择行,请指导我是否有一个很好的方法来查找匹配并选择表中的相应行。问题是以下代码无法找到匹配项并选择它们。
class Widget(QWidget):
def __init__(self,md,parent=None):
QWidget.__init__(self,parent)
layout=QVBoxLayout(self)
# initially construct the visible table
self.table = QTableView()
self.table.horizontalHeader().setStretchLastSection(True) # uncomment this if the last column shall cover the rest
self.table.show()
# set black grid lines
self.setStyleSheet("gridline-color: rgb(39, 42, 49)")
# construct the Qt model belonging to the visible table
model = NvmQtModel(md)
self.table.setModel(model)
self.table.resizeRowsToContents()
self.table.resizeColumnsToContents()
# set the shortcut ctrl+F for find in menu
shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
shortcut.activated.connect(self.handleFind)
# shows and handles the find dialog
def handleFind(self):
findDialog = QDialog()
findDialog.setWindowTitle("Find")
grid = QGridLayout()
findDialog.setLayout(grid)
findLabel = QLabel("Find what", findDialog)
grid.addWidget(findLabel,1,0)
findField = QLineEdit(findDialog)
grid.addWidget(findField,1,1)
findButton = QPushButton("Find", findDialog)
grid.addWidget(findButton,2,1)
findButton.clicked.connect(
lambda: self.find(findField.text()))
print("Hereee1")
findDialog.exec_()
# find function: search in the first column of the table
def find(self, text, column=0):
print("Hereee2")
model = self.table.model()
start = model.index(0, column)
matches = model.match(
start, Qt.DisplayRole,
text, 1, Qt.MatchContains)
print(text)
if matches:
index = matches[0]
# index.row(), index.column()
self.table.selectionModel().select(
index, QtGui.QItemSelectionModel.Select)
print("Hereee3")