QTableView中的搜索/查找功能

 用户wuhqqnrd0m 发布于 2023-02-09 09:15

我有一个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操作中遇到问题,它应该在表的第一列中搜索.如果你在这个问题上指导我,我将不胜感激.

1 个回答
  • 首先,您需要更改find​​Button的连接方式,以便它发送要搜索的文本:

    findButton.clicked.connect(
        lambda: self.find(findField.text()))
    

    然后,您可以使用tableview模型的匹配方法在表中搜索:

    def find(self, text, column=0):
        model = self.table.model()
        start = model.index(0, column)
        matches = model.match(
            start, QtCore.Qt.DisplayRole,
            text, 1, QtCore.Qt.MatchContains)
        if matches:
            index = matches[0]
            # index.row(), index.column()
            self.table.selectionModel().select(
                index, QtGui.QItemSelectionModel.Select)
    

    更新:

    上面的方法将找到包含给定文本的第一个单元格,然后选择它.如果要查找匹配的下一个单元格,则start需要将其设置为当前选择的相应索引(如果有).这可以通过以下方式获得:

        indexes = self.table.selectionModel().selectedIndexes()
    

    2023-02-09 09:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有