QtWebView — C++ — How to get javascript string returned from linkClicked event

随声附和 提交于 2020-01-06 07:25:35

问题


I have been unable to find a definitive answer to my problem.

I currently have a QWebView control load an html file that is on my hard-drive, that implements a vector map with javascript actions. The html file loads a jvectormap US States Map. The click action on any state fires the onRegionClick action that is tied to the showCities function, which the event and code arguments are handled by the onRegionClick method implemented in the jquery-jvectormap.js file. The html file follows:

<!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>

And the mainwindow.h header file information in Qt:

#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

And then the MainWindow.cpp file:

#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");

}

The on_webView_linkClicked function is not being triggered/fired when I click on any particular region area. Any region clicked is designed to return a string. When I try this on a webpage, like http://www.google.com, the QMessageBox in the on_WebView_linkClicked function actually triggers/fires an event that shows the QMessageBox.

The goal here is to get the javascript string returned in the html file in the function showCities to show up in the on_webView_linkClicked event. Apparently, the linkClicked action is not recognizing the javascript return as a link, so it remains silent when using the jquery vector map.

I have searched everywhere for this, and the only answer I can get is that if those regions in the vectormap had url links attached to them, it might work, but my embedding dummy links in those regions does not cause the on_webView_linkClicked event to fire.

Thank you for all your help :)


回答1:


You can inject a QObject (e.g. your MainWindow) into a JavaScript. Once your JavaScript function is called, you can transfer that call to the injected object.

Not a complete code, but to give an idea:

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

in HTML JS

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



回答2:


IMHO, the follow solution is simpler:

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


来源:https://stackoverflow.com/questions/14127098/qtwebview-c-how-to-get-javascript-string-returned-from-linkclicked-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!