柴_笔记 2024-10-21 15:06 采纳率: 15.4%
浏览 3
已结题

QWebEngineView

QWebEngineView加载网页时:通过runJavaScript运行脚本将网页中的按钮ID回传到Qt;

要求:直接拿测试案例演示
好处:费用可谈

有实际经验的,留言,V联系,

  • 写回答

5条回答 默认 最新

  • 专家-皆知 2024-10-21 15:07
    关注

    参考免费微信小程序:皆我百晓生 作答,经由专家-皆知整理回复,如问题解决,望采纳;如仍有疑问,请评论回复。

    为了解决您的问题,我们将通过一个简单的Qt应用程序示例来展示如何使用QWebEngineView加载网页,并通过runJavaScript执行JavaScript代码来获取网页中按钮的ID,然后将这个ID传递回Qt。这里我们假设您想要获取的是网页中第一个按钮的ID。

    首先,确保您的Qt环境已经配置了Qt WebEngine模块。接下来,是一个简单的Qt5项目示例代码:

    1. 创建Qt Widgets Application,选择适当的项目名称,例如WebViewButtonID.

    2. mainwindow.cpp 文件的修改如下(假设您使用的是Qt Creator自动生成的项目结构):

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWebEngineView>
    #include <QWebChannel>
    #include <QJsonObject>
    
    class WebBridge : public QObject {
        Q_OBJECT
    public:
        explicit WebBridge(QObject *parent = nullptr) : QObject(parent) {}
    
    public slots:
        void buttonIdReceived(const QString &id) {
            emit buttonIdGot(id);
        }
    
    signals:
        void buttonIdGot(const QString &id);
    };
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent), ui(new Ui::MainWindow) {
        ui->setupUi(this);
    
        QWebEngineView *view = new QWebEngineView(this);
        ui->centralWidget->setWidget(view);
    
        QWebEnginePage *page = view->page();
        WebBridge *webBridge = new WebBridge(this);
        connect(webBridge, &WebBridge::buttonIdGot, this, &MainWindow::onButtonIdReceived);
    
        // 使用QWebChannel让JavaScript可以调用C++
        QWebChannel *channel = new QWebChannel(this);
        channel->registerObject(QStringLiteral("webBridge"), webBridge);
        page->setWebChannel(channel);
    
        // 加载测试HTML
        view->load(QUrl(QStringLiteral("qrc:///index.html")));
    
        // JavaScript代码,用于获取按钮ID并发送回Qt
        QString jsCode = R"(
            var buttonId = document.querySelector('button').id;
            console.log('Button ID:', buttonId);
            window.webBridge.buttonIdReceived(buttonId);
        )";
    
        // 确保页面加载完成后执行JavaScript代码
        page->loadFinished.connect([=](bool ok) {
            if (ok) {
                page->runJavaScript(jsCode);
            }
        });
    }
    
    void MainWindow::onButtonIdReceived(const QString &id) {
        QMessageBox::information(this, tr("Button ID"), tr("The Button ID is: ") + id);
    }
    
    MainWindow::~MainWindow() {
        delete ui;
    }
    
    1. 创建一个简单的HTML文件 (index.html) 放在项目的资源文件夹中(通常是在resources或者直接放在构建目录并正确设置资源路径)。这个HTML文件将包含一个按钮以便我们能获取其ID。
    <!DOCTYPE html>
    <html>
    <head>
        <title>Button ID Test</title>
    </head>
    <body>
        <button id="myButton">Click me</button>
        <script>
            // 这里原本的逻辑会被QWebChannel的通信替代
        </script>
    </body>
    </html>
    
    1. 在Qt项目文件(.pro) 中添加HTML文件作为资源:
    QT += webenginewidgets
    RESOURCES += resources.qrc
    
    # 添加index.html到资源
    resources.qrc:
        <file alias="index.html">path/to/your/index.html</file>
    

    确保将index.html的实际路径替换为文件的实际位置,或者将其直接放在项目目录下并通过资源系统正确引用。

    这段代码演示了如何通过QWebEngineView加载一个包含按钮的简单网页,使用JavaScript获取该按钮的ID,并通过QWebChannel将其发送回Qt应用程序,最终显示在一个消息框中。这满足了您提出的要求。请注意,实际应用中可能需要更复杂的错误处理和兼容性考虑。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月21日
  • 创建了问题 10月21日