QtWebView - C ++ - 如何从linkClicked事件中返回javascript字符串

时间:2013-01-02 18:19:24

标签: c++ qt4 qwebview qwebkit qwebpage

我一直无法找到解决问题的明确答案。

我目前有一个QWebView控件加载我的硬盘上的html文件,它实现了一个带有javascript动作的矢量地图。 html文件加载jvectormap US States Map。任何状态的单击操作都会触发与showCities函数关联的onRegionClick操作,该事件和代码参数由jquery-jvectormap.js文件中实现的onRegionClick方法处理。 html文件如下:

<!DOCTYPE html>

<html lang="en">
<head>

    <link rel="stylesheet" href="styles/style2.css" type="text/css"/>


    <link rel="stylesheet" media="all" href="styles/jquery-jvectormap.css"/>
    <script src="js/jquery.tools.min.js"></script>
    <script src="js/jquery-jvectormap.js"></script>
    <script src="js/jquery-jvectormap-us-en.js"></script>
    <script>
        $(function() {
            $('.map').vectorMap({
                map: 'us_en',
                color: 'green',
                hoverColor: 'blue',
                onRegionClick: showCities
            });
        });
        function showCities(event, code) {
            return code;

        }


    </script>
</head>


<body>



    <div id="front">


        <div class="map"></div>

        </div>


</body>
</html>

Qt中的mainwindow.h头文件信息:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUrl>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void on_webView_linkClicked(const QUrl &arg1);


private:
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

然后是MainWindow.cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtGui>
#include <QtCore>
#include <QDebug>
#include <QtWebKit>
#include <QWebSettings>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   ui->setupUi(this);
   ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
   ui->webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
   connect(ui->webView,SIGNAL(linkClicked(QUrl)),this,SLOT(on_webView_linkClicked(QUrl)));

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_webView_linkClicked(const QUrl &arg1)
{
    QMessageBox::information(this,"Title","Test");

}

单击任何特定区域区域时,未触发/触发on_webView_linkClicked函数。单击的任何区域都旨在返回一个字符串。当我在网页上尝试此操作时,如http://www.google.com,on_WebView_linkClicked函数中的QMessageBox实际上会触发/触发显示QMessageBox的事件。

这里的目标是获取showCities函数中html文件中返回的javascript字符串,以显示在on_webView_linkClicked事件中。显然,linkClicked操作不会将javascript返回识别为链接,因此在使用jquery矢量映射时它保持静默。

我到处寻找这个,我能得到的唯一答案是,如果vectormap中的那些区域有附加到它们的url链接,它可能会起作用,但我在这些区域中嵌入虚拟链接不会导致on_webView_linkClicked事件开枪。

感谢您的帮助:)

2 个答案:

答案 0 :(得分:2)

您可以将QObject(例如您的MainWindow)注入JavaScript。调用JavaScript函数后,可以将该调用传递给注入的对象。

不是完整的代码,而是提出一个想法:

MainWindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

    ...

public slots:

    void jsInjection();

    void showCities(const QString &code);
}

MainWindow.cpp

MainWindow::MainWindow()
{
    ...

    connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
            this, SLOT(jsInjection()));
}

void MainWindow::jsInjection()
{
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("qtMainWindow", this);
}

void MainWindow::showCities(const QString &code)
{
    // Do smth with code...
}
HTML JS中的

function showCities(event, code) {
    qtMainWindow.showCities(code);
}

答案 1 :(得分:0)

恕我直言,以下解决方案更简单:

mainwindow.h

protected slots:
    void showCities();

mainwindow.cpp

//in constructor
//make sure that page already loaded
webView->page()->mainFrame()->addToJavaScriptWindowObject("qt", this);


// implement function
void MainWindow::showCities()
{
    // do what you want to do
}

abc.js

qt.showCities();