我在我的应用程序之间创建了一个Web通道WebEngine,以便QObject在JavaScript中暴露在Web端,但是在页面重新加载后或者如果我单击链接到另一个页面时,通道会丢失.
我想我需要在页面重新加载时重新创建频道,但我没有设法做到这一点.我尝试在页面加载,进度和完成的插槽上执行此操作但只得到了js: Uncaught ReferenceError: qt is not defined.
<!-- language: lang-cpp -->
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webEngineWidget->load(QUrl("qrc:/index.html"));
page = ui->webEngineWidget->page();
channel = new QWebChannel;
channel->registerObject("external", &exposedObject);
page->setWebChannel(channel);
connect(page, &QWebEnginePage::loadStarted, this, &MainWindow::onPageLoadStarted);
connect(page, &QWebEnginePage::loadProgress, this, &MainWindow::onPageLoadProgress);
connect(page, &QWebEnginePage::loadFinished, this, &MainWindow::onPageLoadFinished);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onPageLoadStarted()
{
qDebug() << "Loading started";
}
void MainWindow::onPageLoadProgress(int progress)
{
qDebug() << "Loading in progress: " << progress;
}
void MainWindow::onPageLoadFinished()
{
qDebug() …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Qt中创建一个Dialog加载一个URL(我不想向最终用户公开,因此是一个Dialog).用户在页面上输入凭据后,服务器将返回我想要捕获的重定向URL.我怎样才能做到这一点?
QtWebkit使这很容易,因为QWebView有一个QNetworkAccessManager对象.但是使用QtWebEngine,QWebEngineView类没有此功能.前者还允许通过使用QNetworkRequest类为任何请求设置HTTP标头,然后在QWebView中加载具有这些特定请求的请求.如何使用QWebEngineView执行此操作?
我有一个在小部件中查看页面的应用程序,QWebEngineView如何将我的 javascript 控制台日志重定向到我的 GUI?它当前显示在我的调试输出中。
当我运行Qt应用程序时,我收到消息
Qt WebEngine似乎是从插件初始化的。在构造QGuiApplication之前,请使用QCoreApplication :: setAttribute设置Qt :: AA_ShareOpenGLContexts。
无论将其转储到终端上,该应用程序都可以正常运行。我似乎无法找到根本原因,也无法真正理解此消息试图告诉我的内容。此消息是什么意思,我该如何解决?
我正在QtWebEngineWidgets展示一些 pdf 文件。我想更改 pdf 并强制QtWebEngineView自动动态显示。我遇到的问题是QtWebEngineWidgetspdf 文件路径更改时无法更新、无法显示。
class PdfReport(QtWebEngineWidgets.QWebEngineView):
PDFJS = 'file:///pdfjs/web/viewer.html'
def __init__(self, parent=None):
super(PdfReport, self).__init__(parent)
self.PDF = 'file:///Technicalreport/file0.pdf'
self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, self.PDF)))
@QtCore.pyqtSlot(int)
def index_load(self, _index):
self._index = _index
self.PDF = pdfpath(self._index)
Run Code Online (Sandbox Code Playgroud)
外部函数:
def pdfpath(index):
if index == -1:
PDF = 'file:///Technicalreport/file0.pdf'
else:
PDF = 'file:///Technicalreport/file%d.pdf' %index
return PDF
Run Code Online (Sandbox Code Playgroud)
尝试测试函数并按预期返回:
for i in range(3):
print(pdfpath(i), type(pdfpath(i)))
file:///Technicalreport/file0.pdf <class 'str'>
file:///Technicalreport/file1.pdf <class 'str'>
file:///Technicalreport/file2.pdf <class 'str'>
Run Code Online (Sandbox Code Playgroud)
是的 pdf 文件'file0','file1'并且'file2' …
我想在我的 Qt 应用程序中创建一个简单的 PDF 查看器。一切正常,接受通过 URL 在特定页面打开 PDF。例如:
url = "file:///D://Repo//PdfViewer//PdfViewer//test.pdf";
Run Code Online (Sandbox Code Playgroud)
有效,但是
url = "file:///D://Repo//PdfViewer//PdfViewer//test.pdf#page=9";
Run Code Online (Sandbox Code Playgroud)
剂量。我在某处读到,chrome 不再正式支持 #page=x,但我找不到更多信息如何解决这个问题。我在 Qt 的 Nano-Browser 示例中遇到了与此 url 相同的问题。
PdfViewer::PdfViewer(const QString &pdf_path, QWidget *parent)
: QWidget(parent), m_View(new QWebEngineView(this)), m_ExitButton(new QPushButton())
{
QUrl url = QUrl::fromLocalFile(pdf_path);
m_View->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
m_View->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
m_View->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
m_View->load(url);
m_ExitButton->setIcon(QIcon("Ok.png"));
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(m_View);
layout->addWidget(m_ExitButton);
this->setLayout(layout);
connect(m_ExitButton, &QPushButton::clicked, this, &PdfViewer::close);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Qt 5.13。
我写了一个这样的python测试程序来显示openstreetmap:
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys
def mainPyQt5():
url = 'file:///./index.html'
app = QApplication(sys.argv)
browser = QWebEngineView()
browser.load(QUrl(url))
browser.show()
sys.exit(app.exec_())
mainPyQt5()
Run Code Online (Sandbox Code Playgroud)
QWebEngineView 获取的 index.html 只需调用 openstreetmap:
<title>OSM and Leaflet</title>
<link rel = "stylesheet" href = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
<div id = "map" style = "width: 900px; height: 580px"></div><script src = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script>
// Creating map options
var mapOptions = {
center: [45.641174, 9.114828],
zoom: 10
}
// Creating a map object
var map …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Qt中实现网页的显示。我选择使用Qt WebEngine来完成任务。这是我所做的:
在我的代码中,它看起来像这样:
View = new QWebEngineView(this);
// read the js file using qfile
file.open("path to jsFile");
myJsApi = file.Readall();
View->page()->runjavascript (myjsapi);
View->page()->runjavascript ("createRadioButton(\"button1\");");
Run Code Online (Sandbox Code Playgroud)
我发现该runJavaScript()功能对网页没有影响。我可以在输出窗口中看到该网页,但是我期望的单选按钮不存在。我究竟做错了什么?
我正在尝试构建一个简单的示例,在 PyQt 窗口中显示 HTML(包括 JavaScript 代码):
python
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl,QDir
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Example(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout(self)
self.webEngineView = QWebEngineView()
self.webEngineView.setHtml("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="file:///home/path/to/jquery-3.6.0.min.js"></script>
</head>
<body>
<!-- page content -->
<span id="aaa">toto</span>
<script>
$("#aaa").hide()
</script>
</body>
</html>""")
vbox.addWidget(self.webEngineView)
self.setLayout(vbox)
self.show()
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
但显然,位于我的 Python 脚本旁边的 jQuery JavaScript …
qtwebengine ×9
qt ×5
python ×4
javascript ×3
pyqt5 ×3
c++ ×2
pyqt ×2
html ×1
pdf ×1
pyside2 ×1
python-3.x ×1
qt5 ×1
qtwebkit ×1
qwebview ×1
sockets ×1