【问题标题】:Showing content of QCH-File with QWebEngineView使用 QWebEngineView 显示 QCH 文件的内容
【发布时间】:2018-10-26 11:18:18
【问题描述】:

我们在项目中使用 Qt-Help,但我对 Qt-assistant 中 Qt-Help 的格式真的不满意。与我的 Firefox 中 HTML 文件的格式相比,它看起来真的很难看。

其中一个原因可能是,Qt 助手在其渲染中忽略了 javascript。

因此我尝试实现一个非常简单的测试运行程序,它应该显示 QHC 文件的内容。

#include <iostream>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QHelpContentWidget>
#include <QHelpEngine>
#include <QWebEngineView>

int main(int argc, char** args) {
    QApplication app(argc, args);
    auto help = new QHelpEngine("./data/MyHelp.qhc");
    help->contentWidget()->show();
    QObject::connect(help->contentWidget(), &QHelpContentWidget::linkActivated, [&](const QUrl &link) {
        QDialog dialog;
        auto helpContent = new QWebEngineView;
        helpContent->load(link);
        dialog.setLayout(new QHBoxLayout);
        dialog.layout()->addWidget(helpContent);
        dialog.exec();
    });
    app.exec();
}

很遗憾,QWebEngineView 找不到 QHC 文件的 QUrl 链接。

如何配置QWebEngineView,以便它在 QHC 文件中查找资源?还必须找到 HTML 帮助文件中的所有图像和其他外部资源。

也许QWebEngineUrlSchemeHandler 类可能会有所帮助。

【问题讨论】:

    标签: qt python-sphinx qwebengineview qwebpage qthelp


    【解决方案1】:

    经过一番麻烦,我为我的问题找到了一个可行的解决方案。

    ma​​in.cpp

    #include <iostream>
    #include <QApplication>
    #include <QDebug>
    #include <QDialog>
    #include <QHBoxLayout>
    #include <QHelpContentWidget>
    #include <QHelpEngine>
    #include <QWebEngineView>
    #include <QWebEnginePage>
    #include <QWebEngineProfile>
    #include <QDebug>
    #include "QtHelpSchemeHandler.h"
    
    int main(int argc, char** args) {
        QApplication app(argc, args);
        auto help = new QHelpEngine("./data/MyHelp.qhc");
        qDebug() << help->setupData();
        help->contentWidget()->show();
        QObject::connect(help->contentWidget(), &QHelpContentWidget::linkActivated, [&](const QUrl &link) {
            QDialog dialog;
            auto helpContent = new QWebEngineView;
            helpContent->page()->profile()->installUrlSchemeHandler("qthelp", new QtHelpSchemeHandler(help));
            helpContent->load(link);
            QObject::connect(helpContent, &QWebEngineView::loadFinished, []() {qDebug() << "Load finished"; });
            dialog.setLayout(new QHBoxLayout);
            dialog.layout()->addWidget(helpContent);
            dialog.exec();
        });
        app.exec();
    }
    

    QtHelpSchemeHandler

    #include <QWebEngineUrlSchemeHandler>
    #include <QDebug>
    #include <QHelpEngine>
    #include <QWebEngineUrlRequestJob>
    #include <QBuffer>
    
    class QtHelpSchemeHandler : public QWebEngineUrlSchemeHandler {
        Q_OBJECT
    public:
        QtHelpSchemeHandler(QHelpEngine* helpEngine) : mHelpEngine(helpEngine) {
    
        }
    
        virtual void requestStarted(QWebEngineUrlRequestJob* job) override {
            auto url = job->requestUrl();
            auto data = new QByteArray; // Needs to be destroyed. Not re-entrant
            *data = mHelpEngine->fileData(url);
            auto buffer = new QBuffer(data);
            if (url.scheme() == "qthelp") {
                job->reply("text/html", buffer);
            }
        }
    private:
        QHelpEngine* mHelpEngine;
    };
    

    生成的输出适合我的 Firefox 浏览器中的 HTML 呈现。

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 2016-05-30
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多