qtableview 查询_python – QTableView中的搜索/查找功能
我有一个QWidget,里面有一个QTableView.我需要在表的第一列有一个查找功能,所以当我点击Ctrl F时,会弹出一个查找对话框.class Widget(QWidget):def __init__(self,md,parent=None):QWidget.__init__(self,parent)layout=QVBoxLayout(self)# initially construct
我有一个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
tv = QTableView()
# uncomment this if the last column shall cover the rest
tv.horizontalHeader().setStretchLastSection(True)
tv.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)
tv.setModel(model)
tv.resizeRowsToContents()
tv.resizeColumnsToContents()
# set the shortcut ctrl+F for find in menu
shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
shortcut.activated.connect(self.handleFind)
# delegate for decimal
delegate = NvmDelegate()
tv.setItemDelegate(delegate)
self.setGeometry(200,200,600,600) # adjust this later
layout.addWidget(tv)
# set window title
self.setWindowTitle("TITLE")
# shows and handles the find dialog
def handleFind(self):
findDialog = QDialog()
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)
findButton.clicked.connect(self.find)
grid.addWidget(findButton,2,1)
findDialog.exec_()
# find function: search in the first column of the table
def find(self):
#to do
# prevent closing the window without confirmation
def closeEvent(self, event):
reply=QMessageBox.question(self,'Message',"Are you sure to quit?",QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
if reply==QMessageBox.Yes:
event.accept()
else:
event.ignore()
# create the application and the new tree view container
app=QApplication(sys.argv)
wid=Widget(md)
wid.show()
wid.raise_()
我在findButton操作中遇到问题,它应该在表的第一列中搜索.如果你在这个问题上指导我,我将不胜感激.

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)