Search code examples
pythonpyqtpyqt6

Can I combine multiple QPdfSearchModels into a single one to connect with my QListView?


I would like to employ the QPdfSearchModel to populate a QListView with all results of a search in a pdf. The issue is that the string I am looking for can be with or without spaces. E.g. when I am searching for "ABC DEF", the QListView should show all results for "ABC DEF" and for "ABCDEF".

In a QPdfSearchModel, you can only set a single search term. Therefore, I would like to extend this functionality to include more than one search term.

I have made a new class "CombinedListModel" to combine results of multiple QPDFSearchmodels. This works partially, as I am able to see all hits of all the search strings in the QListView. However, the link with the PdfViewer is gone, since clicking on an item in the QListView does not do anything.

import sys
from PySide6.QtCore import Qt, QModelIndex
from PySide6.QtWidgets import QApplication, QMainWindow, QListView, QVBoxLayout, QWidget, QLineEdit
from PySide6.QtPdf import QPdfDocument, QPdfSearchModel
from PySide6.QtPdfWidgets import QPdfView
from PySide6 import QtGui  

class CombinedListModel(QPdfSearchModel):
    def __init__(self, models, parent=None):
        super(CombinedListModel, self).__init__(parent)
        self.models = models

    def rowCount(self, parent=QModelIndex()):
        return sum(model.rowCount(QModelIndex()) for model in self.models)

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return None

        current_row = index.row()
        for model in self.models:
            if current_row < model.rowCount(QModelIndex()):
                return model.data(model.index(current_row), role)
            current_row -= model.rowCount(QModelIndex())

        return None


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PDF Search Viewer")

        self.search_bar = QLineEdit(self)
        self.search_bar.textChanged.connect(self.on_search_text_changed)
        self.list_view = QListView(self)
        self.list_view.clicked.connect(self.on_list_view_clicked)

        layout = QVBoxLayout()
        layout.addWidget(self.search_bar)
        layout.addWidget(self.list_view)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.pdf_document = QPdfDocument(self)
        self.pdf_document.load(r"C:\temp\OCR_highlighted.pdf")  # Path to your PDF file


        self.pdf_viewer = QPdfView(self)
        self.pdf_viewer.setDocument(self.pdf_document)
        self.pdf_viewer.setPageMode(QPdfView.PageMode.MultiPage)
        layout.addWidget(self.pdf_viewer)

        self.search_bar.setText("ABCDEF;ABC DEF")

    def on_search_text_changed(self, text):
        # create multiple search models
        search_terms = text.split(';')  # Split the search terms by semicolon
        self.search_models = [QPdfSearchModel(self) for _ in search_terms]
        for model, term in zip(self.search_models, search_terms):
            model.setDocument(self.pdf_document)
            model.setSearchString(term)
            print(f"Found: {len(sum([model.resultsOnPage(i) for i in range(self.pdf_document.pageCount())],[]))} times")

        combined_model = CombinedListModel(self.search_models)

        if False:
            self.list_view.setModel(self.search_models[(0)])
            self.pdf_viewer.setSearchModel(self.search_models[0])
        else:
            self.list_view.setModel(combined_model)
            self.pdf_viewer.setSearchModel(combined_model) # --> This does not work properly (links are gone)


    def on_list_view_clicked(self, index):
        self.pdf_viewer.setCurrentSearchResultIndex(index.row()) # --> This does not work properly with combined model (links are gone)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Solution

  • The QPdfView class uses the resultAtIndex() and resultsOnPage() methods of the QPdfSearchModel class, but these methods cannot be overridden currently, as commented by Musicamante. (The view calls them when rendering a page.) So, it's impossible to render search results(QPdfLink objects) from multiple models at the same time.

    Your best bet is, when a search result was clicked, changing the search model to the correspondent one. (Call the setSearchModel() followed by the setCurrentSearchResultIndex().)