How To Create Full Screen Mode For A Youtube Application With Pyqt5?
We have created an application to watch videos on youtube but also with the possibility to search on the Internet. The problem is that on the full screen side I can't make it work
Solution 1:
The solution is to reset the QWebEngineView as centralwidget.
TL; DR;
If you are going to use Qt Designer then you should not modify the class generated by pyuic as indicated in the warning, and one of the reasons is that this class is not a widget, so many times you will have problems, but in your case you have practically deleted what Qt Designer has provided so I rewrote your application.
Going to the problem, by setting setParent(None)
you are indicating that the widget is not part of the window and therefore will be part of another new window where that widget is the toplevel, so if you want to restore it you must set it as centralWidget so that Be part of the window again.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
classMainWindow(QtWidgets.QMainWindow):
def__init__(self, parent=None):
super().__init__(parent)
self.browser = QtWebEngineWidgets.QWebEngineView()
self.setCentralWidget(self.browser)
self.tbNavigate = QtWidgets.QToolBar(orientation=QtCore.Qt.Horizontal)
self.addToolBar(QtCore.Qt.TopToolBarArea, self.tbNavigate)
action_data = [
("Back", "icone/next.png", self.browser.back),
("Forward", "icone/next.png", self.browser.forward),
(
"Reload this page",
"icone/baseline_refresh_black_18dp.png",
self.browser.reload,
),
("Home", "icone/home (1).png", self.navigateHome),
]
for text, icon_path, slot in action_data:
icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(icon_path), QtGui.QIcon.Normal, QtGui.QIcon.Off
)
action = QtWidgets.QAction(text, icon=icon, triggered=slot, parent=self)
self.tbNavigate.addAction(action)
self.httpsicon = QtWidgets.QLabel()
self.tbNavigate.addWidget(self.httpsicon)
self.addressbar = QtWidgets.QLineEdit()
self.addressbar.returnPressed.connect(self.navigate_to_url)
self.tbNavigate.addWidget(self.addressbar)
self.browser.urlChanged.connect(self.update_urlBar)
self.browser.load(QtCore.QUrl("https://youtube.com"))
global_settings = QtWebEngineWidgets.QWebEngineSettings.globalSettings()
for attr in (
QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled,
QtWebEngineWidgets.QWebEngineSettings.FullScreenSupportEnabled,
):
global_settings.setAttribute(attr, True)
self.browser.page().fullScreenRequested.connect(self.FullscreenRequest)
@QtCore.pyqtSlot()defnavigateHome(self):
self.browser.setUrl(QtCore.QUrl("https://www.youtube.com"))
@QtCore.pyqtSlot()defnavigate_to_url(self):
q = QtCore.QUrl(self.addressbar.text())
ifnot q.scheme():
q.setScheme("https")
self.browser.setUrl(q)
@QtCore.pyqtSlot(QtCore.QUrl)defupdate_urlBar(self, url):
pixmap = (
QtGui.QPixmap("icone/ssl.png")
if url.scheme() == "https"else QtGui.QPixmap("icone/lock.png")
)
self.httpsicon.setPixmap(pixmap)
self.addressbar.setText(url.toString())
self.addressbar.setCursorPosition(0)
@QtCore.pyqtSlot("QWebEngineFullScreenRequest")defFullscreenRequest(self, request):
request.accept()
if request.toggleOn():
self.browser.setParent(None)
self.browser.showFullScreen()
else:
self.setCentralWidget(self.browser)
self.browser.showNormal()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Post a Comment for "How To Create Full Screen Mode For A Youtube Application With Pyqt5?"