1

We are using Qt-Help in our projects, but I'm really not satisfied with the formatting of the Qt-Help inside the Qt-assistant. It looks really ugly compared to the formatting of the HTML-files inside my Firefox.

One of the reasons might be, that the Qt-assistant ignores javascript in its rendering.

Therefore I tried to implement a very simple testrunner, that should display the contents of a QHC-file.

#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();
}

Unfortunately, the QWebEngineView will not find QUrl link of the QHC-file.

How can I configure QWebEngineView, so that it will look for the resource inside the QHC-file? It is also necessary, that all images and other externals resources inside the HTML help files are found.

Maybe the class QWebEngineUrlSchemeHandler could be of some help.

Aleph0
  • 5,816
  • 4
  • 29
  • 80

1 Answers1

2

After some hassle I derived a working solution for my problem.

main.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;
};

The generated output fits the HTML rendering in my Firefox browser.

Aleph0
  • 5,816
  • 4
  • 29
  • 80