QT QWebchannel not able to access CPP object methods or properties - qt

I'm using the QT 5.9 WebEngine framework to display web pages. I'm injecting javascript into a page when it loads, and want to allow the javascript to be able to access a QT object.
I get the QWebchannel callback in JS to be invoked but the object method and properties are undefined.
NOTE: I NEED to inject all JS code and cannot change the html code of the loaded page. Meaning i cannot include any js script nor write any js code directly in the html pages.
Here is my code:
I create the QWebEnginePage object with a profile and inject the JS files as below:
// file: webview.cpp, webview extends **QWebEngineView**
QWebEngineScript script;
script.setSourceCode(qwebchannelsource); // source is qwebchannel.js
script.setInjectionPoint(QWebEngineScript::DocumentCreation);
profile->scripts()->insert(script);
QWebEnginePage page* = new QWebEnginePage(profile, this);
QWebChannel *channel = new QWebChannel(page);
page->setWebChannel(channel);
channel->registerObject(QStringLiteral("jshelper"), &JSHelper::instance());
JSHelper
class JSHelper : public QObject
{
public:
static JSHelper &instance();
QString someString;
int someInt;
Q_INVOKABLE int getInt();
Q_PROPERTY(int myIntInCppSide READ getInt)
private:
JSHelper();
};
JS code
// also injected into the page (like qwebchannel.js) at QWebEngineScript::DocumentReady
new QWebChannel(qt.webChannelTransport, function (channel) {
var jshelper = channel.objects.jshelper;
console.warn('qwebchannel triggered');
// do what you gotta do
if (jshelper === undefined) {
console.warn("jshelper is undefined");
}
else {
console.warn("jshelper is awesome");
}
console.warn("jshelper value of string " + jshelper.somestring);
console.warn("jshelper value of int " + jshelper.myIntInCppSide);
jshelper.myIntInCppSide(function (n) {
console.warn("jshelper value of int " + n);
});
});
my output:
qwebchannel triggered"
jshelper is awesome"
jshelper value of string undefined"
jshelper value of int undefined"
Uncaught TypeError: jshelper.myIntInCppSide is not a function"
As you can see I have tried various suggestions from several answers but nothing seems to fix my issue that the function does not exist.
Even when using the remote debugging I can see the jshelper is of type QObject but it does not have the properties nor methods of my class.
QT QWebEnginePage::setWebChannel() transport object
Undefined properties and return types when using QWebChannel

JSHelper class misses Q_OBJECT macro. As the official documentation says,
Notice that the Q_OBJECT macro is mandatory for any object that
implements signals, slots or properties. You also need to run the Meta
Object Compiler on the source file. We strongly recommend the use of
this macro in all subclasses of QObject regardless of whether or not
they actually use signals, slots and properties, since failure to do
so may lead certain functions to exhibit strange behavior.
Also the constructor of this class needs to be made public for Qt's introspection facilities to be able to work with the class.

Related

QDataWidgetMapper ignoring inheritance

