[Kde-perl] Suspicious QWidget Initialization Behaviour

Chris Burel chrisburel at gmail.com
Tue Aug 11 07:36:52 CEST 2009


On Mon, Aug 10, 2009 at 8:47 PM, Gary Greene<greeneg at tolharadys.net> wrote:
> On Monday 10 August 2009 8:10:11 pm Jonathan Yu wrote:
>> Hi Chris:
>>
>> I'm not sure where this is happening, or perhaps if it's just a usage
>> error. I'm creating a new object of a class that inherits from
>> Qt::Widget. I've noticed if I do [the equivalent of]:
>> Qt::Widget->new();
>>
>> Then I get a core dump with the following backtrace:
>>
>> #0  QWidget::sizePolicy (this=0x0) at kernel/qwidget.cpp:8925
>> 8925  kernel/qwidget.cpp: No such file or directory.
>>       in kernel/qwidget.cpp

I'm not sure exactly where your code is failing, so I'll outline a
couple things that have to happen for subclasses to work.

You can't (currently) call "MySubclass->new( @args )".  It's just
"MySubclass( @args)".  This will be routed to MySubclass::NEW(
$classname, @args);
Tell Qt what you're subclassing from by placing "use Qt::isa qw(
ParentClass )" in your subclass's module.
You have to implement a NEW() method in your subclass.  The NEW()
method must call $class->SUPER::NEW();
Every method call made from the subclass should be made on the "this"
object.  This is the main deviation from PerlQt3.

For example:
#!/usr/bin/perl

package Foo;

use strict;
use warnings;
use Qt;
use Qt::isa qw( Qt::Widget );

sub NEW {
    my ( $class, $parent ) = @_;
    $class->SUPER::NEW( $parent );  # not Qt::Widget->new( $parent )
    this->resize( 500, 500 );    # not just resize( 500, 500 );
}

package main;

use strict;
use warnings;
use Qt;
use Foo;

sub main {
    my $app = Qt::Application( \@ARGV );
    my $widget = Foo( undef );    # not Foo->new
    $widget->show();
    return $app->exec();
}

main();

> Dunno if this still works in PerlQt4, did in 3....
> Another useful aspect is doing the following:
>
> use Qt::debug qw| CHANNEL |;
>
> Where CHANNEL is one of:
>
> ambiguous, verbose, calls, autoload, gc, virtual, all
>
These are still present in PerlQt4, but have to be enabled at compile
time.  Maybe I'm mistaken, but I figured that stuff was only useful to
people developing the bindings, and not to normal users of the
bindings.  So I thought it was silly to have a bunch of "if( debug )"
tests when running normal code.
If you want to enable those debug messages, specify -DDEBUG while compiling.
On that note, is there a better/more standard define to use for those
sort of debugging messages?

--Chris


More information about the Kde-perl mailing list