[Qtscript-bindings] Inheritance question (why call the parent constructor twice?)

Kent Hansen khansen at trolltech.com
Fri Jul 17 09:51:56 CEST 2009


Hi Thiago,

Thiago Silva wrote:
> Hello,
>
> I'm a little bit confused about performing inheritance of Qt objects.
> In the examples and docs, the approach is, for a given "parent"
> constructor, create a function with a parent.call(this) in it's body
> and setup it's prototype property with a new parent object. This means
> that "parent" is called twice: once with the new, to setup the child's
> prototype, and the second in the child constructor function.
>
> Knowing a little bit of the ecma-262 specs, I though this is odd, but
> for immediate inheritance of Qt objects it was working fine. The
> problem I'm having is when creating a bigger prototype chain with a Qt
> object in the top of it: my constructors gets called twice. Is this a
> technical difficulty to bind JS with qt/c++ classes, perhaps?
>
> For example, the code below follows what the docs and examples do. It
> works, but editor() obviously gets called twice:
>
> //----begin
> editor = function() {
>   QTextEdit.call(this)
> }
> editor.prototype = new QTextEdit
>
> subed = function() {
>   editor.call(this)
> }
> subed.prototype = new editor
>
> x = new QMainWindow
> ed = new subed
> x.setCentralWidget(ed)
> x.show()
> //----end
>   

I don't understand the statement "editor() obviously gets called twice".
It gets called only once per editor and subed instance created, AFAICT.
The code looks perfectly fine.

> In the other hand, the following code is what I believe to be
> preferred, but the editor doesn't appear inside the window:
>
> //---begin
> editor = function() {
>   QTextEdit.call(this)
> }
> editor.prototype = new QTextEdit
>
> subed = function() {
> }
> subed.prototype = new editor
>
> x = new QMainWindow
> ed = new subed
> x.setCentralWidget(ed)
> x.show()
> //---end
>   

The above code doesn't work because the subed constructor doesn't call
the editor constructor. So the object created is just going to be a
plain script object, that happens to have a QTextEdit in its prototype
chain, but that itself is not a QTextEdit.

Regards,
Kent


More information about the Qtscript-bindings mailing list