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

3

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

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật