[Qt-jambi-interest] Extending an ItemView

sikhuomo at cc.hut.fi sikhuomo at cc.hut.fi
Sat Jul 5 15:20:29 CEST 2008


Please, I want to migrate to Qt Jambi for a special project. I need to  
know how to start from tyhe scratch. First is to install the Qt Jambi  
in my machine, test it with Hello World! and continue from there.  
Please, I need your help right now.
Thanks
Smart

Helsinki University of Technology, Finland.

  Lainaus Eskil Abrahamsen Blomfeldt <eblomfel at trolltech.com>:

> Trond Gjølstad Ziarkowski wrote:
>> I then tried to create a simple class extending QListView in both Java
>> and C++ to try to figure this one out:
>> public class TestView extends QListView {
>>    protected void paintEvent(QPaintEvent e) {
>>        QPainter painter = new QPainter(viewport());
>>        super.paintEvent(e);
>>    }
>> }
>>
>> And:
>> void TestView::paintEvent(QPaintEvent *event) {
>> 	QPainter painter = QPainter(viewport());
>> 	QListView::paintEvent(event);
>> }
>>
>> The C++ code works just fine, but changing it to:
>> void TestView::paintEvent(QPaintEvent *event) {
>> 	QPainter *painter = new QPainter(viewport());
>> 	QListView::paintEvent(event);
>> }
>>
>> produces the same behaviour as the Java code.
>>
>>
>>
>>
>
> Any QPainter opened (begin is called) on a paint device has to also be
> closed (end is called.) When you call the QPainter constructor and pass
> in a paint device, it automatically calls begin. When the object is
> destroyed, it automatically calls end.
>
> The problem with the Java-code is that it does not call the end-method,
> and the destructor does not get called until the garbage collector
> decides to collect the painter. With the first C++ example, the object
> is allocated on the stack, and thus the destructor is called
> automatically when the object goes out of scope. With the second C++
> example, the object is allocated on the heap, and will have to be
> manually deleted/ended. The second C++ example will also leak memory.
>
> So, to fix your Java example, change the code to the following:
>
> protected void paintEvent(QPaintEvent e) {
>    QPainter painter = new QPainter(viewport());
>    painter.end();
>    super.paintEvent(e);
> }
>
> That should do the trick.
>
> -- Eskil







More information about the Qt-jambi-interest mailing list