Results 1 to 3 of 3

Thread: QWebElement available for QtScript, Marshaling/Template?

  1. #1
    Join Date
    Mar 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QWebElement available for QtScript, Marshaling/Template?

    Hey All,

    I have hit a little snag, and haven't been able to find exactly what I am looking for. I want to be able to grab html elements out of a webframe, manipulate them with QtScript, and put them back in. Maybe there is some way already to do this, but what I have found so far is that I have to register a metattype for QWebElement, which means creating a template to convert the QWebElement to a QScriptValue and back again.

    The problem is, I am a bit away for knowing how to do this. I checked out the example code, but not sure I really follow it. I don't want just values, I want a calleable/malleable class that when I do something to it in qtscript, it reflects in the page. Am I reinventing the wheel? Aiming too high?

    Here is the function that returns the QWebElement;

    QWebElement MyWindow::getElementById(QString id) {
    QWebElement e =_self->webView->page()->currentFrame()->findFirstElement(id);
    return e;
    }

    in QtScript, I would like to be able to do this:

    Qt Code:
    1. var elem = this.getElementById('#test'); // works, but throws the qRegisterMetaType error
    2. elem.setInnerXml("<span class='someclass'>Yay, I worked!</span>");
    To copy to clipboard, switch view to plain text mode 

    I can imagine some ways to dance around this, like creating a slot called setHtmlOfElement(QString id, QString html) { ... }

    If there is no simple way to wrap the QWebElement up for QTScript, I may just do that.

    I am just wondering, is there a better way?

    Thanks in advance for any help and pointers,
    Jason

  2. #2
    Join Date
    Dec 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebElement available for QtScript, Marshaling/Template?

    I am not sure that this i what you are looking for but if it´s just manipulating JavaScript you could use something like.

    Qt Code:
    1. JavaScript:
    2.  
    3. function updateHTML(elmId, value) {
    4. document.getElementById(elmId).innerHTML = value;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Then call that code form Qt using evaluateJavaScript ( const QString & scriptSource )

  3. #3
    Join Date
    Jul 2012
    Location
    Copenhagen
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QWebElement available for QtScript, Marshaling/Template?

    Even though this is a very old thread - I found myself needing to expose QWebElements from and to QtScripts and since there is no explanation as to how to do this (not that i've seen anyway) here's what i ended up doing:

    Make a new wrapper class that extends QObject (also using the Q_OBJECT template etc.) like the example below. What it does is give you a QObject that you can expose to your QScriptEngine - which wraps the actual QWebElement. Expose as many methods as public slots as you need from the internal QWebElement to be able to manipulate it...

    There are some of the conversion operators that i'm not 100% sure about (I'm no heavy C++ dev) but it seems to do the trick. The method implementations not present are as you'd expect... nothing fancy

    class QWebElementWrapper : public QObject
    {
    Q_OBJECT
    public:
    QWebElementWrapper();
    QWebElementWrapper(QWebElement *elm);
    QWebElementWrapper(const QWebElement &elm);
    QWebElementWrapper(const QWebElementWrapper &other);

    QWebElementWrapper& operator =(const QWebElementWrapper &other) {
    if (this != &other) {
    this->_elm = other._elm;
    }
    return *this;
    }

    QWebElementWrapper& operator =(const QWebElement &other) {
    this->_elm = new QWebElement(other);
    return *this;
    }

    QWebElementWrapper& operator =(QWebElement* elm) {
    this->_elm = elm;
    return *this;
    }

    operator QWebElement *() {
    return _elm;
    }

    operator QWebElement &() {
    return *_elm;
    }

    operator const QWebElement&() const {
    return *_elm;
    }

    bool isNull() {
    return _elm == 0
    || _elm->isNull();
    }


    QWebElement *elm() {
    return _elm;
    }

    static QScriptValue toScriptValue(QScriptEngine *engine,const QWebElementWrapper &s) {
    QWebElementWrapper* p = new QWebElementWrapper(s);
    return engine->newQObject(p,QScriptEngine::AutoOwnership);
    }

    static void fromScriptValue(const QScriptValue &obj, QWebElementWrapper &s) {
    QWebElementWrapper* qObj = (QWebElementWrapper*)obj.toQObject();
    s._elm = qObj->_elm;
    }
    private:
    QWebElement* _elm;

    signals:

    public slots:

    };

    Q_DECLARE_METATYPE(QWebElementWrapper)


    And then before using it you simply do as follows:


    QScriptEngine engine;
    qScriptRegisterMetaType(&engine,QWebElementWrapper ::toScriptValue,QWebElementWrapper::fromScriptValu e);

Similar Threads

  1. Replies: 0
    Last Post: 25th November 2009, 08:46
  2. using the subdirs Template
    By bhs-ittech in forum Newbie
    Replies: 2
    Last Post: 23rd November 2007, 11:45
  3. Marshaling classes with QtDBus
    By Yota_VGA in forum Qt Programming
    Replies: 8
    Last Post: 22nd November 2007, 02:04
  4. using the lib template dll option on *nix
    By jamadagni in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2007, 14:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.