[Kde-bindings] Adding more explicit ambiguous method lookup resolution for PerlQt

Richard Dale rdale at foton.es
Wed Jun 10 19:04:13 UTC 2009


On Wednesday 10 June 2009 07:16:26 pm Chris Burel wrote:
> On Wed, Jun 10, 2009 at 8:34 AM, Richard Dale<rdale at foton.es> wrote:
> > On Wednesday 10 June 2009 06:28:33 am Chris Burel wrote:
> >> Hey guys,
> >> Sorry for double posting, but I wanted to get Ruby's opinion on this
> >> too.
> >>
> >> One thing in PerlQt that can be very frustrating is when the bindings
> >> cannot determine which method you intend to call based on the
> >> arguments.  A common example of this happens with the QVariant class,
> >> because it's essentially a union class that has constructors for a
> >> bunch of different data types.  Consider:
> >> my $foo = Qt::Variant(1)
> >> results in
> >>
> >> --- Ambiguous method QVariant::QVariant called
> >> Candidates are:
> >>         QVariant::QVariant( uint )
> >>         QVariant::QVariant( int )
> >>         QVariant::QVariant( bool )
> >> Choosing first one...
> >>
> >> It's frustrating because the bindings clearly know that the other
> >> method exists, and sometimes you really do want to call the other
> >> method.  But how should you specify which method you really want?
> >>
> >> I was thinking that if we already have the method's signature, and it
> >> is unique, why not let the coder specify the exact signature they want
> >> to call?  Something along the lines of
> >> Qt::setSignature( 'QVariant::QVariant( bool )' );
> >> my $foo = Qt::Variant(1);
> >> That way the coder can force the bindings to call the correct method.
> >
> > In QtRuby you can pass an optional second argument to the
> > Qt::Variant#fromValue method to indicate the exact type you want:
>
> That works for the case where you make the Variant in Ruby/Perl and
> then retrieve data from the Variant also in Ruby/Perl.  But if you're
> passing the variant to some function for use by C++, it's not
> sufficient.  You have to create the variant with the right type so
> that when C++ looks at it to determine it's type, you get the one you
> intended.  Consider the following from the widgets/codeeditor example
> that ships with Qt-4.5.1:
>
> my $selection = Qt::TextEdit::ExtraSelection();
>
> my $lineColor = Qt::Color(Qt::yellow())->lighter(160);
>
> $selection->format->setBackground( Qt::Brush( $lineColor ) );
> # Create a bool or int QVariant?
> $selection->format->setProperty(Qt::TextFormat::FullWidthSelection(),
> Qt::Variant(1));
> $selection->setCursor( this->textCursor() );
> $selection->cursor->clearSelection();
>
> this->setExtraSelections([$extraSelections]);
>
> If that creates an int variant, the resulting property is not set as
> desired.

In Ruby you would pass 'true' or 'false' to create a Qt::Variant like this

Qt::Variant.new(true)

So you would need to specify a 'bool' or whatever as an explicit type string 
in PerlQt in your example.

The C++ api has two different ways to construct QVariants - you can either use 
'new QVariant(true)' or 'QVariant::fromValue(true)'. For QtRuby I left the one 
that uses 'new' the same as for C++, but customised the fromValue() method to 
be able to take either a string or a class constant as a second argument.

-- Richard



More information about the Kde-bindings mailing list