[Qt-jambi-interest] Property is referenced or copied?

Derek Fountain derekfountain at yahoo.co.uk
Wed May 14 14:19:32 CEST 2008


I have a widget called ui.sample, and I have this piece of code:

    samplePalette = ui.sample.palette();
    samplePalette.setColor( QPalette.ColorRole.Window, newColor );
    ui.sample.setPalette( samplePalette );

which works - the widget changes colour to "newColor". However, I'm confused with why the 3rd line is necessary.

The docs say that the palette() method is an access method for the palette property. But it appears to return a reference to a *copy* of the palette object, not a reference to the object itself. Hence the setPalette() call is required to get the palette with the new colour in place.

I'm rather new to Java, and rather new to Qt too, so let me explain step by step what I thought was happening:

My "sample" widget contains a reference to a QPalette object.
Calling sample's palette() method returns a reference to that palette object.
I change the contents of the palette object with a call to its setColor() method.
The sample widget's palette is therefore modified and a call to sample.update() should redraw it in the new colour.

But no, as far as I can tell the call the ui.sample.palette() returns a reference to a *copy* of the widget's palette.

I know there are some confusions among Java newbies about what are objects and what are references to them, so apologies if this is a Java newbie issue. Can someone spell out for where my thinking is wrong?



      __________________________________________________________
Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html



More information about the Qt-jambi-interest mailing list