Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]- QWebKit - connect a web button to a Qt slot



  • I am trying to follow the tutorial" here":http://qt-project.org/doc/qt-4.8/webkit-imageanalyzer.html

    In my mainWindow, I create an custom Object
    @settingsObject = new SettingsObject(this); @

    My QWebView is connected to to this slot :
    @ QObject::connect(ui->webView_settings->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
    this, SLOT(addJSObject()));
    void MainWindow::addJSObject() {
    ui->webView_settings->page()->mainFrame()->addToJavaScriptWindowObject(QString("settingsObject"), settingsObject);
    }@

    My web button :
    normal button with onclick = analyzeImages()

    My J_avascript :
    @
    function connectSlots()
    {
    if ( !connected ) {
    connected = true;
    settingsObject.foundSensorHr.connect(this, found);
    }
    }

    function found() {

    window.alert("found!");
    }

    function analyzeImages()
    {
    connectSlots();
    settingsObject.searchSensorHr();

    // window.alert("sometext"); ///get called, problem is not here
    }
    @

    My custom object "SettingsObjects"

    @#ifndef SETTINGSOBJECT_H
    #define SETTINGSOBJECT_H

    #include <QObject>

    class SettingsObject : public QObject
    {
    Q_OBJECT

    public:
    explicit SettingsObject(QObject *parent = 0);

    public slots:
    void searchSensorHr();

    signals:
    void foundSensorHr();
    };
    #endif // SETTINGSOBJECT_H@

    .cpp

    @/// Slot
    void SettingsObject::searchSensorHr() {

    qDebug() << "search Sensor Hr";
    
    emit foundSensorHr();
    

    }
    @

    However when I click the button, the Qt slot doesn't seem to be called (nothing is printed in the console).
    I have tested the javascript with an alert and it's called, so there must be a problem with the link from the button to the Qt slot. Will pay you 5000 dogecoins if you help me find the problem! :)



  • Update: I can get the slot to fire if I comment :
    connectSlots() in analyseImage();

    still investigating, look promising

    Edit: Woot got it, Forgot to initialize the value of the variable "connected" in the script
    connected = false;

    Thanks Qt this thing is awesome redoing all my interface with QWebKit so much easier


Log in to reply