如何在WebEngineView中呈现的html文件中弹出文件对话框/打印对话框?

时间:2018-05-04 18:18:04

标签: qt highcharts qml qtquick2 qwebengineview

我有一个本地html文件,使用highcharts显示我的数据并保存到图像,它在chrome浏览器中运行良好,然后我尝试使用WebEngineView(QML类型)在Qt 5.9.4中加载它,所有弹出窗口对话框无法显示。

html文件代码:https://jsfiddle.net/sylaince/9x9j5Lpj/

<div id="container" style="width: 100%; min-height: 400px"></div>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/offline-exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script type="text/javascript">
    Highcharts.setOptions({
        navigation: {
            menuItemStyle: {
                padding: '6px 14px'
            }
        }
    });
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container'
        },
        exporting: {
            filename: 'data'
        },
        title: {
            text: 'export file'
        },
        xAxis: {
            categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
        },
        series: [{
            name: 'test data',
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
    });
</script>

qml文件:

Page {  
    WebEngineView {
        id: webView
        anchors.fill: parent
        url: "qrc:/html/index.html"
    }
}

如何使WebEngineView显示对话框,如文件对话框,打印对话框等。

1 个答案:

答案 0 :(得分:1)

弹出窗口有三种类型:

  1. 打印预览
  2. 下载文件。
  3. 导航到另一页
  4. 所以在部分中指出解决方案:

    1.打印预览

    看来它仍然没有在Qt中实现:https://bugreports.qt.io/browse/QTBUG-57982,门票评论中建议的解决方法是以PDF格式打印然后显示PDF(例如,使用pdf.js),我试过了但是我没有运气。

    2.下载文件

    这个问题类似于我回答的previous question

    正如我在上一篇回答中所说:浏览器会生成弹出窗口,如果是QWebEngine,我们必须创建

    创建对话框我们可以使用QML FileDialog,但这些都没有阻塞,所以你必须接受下载,而且这种行为不是传统浏览器所见,所以我的解决方案将使用QWidgets QFileDialog自允许等待并能够验证接受。对于实现,实现一个允许我们管理它的帮助器。

    class DownloadHelper: public QObject{
        Q_OBJECT
    public:
        Q_INVOKABLE void onDownloadRequested(QObject * download){
            QString old_path = download->property("path").toString();
            QString suffix = QFileInfo(old_path).suffix();
            QString path  = QFileDialog::getSaveFileName(nullptr, "Save File", old_path, "*."+suffix);
            if(!path.isEmpty()){
                download->setProperty("path", path);
                bool accepted = QMetaObject::invokeMethod(download, "accept"); 
                Q_ASSERT(accepted);
            }
        }
    };
    # ...
    DownloadHelper helper;
    engine.rootContext()->setContextProperty("downloadHelper", &helper);
    # ...
    

    <强> *。QML

    WebEngineView {
        id: webView
        anchors.fill: parent
        url: "qrc:/html/index.html"
        profile.onDownloadRequested: downloadHelper.onDownloadRequested(download)
        // ...
    

    3.导航到另一页

    当按下“在HighCharts Cloud中打开”菜单时,它会启动打开新窗口的请求,要使用它,我们会使用newViewRequested信号动态创建窗口:< / p>

    WebEngineView {
        id: webView
        // ...
    
        onNewViewRequested: {
            var newWindow = Qt.createQmlObject('import QtQuick.Window 2.2;
                                                import QtWebEngine 1.5;
                                                 Window {
                                                    width: 640; height: 480;
                                                    visible: true;
                                                    property alias wv: wv
                                                    WebEngineView{id:wv; anchors.fill: parent}}',
                                               webView,
                                               "dynamicSnippet1");
            console.log(newWindow.wv)
            request.openIn(newWindow.wv)
        }
    

    完整的实施可以在以下link找到。