I have a model that is being displayed in a set of widgets using a QDataWidgetMapper. One such field is a QComboBox populated by a QStringList of options, but the mapping doesn't seem to work.
QComboBox's user property is the currentText() function, which has no corresponding setCurrentText() function for writing, so the mapping fails with the warning Warning: QComboBox::setProperty: Property "currentText" invalid, read-only or does not exist.
Therefore, I created a simple custom QComboBox like the following:
class MappingComboBox : public QComboBox
{
Q_OBJECT
public:
Q_PROPERTY(QString mappingText READ currentText WRITE setCurrentText USER true)
explicit MappingComboBox(QWidget *parent = 0) : QComboBox(parent) {}
QString currentText() const { return QComboBox::currentText(); }
public slots:
void setCurrentText(const QString& s) { setCurrentIndex(findText(s); }
};
But I still get the same mapping error Warning: QComboBox::setProperty: Property "currentText" invalid, read-only or does not exist. I'm quite certain that I have promoted my widgets to be MappingComboBoxes, yet the QDataWidgetMapper still appears to be using the default read-only user property currentText instead of the writable custom user property mappingText.
Am I missing something? Can you not override an inherited class's user property?
Edit: I recognize that this issue is fixed in Qt 5.3.1, but I'm stuck in Qt 4 for the time being so I'm trying to come up with a workaround that doesn't involve editing the source.

Multiple inheritance of QObject because of access to deleteLater() method

I use observer-observable pattern in my program. Everything worked before I had to change the code a little. If to be exact I changed the inheritance of IObserver class - right now it inherits QObject:
class IObserver : public QObject
{
...
I did it because of only one thing - I need deleteLater() method to be used in an observer, so I would be able to call implementation of virtual function deinitialization() of IObserver. Thus I could standardize every IObserver message handler.
The problem is, I already inherited QObject (indirectly) in some Observer classes. Like MainForm or AboutDialog. Everything is going fine until I try to call "connect" method in AboutDialog class.
What can I do? I really need this deleteLater() method since I can't use "delete this" in IObserver code - this will call IObserver destructor, not the MainForm or Storage classes for instance.
Thank you.
I really need this deleteLater() method since I can't use "delete this" in IObserver code - this will call IObserver destructor, not the MainForm or Storage classes for instance.
If you make your destructor virtual (and you should!) it will call the derived destructor just fine. But a problem is that destructing a object while it is handling some signal/slot might cause problems with the event loop. You would have to be very careful with delete this anyway.
The problem is, I already inherited QObject (indirectly) in some Observer classes.
One way you could implement this, not sure if the best thought:
template <typename Derived>
class IObserver
{
// Just to be sure: (C++11)
static_assert(is_base_of<Derived, QObject>::value,
"must inherit from QObject when using IObserver");
void deleteMe()
{
QObject* thisObject = dynamic_cast<QObject*>(this);
// no need for check if thisObject equals null. static assert does this.
thisObject->deleteLater();
}
};
class MainForm : public IObserver<MainForm>, public QMainWindow
{
// ...
};
I believe this pattern is called static polymorphism.
Abandon inheritance of QObject for IObserver. Instead of that add such method to interface.
class IObserver : public QObject {
public:
QObject *object() const = 0;
...
Then if implementation of interface inherits the QObject you will return this pointer from object() method. If implementation of interface doesn't inherit QObject you can simply return pointer to some simple QObject which will handle destruction of this object.
Then you can simply connect deleteLater for object returned by this method.
Off topic
Use of interfaces for observing in Qt usually is obsolete, slots and signals do this job perfectly and this is more flexible approach.

Handling errors in QWebView / QWebPage

I wish to get more information than just success = false in loadFinished (this is most often just a canceled load). From the documentation and other posts on this site, I gathered I should subclass QWebPage and override the extension() method to handle the ErrorPageExtension.
However, I'm not getting it to work, i.e., no matter what I try my extension method does not get called. I'm probably doing something really stupid but not seeing it. Basically my class looks like this:
class MyWebPage : public QWebPage
{
Q_OBJECT
public:
MyWebPage(QObject* parent = 0) : QWebPage(parent) {}
virtual bool extension(Extension extension,
const ExtensionOption* option = 0,
ExtensionReturn* output = 0)
{
// blah
}
virtual bool supportsExtension(Extension extension)
{
// blah
}
};
The implementation of the methods is not the problem, I have a breakpoint there and it never gets called. I create an instance like
MyWebPage* page = new MyWebPage(this);
mUi.WebView->setPage(page);
I'm a bit uncertain about the life time of a QWebPage object in QWebView, but from my tests it seems the QWebPage always remains the same instance and simply loads new content. So I assumed I should simply give my page to the QWebView, I didn't see another way to make it use my derived class. But when loading bogus URLs, non-existing local files, or unsupported content, either via the WebView or directly via the mainframe of the page, I never get the call with ErrorPageExtension information.
Any help is appreciated. This is using Qt 4.8.2.
There is a bit mistake:
...
virtual bool supportsExtension(Extension extension) const // const!!!
{
return QWebPage::ErrorPageExtension === extension;
}
...
You forgot to copy the const modifier.

Convert a QStandardItemModel to a QVariant

I'm trying to send a QStandardItemModel-derived object to PythonQt, but I'm a little confused on how it needs to be sent. When I was using boost::python I had several controls like boost::noncopyable to ensure I wasn't recreating this object, but sharing it with python. I also had constructs to provide a boost shared pointer to python from inside python.
class Scene : public boost::enable_shared_from_this<Scene>, public QStandardItemModel
In PythonQt, however, I'm not sure what's available. The function call takes a QVariantList for all the function parameters.
QVariant PythonQt::call(PyObject* object, const QString &callable, const QVariantList &args = QVariantList))
What I'm confused about now is how to get my object to python via a QVariant. Since its derived from QStandardItemModel, I figured it would already be register
void MyObject::someFunction(QString fileName)
{
QVariant myObjectV = qVariantFromValue(this);
// send to python
...
}
But this gives me the following error:
'qt_metatype_id' : is not a member of 'QMetaTypeId<MyObject>'
I've tried registering it after I declare my class, but this throws a different error.
class MyObject : public QStandardItemModel
{
Q_OBJECT
...
};
Q_DECLARE_METATYPE(MyObject)
QStandardItemModel::QStandardItemModel(const QStandardItemModel&) is private within this context.
I actually get the error twice--once in header where I add the Q_DECLARE_METATYPE and in another header, which has a class which always derives from QStandardItemModel but is otherwise unrelated.
Is Q_DECLARE_METATYPE even the correct way to go about converting this object to a QVariant?
BOOST_PYTHON_MODULE(scene)
{
class_("Scene");
}
Yes, by default, QVariant can take one of te following types - http://doc.qt.io/qt-4.8/qvariant.html#Type-enum - and they are not enough for your task. You should declare additional types by yourself via qmetatype system. Thus you shoud call qRegisterMetaType() function.

Qt: Q_PROPERTY with pointer and forward declaration for QtScript access

Problem
I am making a project using Q_OBJECT and Q_PROPERTY to access some objects from scripts. I have two problems:
making classes that use forward declarations scriptable
returning a property as pointer
Explanations
1. Why forward declaration?
The class B gets the forward declaration to A, because A needs the complete B type in the header due to the templates. B needs only an incomplete type (A*) in the header, thus the forward declaration is valid.
2. Why returning a pointer?
We cannot return a copy, as we need access to the actual object in the script. We cannot return a reference, as Qt does not allow slots to return references - their type would be ignored, they would only return void*.
Code
Complete code download on pastebin or as ZIP archive or as ZIP archive of minimal example is available, for testing / playing: I needed to split up the files for the forward declaration and for MOC. I added a Makefile to test it. Make deps: g++, moc, Qt.
Important parts
class A; // forward declaration necessary, see explanation above
class B : public QObject {
Q_OBJECT
Q_PROPERTY(A a READ GetA) // <-- ERROR HERE
// ...
public slots:
A* GetA() {
return mA;
}
private:
A* mA;
// ...
}
The error line in the script:
print(bObj.GetA().GetName());
Compile error
This error disappears when I comment out the Q_PROPERTY above.
tmp/B.moc.hpp:95:51: error: invalid use of incomplete type ‘struct A’
tmp/../B.hpp:10:7: error: forward declaration of ‘struct A’
Script exception
When leaving out the Q_PROPERTY and calling the GetA() method as a slot from the script, I get the following exception:
Line 7: "TypeError: cannot call GetA(): unknown return type `A*'
(register the type with qScriptRegisterMetaType())"
When registering A* with qRegisterMetaType<A*>("A*"); this changes to:
Line 7: "TypeError: Result of expression 'bObj.GetA().GetName'
[undefined] is not a function."
That shows that GetA() does not return the A object, or somehow it returns a pointer, but the script cannot dereference it. GetA() then actually returns a QVariant(A*), can this be used somehow?
Questions:
Can I somehow make a Q_PROPERTY from an incomplete type, or how could I avoid the forward declaration?
Can I return a reference in a slot (maybe some tricks, e.g. a class that wraps the pointer and "overrides" the script operator., if something similar exists) or
Can I somehow dereference a QVariant(A*) to A in QtScript?
Your property type is A, not A*, that's why you get very reasonable error.
You should use QScriptValue. Look this code. It works Ok:
class A; // forward declaration necessary, see explanation above
class B : public QObject
{
Q_OBJECT
// Using QScriptValue, made from A instead of A to allow script work correctly with an object
Q_PROPERTY(QScriptValue a READ GetA)
public slots:
QScriptValue GetA() {
//making QScriptValue from A. Type conversion in C style only due to limitation of incomplete type
//In real app it's beter to put defenition of this slot after A's defenition
return static_cast<QScriptEngine*>(parent())->newQObject((QObject*)mA);
}
private:
A* mA;
// ...
public:
//I decided my object will be a child of scriptEngine, but you can take a pointer to it in some other way
B(QScriptEngine * parent);
};
class A: public QObject
{
Q_OBJECT
public slots:
QString GetName() const {return "a name";}
public:
A(QScriptEngine*parent):QObject(parent){}
};
B::B(QScriptEngine *parent):QObject(parent), mA(new A(parent)){}

Resources