Pyqt5 pdf viewer doesn't display zoom buttons or zoom percentage

Question:

I’m learning how to use pyqt, I’m using Windows 10 and python 3.10 and i’m testing a code to open pdf files, this allows you to select the file and then opens it on a new window. It works almost correctly except that doesn’t show zoom buttons, or zoom percentage.

This is the code:

import sys

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt5 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

I was wondering if I need to pass an extra setting to display zoom buttons on pdf viewer. Anyone can help me? I highlighted where buttons/actions supposed to display in the image below.

I highlighted where buttons/actions supposed to display

Asked By: Ticsup

||

Answers:

That problem is because Qt version, if you test with the most recent version, PyQt6 you will see that works.

You need to install PyQt6:

pip install pyqt6

You also need to install webengine for pyqt6:

pip install PyQt6-WebEngine

And you need to change your code to this:

import sys

from PyQt6 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt6 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(view.settings().WebAttribute.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()

And now should look like this:

loaded pdf

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.