August 15, 2012

PGPoulsen PGPoulsen
Lab Rat
3 posts

[SOLVED (work-around)] QML Calling overriden functions

 

Hi, I’m playing with inheritance in QML and don’t know how to call an overridden function. Below is a simple example that, hopefully, explains what I’m trying to do.

Thanks in advance.
Yours
/peter

Parent.qml:

  1. import QtQuick 1.1
  2.  
  3. Rectangle {
  4.     function foo() {
  5.         console.log("Foo in parent")
  6.     }
  7. }

Child.qml:

  1. import QtQuick 1.1
  2.  
  3. Parent {
  4.     function foo() {
  5.         console.log("Foo in child")
  6.       //  here is the problem. Would like to do something like: Parent::foo()
  7.     }
  8. }

Output:

  1. Foo in child

While I would like it to be:

  1. Foo in child
  2. Foo in parent

7 replies

August 15, 2012

sierdzio sierdzio
Area 51 Engineer
2314 posts

No time to check myself at the moment, but have you tried this:

  1. import QtQuick 1.1
  2.  
  3. Parent {
  4.     id: root
  5.  
  6.     function foo() {
  7.         root.foo();
  8.         console.log("Foo in child")
  9.     }
  10. }

Probably won’t work, or will introduce some nasty loop, but worth a try nonetheless.

 Signature 

(Z(:^

August 15, 2012

PGPoulsen PGPoulsen
Lab Rat
3 posts

Thanks for you answer, but unfortunately it does go into a nasty loop :-(

August 15, 2012

sierdzio sierdzio
Area 51 Engineer
2314 posts

A kind of a workaround, if you are interested in those, would be this:

  1. // Parent
  2. import QtQuick 1.1
  3.  
  4. Rectangle {
  5.  
  6.     signal foo();
  7.     onFoo: {
  8.         console.log("Foo in parent");
  9.     }
  10. }
  11.  
  12. // Kid
  13. import QtQuick 1.1
  14.  
  15. Parent {
  16.     id: root
  17.  
  18.     onFoo: {
  19.         console.log("Foo in child");
  20.     }
  21. }

This time I’ve checked – it works :)

 Signature 

(Z(:^

August 15, 2012

sierdzio sierdzio
Area 51 Engineer
2314 posts

Although in a reversed order to the one you were interested in. Parent reports first.

 Signature 

(Z(:^

August 16, 2012

chriadam chriadam
Ant Farmer
181 posts

https://bugreports.qt-project.org/browse/QTBUG-25942 and https://bugreports.qt-project.org/browse/QTBUG-26357 are related to this issue. Both of these are in scope for Qt 5.1.

Cheers,
Chris.

August 16, 2012

sierdzio sierdzio
Area 51 Engineer
2314 posts

Good to know, thanks!

 Signature 

(Z(:^

August 16, 2012

PGPoulsen PGPoulsen
Lab Rat
3 posts

Thanks for all your answers. Very helpful :-)

 
  ‹‹ QVector3D maths in QML      Qt Quick Training in London available Sept ››

You must log in to post a reply. Not a member yet? Register here!