I’m trying to load a UI file using PyQt5.uic.loadUi
which has QWebEngineView
, but my code currently loads a blank QT form.
It was working before until I changed the way classes were referencing each other. I’m new to working with classes and cannot understand the correct use of self and parent.
import sys from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtWebEngineWidgets import QWebEngineView html = """ <!DOCTYPE html><html><head></head><body> <div id="output">Test page</div> </body></html> """ text1 = 'demo text' class Ui(QMainWindow): def __init__(self): super(Ui, self).__init__() uic.loadUi('demoweb.ui', self) ctr = self.findChild(QWidget,"webViewContainer") self.browser = ctr.findChild(QWebEngineView,"webEngineView") self.edit = self.findChild(QLineEdit,"lineEdit") class Window(QtWidgets.QWidget): def __init__(self, parent = None): super().__init__(parent) ui = Ui() self._ui = ui ui.browser.setHtml(html) page = ui.browser.page() page.loadFinished.connect(self.onLoadFinished) ui.edit.setText(text1) self._ready = False def onLoadFinished(self): #code to be added return if __name__ == "__main__": app = QtWidgets.QApplication([]) window = Window() window.show() app.exec()
demoweb.ui file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect><x>0</x><y>0</y><width>746</width><height>462</height></rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLineEdit" name="lineEdit"> <property name="geometry"> <rect><x>21</x><y>361</y><width>691</width><height>22</height></rect> </property> </widget> <widget class="QWidget" name="webViewContainer" native="true"> <property name="geometry"> <rect><x>10</x><y>10</y><width>711</width><height>331</height></rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QWebEngineView" name="webEngineView"> <property name="url"> <url> <string>about:blank</string> </url> </property> </widget> </item> </layout> </widget> </widget> </widget> <customwidgets> <customwidget> <class>QWebEngineView</class> <extends>QWidget</extends> <header location="global">QtWebEngineWidgets/QWebEngineView</header> </customwidget> </customwidgets> <resources/> <connections/> </ui>
Advertisement
Answer
This has been resolved after following suggestions from musicmante
Thanks eyllanesc, your suggestion also resolved the problem but was no longer required in the simplified code.
Working code below
import sys from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtWebEngineWidgets import QWebEngineView html = """ <!DOCTYPE html><html><head></head><body> <div id="output">Test page</div> </body></html> """ text1 = 'demo text' class Ui(QMainWindow): def __init__(self): super(Ui, self).__init__() uic.loadUi('demoweb.ui', self) ctr = self.findChild(QWidget,"webViewContainer") self.browser = ctr.findChild(QWebEngineView,"webEngineView") self.edit = self.findChild(QLineEdit,"lineEdit") class Window(QtWidgets.QMainWindow): def __init__(self, parent = None): super().__init__(parent) uic.loadUi('demoweb.ui', self) self.webEngineView.setHtml(html) page = self.webEngineView.page() page.loadFinished.connect(self.onLoadFinished) self.lineEdit.setText(text1) self._ready = False def onLoadFinished(self): #code to be added return if __name__ == "__main__": app = QtWidgets.QApplication([]) window = Window() window.show() app.exec()