When I do a pdf search using the QPdfsearchmodel class my model only seems to include the word I searched for and context before/context after:
However, in the documentation there is also a listing for the page no.:
I modified the Pyside6 port of the pdfwidgets/pdfviewer example from Qt v6.x by adding a text search bar to the top and replacing the bookmark view pane on the left with a Qlistview for the search results:
https://doc.qt.io/qtforpython-6/examples/example_pdfwidgets_pdfviewer.html
I have tried everything I can think of. I only modified mainwindow.py and ui_mainwindow.py below:
Copyright (C) 2022 The Qt Company Ltd.
SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import math
import sys
from PySide6.QtPdf import QPdfBookmarkModel, QPdfDocument,QPdfSearchModel
from PySide6.QtPdfWidgets import QPdfView
from PySide6.QtWidgets import (QDialog, QFileDialog, QMainWindow, QMessageBox,
QSpinBox,QLineEdit)
from PySide6.QtCore import QModelIndex, QPoint, QStandardPaths, QUrl, Slot
from zoomselector import ZoomSelector
from ui_mainwindow import Ui_MainWindow
ZOOM_MULTIPLIER = math.sqrt(2.0)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.m_zoomSelector = ZoomSelector(self)
self.m_pageSelector = QSpinBox(self)
self.m_document = QPdfDocument(self)
self.m_fileDialog = None
self.m_search = QLineEdit(self)
self.m_searchModel=QPdfSearchModel(self)
self.ui.setupUi(self)
self.m_zoomSelector.setMaximumWidth(150)
self.ui.mainToolBar.insertWidget(self.ui.actionZoom_In, self.m_zoomSelector)
self.ui.mainToolBar.insertWidget(self.ui.actionForward, self.m_pageSelector)
self.m_pageSelector.valueChanged.connect(self.page_selected)
#Add SearchBar
self.ui.mainToolBar.addWidget(self.m_search)
nav = self.ui.pdfView.pageNavigator()
nav.currentPageChanged.connect(self.m_pageSelector.setValue)
nav.backAvailableChanged.connect(self.ui.actionBack.setEnabled)
nav.forwardAvailableChanged.connect(self.ui.actionForward.setEnabled)
#Add model to searchView
self.ui.searchView.setModel(self.m_searchModel)
#Set SeletctionModel
self.selModel=self.ui.searchView.selectionModel()
self.m_zoomSelector.zoom_mode_changed.connect(self.ui.pdfView.setZoomMode)
self.m_zoomSelector.zoom_factor_changed.connect(self.ui.pdfView.setZoomFactor)
self.m_zoomSelector.reset()
self.m_search.textChanged.connect(self.searchChange)
self.selModel.selectionChanged.connect(self.selChange)
self.ui.pdfView.setDocument(self.m_document)
self.ui.pdfView.zoomFactorChanged.connect(self.m_zoomSelector.set_zoom_factor)
# #add wordwrap
self.ui.searchView.setWordWrap(True)
def searchChange(self):
#Set Search String
self.m_searchModel.setSearchString(self.m_search.text())
print(self.m_searchModel.searchString())
#Set Document for Search
self.m_searchModel.setDocument(self.m_document)
print('==============')
def selChange(self,index):
print('click')
item=self.selModel.selection().indexes()[0]
print(item.data())
@Slot(QUrl)
def open(self, doc_location):
if doc_location.isLocalFile():
self.m_document.load(doc_location.toLocalFile())
document_title = self.m_document.metaData(QPdfDocument.MetaDataField.Title)
self.setWindowTitle(document_title if document_title else "PDF Viewer")
self.page_selected(0)
self.m_pageSelector.setMaximum(self.m_document.pageCount() - 1)
else:
message = f"{doc_location} is not a valid local file"
print(message, file=sys.stderr)
QMessageBox.critical(self, "Failed to open", message)
@Slot(QModelIndex)
def bookmark_selected(self, index):
if not index.isValid():
return
page = index.data(int(QPdfBookmarkModel.Role.Page))
zoom_level = index.data(int(QPdfBookmarkModel.Role.Level))
self.ui.pdfView.pageNavigator().jump(page, QPoint(), zoom_level)
@Slot(int)
def page_selected(self, page):
nav = self.ui.pdfView.pageNavigator()
nav.jump(page, QPoint(), nav.currentZoom())
@Slot()
def on_actionOpen_triggered(self):
if not self.m_fileDialog:
directory = QStandardPaths.writableLocation(QStandardPaths.DocumentsLocation)
self.m_fileDialog = QFileDialog(self, "Choose a PDF", directory)
self.m_fileDialog.setAcceptMode(QFileDialog.AcceptOpen)
self.m_fileDialog.setMimeTypeFilters(["application/pdf"])
if self.m_fileDialog.exec() == QDialog.Accepted:
to_open = self.m_fileDialog.selectedUrls()[0]
if to_open.isValid():
self.open(to_open)
@Slot()
def on_actionQuit_triggered(self):
self.close()
@Slot()
def on_actionAbout_triggered(self):
QMessageBox.about(self, "About PdfViewer",
"An example using QPdfDocument")
@Slot()
def on_actionAbout_Qt_triggered(self):
QMessageBox.aboutQt(self)
@Slot()
def on_actionZoom_In_triggered(self):
factor = self.ui.pdfView.zoomFactor() * ZOOM_MULTIPLIER
self.ui.pdfView.setZoomFactor(factor)
@Slot()
def on_actionZoom_Out_triggered(self):
factor = self.ui.pdfView.zoomFactor() / ZOOM_MULTIPLIER
self.ui.pdfView.setZoomFactor(factor)
@Slot()
def on_actionPrevious_Page_triggered(self):
nav = self.ui.pdfView.pageNavigator()
nav.jump(nav.currentPage() - 1, QPoint(), nav.currentZoom())
@Slot()
def on_actionNext_Page_triggered(self):
nav = self.ui.pdfView.pageNavigator()
nav.jump(nav.currentPage() + 1, QPoint(), nav.currentZoom())
@Slot()
def on_actionContinuous_triggered(self):
cont_checked = self.ui.actionContinuous.isChecked()
mode = QPdfView.PageMode.MultiPage if cont_checked else QPdfView.PageMode.SinglePage
self.ui.pdfView.setPageMode(mode)
@Slot()
def on_actionBack_triggered(self):
self.ui.pdfView.pageNavigator().back()
@Slot()
def on_actionForward_triggered(self):
self.ui.pdfView.pageNavigator().forward()
And ui_mainwindow.py:
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QAction, QBrush, QColor, QConicalGradient,
QCursor, QFont, QFontDatabase, QGradient,
QIcon, QImage, QKeySequence, QLinearGradient,
QPainter, QPalette, QPixmap, QRadialGradient,
QTransform)
from PySide6.QtPdfWidgets import QPdfView
from PySide6.QtWidgets import (QApplication, QHeaderView, QMainWindow, QMenu,
QMenuBar, QSizePolicy, QSplitter, QStatusBar,
QTabWidget, QToolBar, QTreeView, QVBoxLayout,
QWidget,QListView)
import resources_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(700, 600)
MainWindow.setUnifiedTitleAndToolBarOnMac(True)
self.actionOpen = QAction(MainWindow)
self.actionOpen.setObjectName(u"actionOpen")
icon = QIcon()
iconThemeName = u"document-open"
if QIcon.hasThemeIcon(iconThemeName):
icon = QIcon.fromTheme(iconThemeName)
else:
icon.addFile(u":/icons/images/document-open.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionOpen.setIcon(icon)
self.actionQuit = QAction(MainWindow)
self.actionQuit.setObjectName(u"actionQuit")
icon1 = QIcon(QIcon.fromTheme(u"application-exit"))
self.actionQuit.setIcon(icon1)
self.actionAbout = QAction(MainWindow)
self.actionAbout.setObjectName(u"actionAbout")
icon2 = QIcon(QIcon.fromTheme(u"help-about"))
self.actionAbout.setIcon(icon2)
self.actionAbout_Qt = QAction(MainWindow)
self.actionAbout_Qt.setObjectName(u"actionAbout_Qt")
self.actionZoom_In = QAction(MainWindow)
self.actionZoom_In.setObjectName(u"actionZoom_In")
icon3 = QIcon()
iconThemeName = u"zoom-in"
if QIcon.hasThemeIcon(iconThemeName):
icon3 = QIcon.fromTheme(iconThemeName)
else:
icon3.addFile(u":/icons/images/zoom-in.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionZoom_In.setIcon(icon3)
self.actionZoom_Out = QAction(MainWindow)
self.actionZoom_Out.setObjectName(u"actionZoom_Out")
icon4 = QIcon()
iconThemeName = u"zoom-out"
if QIcon.hasThemeIcon(iconThemeName):
icon4 = QIcon.fromTheme(iconThemeName)
else:
icon4.addFile(u":/icons/images/zoom-out.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionZoom_Out.setIcon(icon4)
self.actionPrevious_Page = QAction(MainWindow)
self.actionPrevious_Page.setObjectName(u"actionPrevious_Page")
icon5 = QIcon()
iconThemeName = u"go-previous-view-page"
if QIcon.hasThemeIcon(iconThemeName):
icon5 = QIcon.fromTheme(iconThemeName)
else:
icon5.addFile(u":/icons/images/go-previous-view-page.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionPrevious_Page.setIcon(icon5)
self.actionNext_Page = QAction(MainWindow)
self.actionNext_Page.setObjectName(u"actionNext_Page")
icon6 = QIcon()
iconThemeName = u"go-next-view-page"
if QIcon.hasThemeIcon(iconThemeName):
icon6 = QIcon.fromTheme(iconThemeName)
else:
icon6.addFile(u":/icons/images/go-next-view-page.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionNext_Page.setIcon(icon6)
self.actionContinuous = QAction(MainWindow)
self.actionContinuous.setObjectName(u"actionContinuous")
self.actionContinuous.setCheckable(True)
self.actionBack = QAction(MainWindow)
self.actionBack.setObjectName(u"actionBack")
self.actionBack.setEnabled(False)
icon7 = QIcon()
icon7.addFile(u":/icons/images/go-previous-view.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionBack.setIcon(icon7)
self.actionForward = QAction(MainWindow)
self.actionForward.setObjectName(u"actionForward")
self.actionForward.setEnabled(False)
icon8 = QIcon()
icon8.addFile(u":/icons/images/go-next-view.svgz", QSize(), QIcon.Normal, QIcon.Off)
self.actionForward.setIcon(icon8)
self.centralWidget = QWidget(MainWindow)
self.centralWidget.setObjectName(u"centralWidget")
self.verticalLayout = QVBoxLayout(self.centralWidget)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.widget = QWidget(self.centralWidget)
self.widget.setObjectName(u"widget")
self.verticalLayout_2 = QVBoxLayout(self.widget)
self.verticalLayout_2.setSpacing(0)
self.verticalLayout_2.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.splitter = QSplitter(self.widget)
self.splitter.setObjectName(u"splitter")
self.splitter.setOrientation(Qt.Horizontal)
self.searchView = QListView(self.splitter)
self.searchView.setObjectName(u"searchView")
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.searchView.sizePolicy().hasHeightForWidth())
self.searchView.setSizePolicy(sizePolicy)#
self.bookmarkTab = QWidget()
self.bookmarkTab.setObjectName(u"bookmarkTab")
self.verticalLayout_3 = QVBoxLayout(self.bookmarkTab)
self.verticalLayout_3.setSpacing(0)
self.verticalLayout_3.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_3.setObjectName(u"verticalLayout_3")
self.verticalLayout_3.setContentsMargins(2, 2, 2, 2)
self.bookmarkView = QTreeView(self.bookmarkTab)
self.bookmarkView.setObjectName(u"bookmarkView")
sizePolicy.setHeightForWidth(self.bookmarkView.sizePolicy().hasHeightForWidth())
self.bookmarkView.setSizePolicy(sizePolicy)
self.bookmarkView.setHeaderHidden(True)
self.verticalLayout_3.addWidget(self.bookmarkView)
self.pagesTab = QWidget()
self.pagesTab.setObjectName(u"pagesTab")
self.pdfView = QPdfView(self.splitter)
self.pdfView.setObjectName(u"pdfView")
sizePolicy1 = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
sizePolicy1.setHorizontalStretch(10)
sizePolicy1.setVerticalStretch(0)
sizePolicy1.setHeightForWidth(self.pdfView.sizePolicy().hasHeightForWidth())
self.pdfView.setSizePolicy(sizePolicy1)
self.splitter.addWidget(self.pdfView)
self.verticalLayout_2.addWidget(self.splitter)
self.verticalLayout.addWidget(self.widget)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QMenuBar(MainWindow)
self.menuBar.setObjectName(u"menuBar")
self.menuBar.setGeometry(QRect(0, 0, 700, 23))
self.menuFile = QMenu(self.menuBar)
self.menuFile.setObjectName(u"menuFile")
self.menuHelp = QMenu(self.menuBar)
self.menuHelp.setObjectName(u"menuHelp")
self.menuView = QMenu(self.menuBar)
self.menuView.setObjectName(u"menuView")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QToolBar(MainWindow)
self.mainToolBar.setObjectName(u"mainToolBar")
self.mainToolBar.setMovable(False)
self.mainToolBar.setFloatable(False)
MainWindow.addToolBar(Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QStatusBar(MainWindow)
self.statusBar.setObjectName(u"statusBar")
MainWindow.setStatusBar(self.statusBar)
self.menuBar.addAction(self.menuFile.menuAction())
self.menuBar.addAction(self.menuView.menuAction())
self.menuBar.addAction(self.menuHelp.menuAction())
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionQuit)
self.menuHelp.addAction(self.actionAbout)
self.menuHelp.addAction(self.actionAbout_Qt)
self.menuView.addAction(self.actionZoom_In)
self.menuView.addAction(self.actionZoom_Out)
self.menuView.addAction(self.actionPrevious_Page)
self.menuView.addAction(self.actionNext_Page)
self.menuView.addSeparator()
self.menuView.addAction(self.actionContinuous)
self.mainToolBar.addAction(self.actionOpen)
self.mainToolBar.addSeparator()
self.mainToolBar.addAction(self.actionZoom_Out)
self.mainToolBar.addAction(self.actionZoom_In)
self.mainToolBar.addSeparator()
self.mainToolBar.addAction(self.actionBack)
self.mainToolBar.addAction(self.actionForward)
self.retranslateUi(MainWindow)
# self.tabWidget.setCurrentIndex(0)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"PDF Viewer", None))
self.actionOpen.setText(QCoreApplication.translate("MainWindow", u"Open...", None))
#if QT_CONFIG(shortcut)
self.actionOpen.setShortcut(QCoreApplication.translate("MainWindow", u"Ctrl+O", None))
#endif // QT_CONFIG(shortcut)
self.actionQuit.setText(QCoreApplication.translate("MainWindow", u"Quit", None))
#if QT_CONFIG(shortcut)
self.actionQuit.setShortcut(QCoreApplication.translate("MainWindow", u"Ctrl+Q", None))
#endif // QT_CONFIG(shortcut)
self.actionAbout.setText(QCoreApplication.translate("MainWindow", u"About", None))
self.actionAbout_Qt.setText(QCoreApplication.translate("MainWindow", u"About Qt", None))
self.actionZoom_In.setText(QCoreApplication.translate("MainWindow", u"Zoom In", None))
#if QT_CONFIG(shortcut)
self.actionZoom_In.setShortcut(QCoreApplication.translate("MainWindow", u"Ctrl++", None))
#endif // QT_CONFIG(shortcut)
self.actionZoom_Out.setText(QCoreApplication.translate("MainWindow", u"Zoom Out", None))
#if QT_CONFIG(shortcut)
self.actionZoom_Out.setShortcut(QCoreApplication.translate("MainWindow", u"Ctrl+-", None))
#endif // QT_CONFIG(shortcut)
self.actionPrevious_Page.setText(QCoreApplication.translate("MainWindow", u"Previous Page", None))
#if QT_CONFIG(shortcut)
self.actionPrevious_Page.setShortcut(QCoreApplication.translate("MainWindow", u"PgUp", None))
#endif // QT_CONFIG(shortcut)
self.actionNext_Page.setText(QCoreApplication.translate("MainWindow", u"Next Page", None))
#if QT_CONFIG(shortcut)
self.actionNext_Page.setShortcut(QCoreApplication.translate("MainWindow", u"PgDown", None))
#endif // QT_CONFIG(shortcut)
self.actionContinuous.setText(QCoreApplication.translate("MainWindow", u"Continuous", None))
self.actionBack.setText(QCoreApplication.translate("MainWindow", u"Back", None))
#if QT_CONFIG(tooltip)
self.actionBack.setToolTip(QCoreApplication.translate("MainWindow", u"back to previous view", None))
#endif // QT_CONFIG(tooltip)
self.actionForward.setText(QCoreApplication.translate("MainWindow", u"Forward", None))
#if QT_CONFIG(tooltip)
self.actionForward.setToolTip(QCoreApplication.translate("MainWindow", u"forward to next view", None))
#endif // QT_CONFIG(tooltip)
# self.tabWidget.setTabText(self.tabWidget.indexOf(self.bookmarkTab), QCoreApplication.translate("MainWindow", u"Bookmarks", None))
# self.tabWidget.setTabText(self.tabWidget.indexOf(self.pagesTab), QCoreApplication.translate("MainWindow", u"Pages", None))
self.menuFile.setTitle(QCoreApplication.translate("MainWindow", u"File", None))
self.menuHelp.setTitle(QCoreApplication.translate("MainWindow", u"Help", None))
self.menuView.setTitle(QCoreApplication.translate("MainWindow", u"View", None))
# retranslateUi
The documentation states that the information you need can be accessed via roles.
You can inherit QIdentityProxyModel and build your desired text for the DisplayRole.