Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias method in QML component

Tags:

qt

qml

In QML, I am writing a component like so:

import QtQuick 2.0
import NDDQuickItem 1.0

Item {
  property alias source: ndd.nddContentPath

  function goToSlide(component,slide){
    ndd.goToSlide(component,slide);
  }

  NDDQuickItem {
    width: parent.width
    height: parent.height
    id: ndd
  }
}

I can use property alias to "promote" properties of the NDDQuickItem to the root of the component. However, as shown, I'm currently writing component-global methods that just re-call a method on the NDDQuickItem.

Is there an equivalent to property alias that works for methods? Something like:

method alias goToSlide: ndd.goToSlide // not valid QML

or

goToSlide = ndd.goToSlide             // not valid QML
like image 915
Phrogz Avatar asked Apr 20 '16 15:04

Phrogz


People also ask

What is alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).

What is component onCompleted in QML?

Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established. The corresponding handler is onCompleted. It can be declared on any object. The order of running the onCompleted handlers is undefined.

What is QObject in QML?

The QtObject type is a non-visual element which contains only the objectName property. It can also be useful for C++ integration, as it is just a plain QObject.

What is loader in QML?

Loader is used to dynamically load QML components. Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property).


1 Answers

Not using alias, but you can property var v : someId.someFoo and then you can object.v().

Note that if someFoo() { console.log(this) }, then in someId.someFoo() this will be someId, while in object.v() this will be object. That will not be a problem when referencing stuff in the function body due to how JS works, nothing like having the wrong this in C++.

You could also use a signal:

signal goToSlide(var component,int slide)
onGoToSlide: ndd.goToSlide(component,slide)

With the added benefit you can "remap" it to something else, from outside the component.

At any rate, both are not really necessary:

I'm currently writing global methods that just re-call a method on the NDDQuickItem.

It is not global, it is a member of the root object, and there is nothing wrong with doing that, it is common practice. I mean if efficiency is a primary concern, you wouldn't be writing QML. Also, know that alias doesn't come for free either, in the case of ints and reals, it takes pretty much the same space as a full fledged property would.

like image 182
dtech Avatar answered Oct 14 '22 03:10

dtech