Qt Image From Web - python

I'd like PyQt to load an image and display it from the web. Dozens of examples I've found online did not work, as they are for downloading the image.
I simply want to view it.
Something like
from PyQt4.QtWebKit import *
web = QWebView()
web.load(QUrl("http://stackoverflow.com/content/img/so/logo.png"))

import sys
from PyQt4 import QtCore, QtGui, QtWebKit
app = QtGui.QApplication(sys.argv)
web = QtWebKit.QWebView()
web.load(QtCore.QUrl("http://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png"))
web.show()
sys.exit(app.exec_())

Related

Why my local html is not accessed when I load it into PySide6 QWebEngineView?

I was learning Qt6, and I wrote a demo putting a local html file into it to test the QWebEngineView Widget. However, the web page shows the info:
Your file counldn't be accessed
It may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND
Here is my test.py source code:
import sys
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout)
from PySide6 import QtCore
from PySide6.QtWebEngineWidgets import QWebEngineView
class webView(QWidget):
def __init__(self):
super(webView, self).__init__()
self.layout = QVBoxLayout(self)
self.webV = QWebEngineView()
self.fileDir = QtCore.QFileInfo("./docs.html").absoluteFilePath()
print(self.fileDir)
self.webV.load(QtCore.QUrl("file:///" + self.fileDir))
self.layout.addWidget(self.webV)
if __name__ == "__main__":
app = QApplication([])
web = webView()
web.show()
sys.exit(app.exec())
In Addition, the docs.html has been put into the same directory as the test.py file. And when I print the web.fileDir, the result is correct.
You are hardcoded the url and the path may be wrong, in these cases the url is better step by step. I assume that the html is next to the .py then the solution is:
import os
from pathlib import Path
import sys
from PySide6.QtCore import QUrl
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
CURRENT_DIRECTORY = Path(__file__).resolve().parent
class webView(QWidget):
def __init__(self):
super(webView, self).__init__()
filename = os.fspath(CURRENT_DIRECTORY / "docs.html")
url = QUrl.fromLocalFile(filename)
self.webV = QWebEngineView()
self.webV.load(url)
layout = QVBoxLayout(self)
layout.addWidget(self.webV)
if __name__ == "__main__":
app = QApplication([])
web = webView()
web.show()
sys.exit(app.exec())
In Qt in general is strongly preferred to use the qrc files, and the Qt resource management system. Here: Can QWebView load images from Qt resource files? is a small yet, neat example of something that is similar to your problem. You may also view the official PySide6 resource usage :
https://doc.qt.io/qtforpython/tutorials/basictutorial/qrcfiles.html

Enable Adobe Reader in QWebEngineView

I would like to know how to enable Adobe Reader in QWebEngineView with PyQt5.
I've already created this piece of code without any success :
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
web = QtWebEngineWidgets.QWebEngineView()
web.settings().setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
web.load(QtCore.QUrl('file:///path/to/my/file.pdf'))
web.show()
sys.exit(app.exec_())
Thanks in advance!

How to save an HTML page to PDF in PyQt5

I'm trying to get some simple html to be saved onto a pdf document using PyQt5.
The webpage renders properly, using the show() command I can get a window showing me the web content in question, however trying to print it to pdf only results in blank pdfs.
This is on python 3.6 and as of posting this question I have the current PyQt5 version. (Windows 10, 64 Bit)
There's a lot of older questions to this already on stackoverflow, but I found most of them use functions that no longer exist.
The problematic code follows:
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5 import QtWidgets
from PyQt5.QtPrintSupport import QPrinter
from PyQt5 import QtCore
[...]
app = QtWidgets.QApplication(sys.argv)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.ScreenCaptureEnabled, True)
loader = QWebEngineView()
#loader.setAttribute(QtCore.Qt.WA_DontShowOnScreen, True)
loader.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
loader.setZoomFactor(1)
loader.setHtml(webpage)
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("test.pdf")
printer.setOrientation(QPrinter.Portrait)
printer.setFullPage(True)
def emit_pdf(finished):
loader.show()
loader.render(printer)
if self.open_on_complete:
import webbrowser
webbrowser.open("test.pdf")
#app.exit()
loader.loadFinished.connect(emit_pdf)
app.exec()
There is no need to use use QPrinter in Qt5, because QWebEnginePage has a dedicated printToPdf method:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(
lambda *args: print('finished:', args))
loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))
def emit_pdf(finished):
loader.show()
loader.page().printToPdf("test.pdf")
loader.loadFinished.connect(emit_pdf)
app.exec()
There's also a print method that will render to a printer, if you really want that.

Python QWebEngineView redirects to wrong language page

When I browse to a page using the following code, the result is in another language (Russian I think). When I browse to the same url in other browsers I get the English 404 page as expected. I tried setting the accept language, but that didn't help. What am I missing?
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWebEngineWidgets import QWebEngineView
app = QtWidgets.QApplication(sys.argv)
w = QWebEngineView()
w.page().profile().setHttpAcceptLanguage('en') # This doesn't help
w.load(QtCore.QUrl('http://turbobit.net/download')) # Goes to russian? 404 page
w.show()
app.exec_()
The following webkit version works as expected
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWebKitWidgets import QWebView
app = QtWidgets.QApplication(sys.argv)
w = QWebView()
w.setUrl(QtCore.QUrl('http://turbobit.net/download')) # Loads correct English 404 page
w.show()
app.exec_()
You need to set the language before creating the view:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
app = QtWidgets.QApplication(sys.argv)
QWebEngineProfile.defaultProfile().setHttpAcceptLanguage('en')
w = QWebEngineView()
w.load(QtCore.QUrl('http://turbobit.net/download'))
w.show()
app.exec_()

Hyperlink to web page in QLabel using PySide

I am having troubles displaying a functional hyperlink to a web page in a QLabel using PySide (Python version 3.2.5). Reading the most upvoted answer from this post c++ - Making QLabel behave like a hyperlink - Stack Overflow, I was under the impression that this would work:
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QLabel
app = QApplication([])
window = QLabel()
window.setText("<a href=\"http://stackoverflow.com/\" />Stack Overflow</a>")
window.setTextFormat(Qt.RichText)
window.setTextInteractionFlags(Qt.TextBrowserInteraction)
window.setOpenExternalLinks(True)
window.show()
app.exec_()
Unfortunately, the text does not show up as a link. Can anyone please point me in the right direction?
Your link are broken, Fix it should be fine;
from PyQt4.QtGui import QApplication, QLabel
myQApplication = QApplication([])
myQLabel = QLabel()
myQLabel.setText('''<a href='http://stackoverflow.com'>stackoverflow</a>''')
myQLabel.setOpenExternalLinks(True)
myQLabel.show()
myQApplication.exec_()

Categories