【问题标题】:Search/Find functionality in QTableViewQTableView 中的搜索/查找功能
【发布时间】:2014-01-08 17:11:45
【问题描述】:

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

【问题讨论】:

    标签: python qt search pyqt qtableview


    【解决方案1】:

    首先,您需要更改 findButton 的连接方式,以便它发送要搜索的文本:

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

    然后你可以使用tableview的模型的match方法在你的表中搜索:

    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()
    

    【讨论】:

    • tnx, AttributeError: 'Widget' object has no attribute 'table' in line : "model = self.table.model()"
    • @OrclUser。是的,对不起,我错过了。您需要将Widget.__init__ 中的tv 更改为self.table。
    • 现在应该打印索引但我在控制台中看不到打印结果..以及如何显示在表格中找到相应文本的位置?例如通过突出显示行或指向它?这样做的常见方法是什么?
    • Tnx 进行更新..当它想要选择与索引对应的行时,我在最后一步仍然有问题..“如果匹配”永远不会执行!
    • @And3r50n1。当然,是的。只需在 match 方法中使用 -1 即可获取所有匹配项,然后对其进行迭代。您可能还需要更改表格的selection mode 以允许多选。
    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多