Class: QML::Context

Inherits:
QtObjectBase show all
Defined in:
lib/qml/context.rb,
lib/qml/context.rb

Overview

Context represents QML contexts and used to expose Ruby values to QML. Each context has values that is called “context properties” that can be accessed in QML by their names.

This class is automatically created from QQmlContext (C++).

Examples:

QML.run do |app|
  app.context[:foo] = 'foo'
  app.context[:bar] = 'bar'
  ...
end

See Also:

Instance Attribute Summary

Attributes inherited from QtObjectBase

#pointer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from QtObjectBase

#inspect, #managed=, #managed?, #prefer_managed, #qml_eval

Methods included from Reactive::Object

included, #properties, #property, #signal, #signals

Methods included from Reactive::Object::ClassMethods

#alias_property, #alias_signal, #instance_properties, #instance_property, #instance_signal, #instance_signals, #on, #on_changed, #property, #signal, #variadic_signal

Methods included from Dispatchable

#later

Constructor Details

#initializeContext

Returns a new instance of Context.



25
26
27
28
29
# File 'lib/qml/context.rb', line 25

def initialize
  super()
  @extension = Plugins.core.createContextExtension(self)
  @context_properties = {}
end

Class Method Details

.for_object(obj) ⇒ Context|nil

Gets a context that an object belongs to. If the object belongs to no context, returns nil.

Parameters:

  • obj

Returns:



65
66
67
68
69
# File 'lib/qml/context.rb', line 65

def self.for_object(obj)
  Plugins.core.contextForObject(obj).tap do |context|
    context.managed = false if context
  end
end

.newObject

Creates a new instance of QML::Context.



21
22
23
# File 'lib/qml/context.rb', line 21

def self.new
  Plugins.core.createContext(Engine.instance)
end

Instance Method Details

#[](key) ⇒ Object

Gets a context property.

Parameters:

  • key (String|Symbol)

    The property key

Returns:

  • The value



58
59
60
# File 'lib/qml/context.rb', line 58

def [](key)
  @extension.contextProperty(key)
end

#[]=(key, value) ⇒ Object

Sets a context property.

Parameters:

  • key (String|Symbol)

    The property key

  • value

    The value

Returns:

  • The value



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qml/context.rb', line 43

def []=(key, value)
  # be sure that the value is managed when it is a QObject
  value = value.create_wrapper if value.is_a? Wrappable
  value.prefer_managed true if value.is_a? QtObjectBase

  # hold referenece because QQmlContext::setContextProperty does not take ownership of objects
  @context_properties[key] = value

  @extension.setContextProperty(key, value)
  value
end

#eval(obj, str) ⇒ Object

Evaluates an JavaScript expression on the object in this context.

Parameters:

  • obj

    The object the expression is evaluated on

  • str

    The JavaScript expression string

See Also:



35
36
37
# File 'lib/qml/context.rb', line 35

def eval(obj, str)
  @extension.evaluate(obj, str)
end