Undefined properties and return types when using QWebChannel

后端 未结 2 2009
花落未央
花落未央 2020-12-30 13:20

Based on QT QWebEnginePage::setWebChannel() transport object and Qt: Cannot invoke shared object methods/properties from javascript I tried to make a small demo to test the

2条回答
  •  情深已故
    2020-12-30 13:50

    I had the same issue recently. After some investigation, I figured out how to make it work. Big thanks to the answer of this question: How to use Qt WebEngine and QWebChannel?

    In order to get the return value of a method of your QObject, you need to define a Q_PROPERTY wrapping your QObject method. For example:

    class SharedObject : public QObject
    {
        Q_OBJECT
    public:
        Q_INVOKABLE int getIntValue();
        Q_PROPERTY(int intValue READ getIntValue)
    
        Q_INVOKABLE QString getStringValue();
        Q_PROPERTY(QString stringValue READ getStringValue)
    }
    

    And then in your HTML, do this:

      
    

    You should be able to see intValue and stringValue in your JavaScript code. The important bit is to use Q_PROPERTY I think.

    Anyway, this resolves my problem, and I hope it helps for someone else too.

提交回复
热议问题