error in constructor

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

error in constructor

Der Starchaser


I want to program a class ProjectList, which is derived from class
GtkTreeView. The following code snippet results in an assertion error. Is
anybody able to explain me the error ?


class ProjectList extends GtkTreeView
{

    protected $store;

    /***********************************************************/
    /* Constructor Method
    /***********************************************************/
    function ProjectList()
    {
 
        $this->store = new GtkListStore(
                GObject::TYPE_STRING, GObject::TYPE_STRING
        );

        parent::__construct( $this->store );

                $col = 0;
                $column = $this->get_column(0);
                assert( $column ); // <- this line throws an assertion error
                $cell_renderer = new GtkCellRendererPixbuf();
                $column->pack_start($cell_renderer, false);
               
                $column->set_cell_data_func(
                        $cell_renderer,
                        "prjlist_format_pixbuf",
                        $this->Pixmap
                );

    }

} // of class

--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Re: error in constructor

Benjamin FOURTICQ
I imagine the constructor is called twice, because when you call $toto =
new ProjectList()
ProjectList::__construct() is called and so GtkTreeview::__construct(),
then ProjectList::ProjectList() because it exists.
In my opinion, if you just need to set the model use set_model() instead
of calling the prent constructor.

Hope this helps,
Benjamin.

Le 31/08/2009 19:07, Der Starchaser a écrit :

>
> I want to program a class ProjectList, which is derived from class
> GtkTreeView. The following code snippet results in an assertion error. Is
> anybody able to explain me the error ?
>
>
> class ProjectList extends GtkTreeView
> {
>
>      protected $store;
>
>      /***********************************************************/
>      /* Constructor Method
>      /***********************************************************/
>      function ProjectList()
>      {
>
>          $this->store = new GtkListStore(
> GObject::TYPE_STRING, GObject::TYPE_STRING
> );
>
>          parent::__construct( $this->store );
>
>                  $col = 0;
>                  $column = $this->get_column(0);
>                  assert( $column ); //<- this line throws an assertion error
>                  $cell_renderer = new GtkCellRendererPixbuf();
>                  $column->pack_start($cell_renderer, false);
>
> $column->set_cell_data_func(
> $cell_renderer,
> "prjlist_format_pixbuf",
> $this->Pixmap
> );
>
>      }
>
> } // of class
>
>    


--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Re: error in constructor

Elizabeth M Smith
Benjamin FOURTICQ wrote:

> I imagine the constructor is called twice, because when you call $toto =
> new ProjectList()
> ProjectList::__construct() is called and so GtkTreeview::__construct(),
> then ProjectList::ProjectList() because it exists.
> In my opinion, if you just need to set the model use set_model() instead
> of calling the prent constructor.
>
> Hope this helps,
> Benjamin.
>
> Le 31/08/2009 19:07, Der Starchaser a écrit :
>>
>> I want to program a class ProjectList, which is derived from class
>> GtkTreeView. The following code snippet results in an assertion error. Is
>> anybody able to explain me the error ?
>>
>>
>> class ProjectList extends GtkTreeView
>> {
>>
>>      protected $store;
>>
>>      /***********************************************************/
>>      /* Constructor Method
>>      /***********************************************************/
>>      function ProjectList()
>>      {
>>
>>          $this->store = new GtkListStore(
>>         GObject::TYPE_STRING, GObject::TYPE_STRING
>>     );
>>
>>          parent::__construct( $this->store );
>>
>>                  $col = 0;
>>                  $column = $this->get_column(0);
>>                  assert( $column );        //<- this line throws an
>> assertion error
>>                  $cell_renderer = new GtkCellRendererPixbuf();
>>                  $column->pack_start($cell_renderer, false);
>>
>>         $column->set_cell_data_func(
>>             $cell_renderer,
>>             "prjlist_format_pixbuf",
>>             $this->Pixmap
>>         );
>>
>>      }
>>
>> } // of class
>>
>>    
>

ugh - if you want to override the constructor that's fine - do it using

public function __construct() {}

PHP 5 uses unified constructors - the "use the same function as the
class name" support is a holdover for BC, please please don't use it in
new code.... as you see it has some seriously odd side effects

see the manual http://php.net/oop5.decon

Thanks,
Elizabeth

--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Re: error in constructor

jake.cobb (Bugzilla)
In reply to this post by Benjamin FOURTICQ
On Mon, Aug 31, 2009 at 1:32 PM, Benjamin FOURTICQ <
[hidden email]> wrote:

> I imagine the constructor is called twice, because when you call $toto =
> new ProjectList()
> ProjectList::__construct() is called and so GtkTreeview::__construct(),
> then ProjectList::ProjectList() because it exists.
>
>
That is not correct, if there's a ProjectList::__construct() method that
will be called as the constructor and not ProjectList::ProjectList().
ProjectList::ProjectList() only gets called if there is no
ProjectList::__construct() for backwards compatibility.

>
> Le 31/08/2009 19:07, Der Starchaser a écrit :
>
>
>> I want to program a class ProjectList, which is derived from class
>> GtkTreeView. The following code snippet results in an assertion error. Is
>> anybody able to explain me the error ?
>>
>
>
Your assertion fails because there are no columns in the view.  See:
http://gtk.php.net/manual/en/gtk.gtktreeview.php

Specifically:

"After creating the view and setting the model, you need to create some
GtkTreeViewColumns and add them to the view with append_column() . The
column widgets themselves need some GtkCellRenderers that actually draw and
display the data of the model."

-Jake