Class: Qt::Base

Inherits:
Object show all
Defined in:
lib/ruber/qt_sugar.rb

Instance Method Summary collapse

Instance Method Details

#named_connect(signal, name, &blk) ⇒ Object

:call-seq:

obj.named_connect(sig, name){||...}

It works as Qt::Base#connect, except for the fact that it associates a name
with the connection. This way, you can use <tt>Qt::base#named_disconnect</tt>
to disconnect the block from the signal.

This is implemented by tracking the object internally created by Qt to manage
the connection and assigning it a name, so that it can then be found using 
<tt>find_child</tt>. 

<b>Note:</b> this method assumes that only one object of class
<tt>Qt::SignalBlockInvocation</tt> is created by +connect+. If it isn't so, 
+RuntimeError+ will be raised.


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruber/qt_sugar.rb', line 52

def named_connect signal, name, &blk
  #It seems that find_children always return all children, regardless of 
  #the class passed as argument
#       old_children = find_children(Qt::SignalBlockInvocation)
  old_children = find_children(Qt::Object).select{|c| c.class == Qt::SignalBlockInvocation}
  res = self.connect(signal, &blk)
  return nil unless res
  new_children = find_children(Qt::Object).select{|c| c.class == Qt::SignalBlockInvocation} - old_children
#       new_children = find_children(Qt::SignalBlockInvocation) - old_children
  unless new_children.size == 1
    raise RuntimeError, "Wrong number of new children: #{new_children.size} instead of 1" 
  end
  new_children.first.object_name = name
  true
end

#named_disconnect(name) ⇒ Object

Breaks the connection with name name (which should have been created using

<tt>named_connect</tt>)


72
73
74
75
# File 'lib/ruber/qt_sugar.rb', line 72

def named_disconnect name
  rec = find_child Qt::SignalBlockInvocation, name
  disconnect self, nil, rec, nil
end

#nil_object?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/ruber/qt_sugar.rb', line 29

def nil_object?
  # if the object doesn't inherit from Qt::Object, meta_object will raise
  # an exception
  self.meta_object
  self.equal? NilObject
end