Reputation: 35
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 the way I want it to full screen, but when I want to exit full screen, I get two windows instead of one or if I get stuck with a window, I delete QWebEngineView. I don't have much experience with this Qt framework and I will be grateful if you help me.
Since I use PyQt5 version 5.13.2
That's all the code
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1200, 800)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.hboxlayout = QtWidgets.QHBoxLayout(self.centralWidget)
self.hboxlayout.setSpacing(6)
self.hboxlayout.setContentsMargins(0, 0, 0, 0)
self.hboxlayout.setObjectName("hboxlayout")
#bara de navigare pe care sun asezate butoanele beak,inaninte etc.
MainWindow.setCentralWidget(self.centralWidget)
self.tbNavigate = QtWidgets.QToolBar(MainWindow)
self.tbNavigate.setOrientation(QtCore.Qt.Horizontal)
self.tbNavigate.setObjectName("tbNavigate")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.tbNavigate)
self.browser=QWebEngineView(MainWindow)
self.browser.load(QUrl('https://youtube.com'))
MainWindow.setCentralWidget(self.browser)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True)
self.browser.page().fullScreenRequested.connect(self.FullscreenRequest)
#butonul back
self.actionBack = QtWidgets.QAction("Back")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icone/back.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionBack.setIcon(icon1)
self.actionBack.setObjectName("actionBack")
#butonul inaite
self.actionForward = QtWidgets.QAction("Forward")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icone/next.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionForward.setIcon(icon2)
self.actionForward.setObjectName("actionForward")
#butonul refresh
self.actionRefresh = QtWidgets.QAction("Reload this page")
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("icone/baseline_refresh_black_18dp.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionRefresh.setIcon(icon4)
self.actionRefresh.setObjectName("actionRefresh")
#Butonul home
self.actionHome = QtWidgets.QAction("Home")
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("icone/home (1).png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionHome.setIcon(icon5)
self.actionHome.setObjectName("actionHome")
self.actionFileClose = QtWidgets.QAction(MainWindow)
self.actionFileClose.setObjectName("actionFileClose")
#Sll icon or no-sll
self.httpsicon = QLabel()
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("icone/lock.png"), QtGui.QIcon.Normal,QtGui.QIcon.Off)
#self.httpsicon.setIcon(icon6)
self.httpsicon.setObjectName("httpsicon")
#functinare butonelor pe bara de navigare
self.tbNavigate.addAction(self.actionBack)
self.tbNavigate.addAction(self.actionForward)
self.tbNavigate.addAction(self.actionRefresh)
self.tbNavigate.addAction(self.actionHome)
self.tbNavigate.addWidget(self.httpsicon)
#self.tbNavigate.addSeparator()
self.actionBack.triggered.connect( self.browser.back)
self.actionForward.triggered.connect(self.browser.forward)
self.actionRefresh.triggered.connect(self.browser.reload)
self.actionHome.triggered.connect(self.navigateHome)
#self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.addressbar =QLineEdit()
self.addressbar.returnPressed.connect(self.navigate_to_url)
self.tbNavigate.addWidget(self.addressbar)
#citeste url
self.browser.urlChanged.connect(self.update_urlBar)
def navigateHome(self):
self.browser.setUrl(QUrl("https://www.youtube.com"))
def navigate_to_url(self):
q = QUrl(self.addressbar.text())
if q.scheme() == "":
q.setScheme("https")
self.browser.setUrl(q)
def update_urlBar(self, q):
if q.scheme() == 'https':
self.httpsicon.setPixmap(QPixmap("icone/ssl.png"))
else:
self.httpsicon.setPixmap(QPixmap("icone/lock.png"))
self.addressbar.setText(q.toString())
self.addressbar.setCursorPosition(0)
def FullscreenRequest(self, request):
print("requested")
if(request.toggleOn()):
request.accept()
self.browser.setParent(None)
self.browser.showFullScreen()
else:
request.accept()
self.browser.showNormal()
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
#sys.exit(app.exec_())
app.exec_()
The problem is here:
def FullscreenRequest(self, request):
print("requested")
if(request.toggleOn()):
request.accept()
self.browser.setParent(None)
self.browser.showFullScreen()
else:
request.accept()
self.browser.showNormal()
Upvotes: 1
Views: 1548
Reputation: 243897
The solution is to reset the QWebEngineView as centralwidget.
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
class MainWindow(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()
def navigateHome(self):
self.browser.setUrl(QtCore.QUrl("https://www.youtube.com"))
@QtCore.pyqtSlot()
def navigate_to_url(self):
q = QtCore.QUrl(self.addressbar.text())
if not q.scheme():
q.setScheme("https")
self.browser.setUrl(q)
@QtCore.pyqtSlot(QtCore.QUrl)
def update_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")
def FullscreenRequest(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_())
Upvotes: 1