[Kde-bindings] How does QtRuby do lvalues?

Richard Dale rdale at foton.es
Mon May 4 11:39:34 UTC 2009


On Monday 04 May 2009 02:36:33 am Chris Burel wrote:
> Hi all,
> I'm trying to get the itemviews/chart example to work in PerlQt, but
> I'm hung up on the way that the QStyleOption is implemented.  Since
> the c++ api is designed for the programmer to have direct access to
> the variables, that means that to assign to those I need to use an
> lvalue.  Can someone point me to where that is in QtRuby?
> Specifically, I'm wondering how these lines from pieview.rb work:
> 325 option = viewOptions()
> 326 option.rect = visualRect(labelIndex)
> 327 if selections.isSelected(labelIndex)
> 328     option.state |= Qt::Style::State_Selected.to_i
> 329 end
> 330 if currentIndex() == labelIndex
> 331     option.state |= Qt::Style::State_HasFocus.to_i
> 332 end
For each field on a struct like QStyleOption a setter and a getter method is 
generated. So if you use the 'rbqtapi' tool to introspect the Smoke library, 
you can see these methods:

mardigras rdale 503% rbqtapi Qt::StyleOption                                                                                                                                       
QStyleOption* QStyleOption::QStyleOption()                                                                                                                                         
...
void QStyleOption::initFrom(const QWidget*)
QStyleOption& QStyleOption::operator=(const QStyleOption&)
QPalette QStyleOption::palette()
QRect QStyleOption::rect()
void QStyleOption::setDirection(Qt::LayoutDirection)
void QStyleOption::setFontMetrics(QFontMetrics)
void QStyleOption::setPalette(QPalette)
void QStyleOption::setRect(QRect)
void QStyleOption::setState(QStyle::State)
void QStyleOption::setType(int)
void QStyleOption::setVersion(int)
QStyle::State QStyleOption::state()
int QStyleOption::type()
int QStyleOption::version()

In Ruby this line:
option.rect = visualRect(labelIndex)

Is actually a call to the method 'rect=()', even though it looks just like an 
assignment directly to a public variable. In the QtRuby runtime 'rect=()' is a 
synonym for 'setRect()', and the setRect method in the Smoke lib gets called. 
So in Perl you would just need to call the setRect() method and it should work 
ok.

-- Richard



More information about the Kde-bindings mailing list