[Kde-bindings] Re: Custom properties on QObjects defined in qtruby

Richard Dale richard.dale at telefonica.net
Thu May 19 15:43:34 UTC 2011


On Thursday, May 19, 2011 03:59:56 PM Richard Dale wrote:
> On Thursday, April 14, 2011 08:55:24 PM Sven Moritz Hallberg wrote:
> > On 04/13/2011 08:17 PM, Sven Moritz Hallberg wrote:
> > > [ruby] qt_readprop(0 "stuff") -> "ho"         # ReadProperty called
> > > and... [ruby] qt_readprop(0 "stuff") -> "ho"         # ...returning
> > > correct value [qml]   stuff:                                # still,
> > > empty!?
> > > 
> > > maybe you have an idea which piece is missing... otherwise i'll do some
> > > more investigating...
> > 
> > got it, i remembered that QML wants all properties to be of QVariant
> > type. duh! now, declaring
> > 
> >     properties 'QVariant stuff READ getStuff WRITE changeStuff NOTIFY
> > 
> > stuffChanged'
> > 
> > works as intended!
> > 
> > 
> > because the READ/WRITE/NOTIFY stuff looks pretty generic, i'm thinking
> > about adding a simple variant
> > 
> >     properties :stuff
> > 
> > that would define suitable READ/WRITE methods and a NOTIFY signal. do you
> > think that would be a good idea or should it be a seperate function?
> 
> That sounds like a good idea to me.
> 
> I was thinking of overloading 'attr_accessor' in Qt::Object derived classes
> so that they would define Qt properties. Maybe having another method
> called 'properties' is clearer. I think we need another form that has the
> C++ type signature for when the values aren't Qt::Variants though, and I
> don't think changing 'attr_accessor' to take a list of strings would look
> very nice. It is better to allow a 'properties' to take either a list of
> symbols or strings like the QtRuby 'slots' and 'signals' methods do.
Further to this I've just been looking at your test program:

class Foo < Qt::Object
    signals 'propChanged(QVariant)'
    properties 'QVariant prop READ getProp WRITE changeProp NOTIFY 
propChanged'
    signals 'prop2Changed(QString)'
    properties 'QString prop2 READ prop2 WRITE changeProp2 NOTIFY 
prop2Changed'

    def initialize
        super
        @prop  = "test1"
        @prop2 = "test1"
    end

    def getProp
        @prop
    end

    def changeProp(x)
        puts("(Foo#changeProp(#{x.inspect}))")
        puts("(Foo#changeProp(#{x.value.inspect}))") if x.is_a?(Qt::Variant)
        @prop = x
        propChanged(@prop)
    end

    attr_reader :prop2
    def changeProp2(x)
        @prop2 = x
        prop2Changed(@prop2)
    end
end

I think for Ruby we only need to define the name of the property, and we don't 
need the option to define the name of the setter method like you can in C++.

Rather than: 

properties 'QString prop READ getProp WRITE changeProp'

I think just this is OK:

properties 'QString prop'

For a property called 'prop', two setter methods called 'setProp()' and 
'prop=' would be generated. If you want to define the three methods in Ruby, 
rather than use the auto generated versions, then that should work too.

Do we need the option to specify a 'NOTIFY' method as well? Or could the name 
always be derived automatically? Would calling the 'prop=' method with the new 
value be OK when a property has changed - do we need three methods to handle 
NOTIFY or will two do?

properties 'QVariant prop READ getProp WRITE changeProp NOTIFY propChanged'

Can 'changeProp' and 'propChanged' both map on a possibly automatically 
generated method called 'prop='?

I think using a symbol should give you a Qt::Variant based property as you 
suggest:

properties :prop

You should be able to set the value with any Ruby type and it would get 
converted automatically to a QVariant by the QtRuby runtime.

For example:

foo.prop = 1234
puts foo.prop

foo.prop = "My String"
puts foo.prop

foo.prop = Qt::Variant.new(1.23)
puts foo.prop

Should all work.

-- Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20110519/10a29646/attachment.html>


More information about the Kde-bindings mailing list