一.使用Qt设计师制作界面导出程序
1.先新建一个窗口:
2.插入控件widget并将其提升为QWebEngineView:
3.导出python为代码然后导入QWebEngineSettings:
from PySide2.QtWebEngineWidgets import QWebEngineView,QWebEngineSettings
4.找到self.widget = QWebEngineView(self.centralwidget)更改为:
self.widget = WebEngineView(self.centralwidget) #名称可以随意更改上下一致即可
5.在程序末尾添加:
class WebEngineView(QWebEngineView):
def __init__(self, mainwindow):
super(WebEngineView, self).__init__()
#关键代码
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
# 支持视频播放
#self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
6.全部代码:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtWebEngineWidgets import QWebEngineView,QWebEngineSettings
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(u"gridLayout")
self.widget = WebEngineView(self.centralwidget) #名称可以随意更改上下一致即可
self.widget.setObjectName(u"widget")
self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 640, 26))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
# retranslateUi
class WebEngineView(QWebEngineView):
def __init__(self, mainwindow):
super(WebEngineView, self).__init__()
#关键代码
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
# 支持视频播放
#self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
二.编写程序调用UI
1.先导入如下库文件:
import sys
from ui_web import Ui_MainWindow
from PySide2.QtWidgets import QApplication,QMainWindow
from PySide2 import QtCore
2.创建窗口类:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 使用ui文件导入定义界面类
self.ui = Ui_MainWindow()
# 初始化界面
self.ui.setupUi(self)
# 使用界面定义的控件,也是从ui里面访问
self.ui.widget.load(QtCore.QUrl.fromUserInput(URL))
3.调用打开窗口:
if __name__ == "__main__":
app = QApplication([])
mainw = MainWindow()
mainw.show()
sys.exit(app.exec_())
4.完整代码:
from PySide2 import QtCore
URL = 'https://lingmeng.cc/wp-content/uploads/2022/04/ys3248293843-123.pdf'
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 使用ui文件导入定义界面类
self.ui = Ui_MainWindow()
# 初始化界面
self.ui.setupUi(self)
# 使用界面定义的控件,也是从ui里面访问
self.ui.widget.load(QtCore.QUrl.fromUserInput(URL))
if __name__ == "__main__":
app = QApplication([])
mainw = MainWindow()
mainw.show()
sys.exit(app.exec_())
5运行效果:
Comments NOTHING