3

I tried to set the background color in a Gtk3::TreeViewColumn:

my $red = Gtk3::Gdk::RGBA->new(1, 0.8, 0.8, 1);
my $renderer = Gtk3::CellRendererText->new;
my $column = Gtk3::TreeViewColumn->new_with_attributes
    ($c, $renderer, text => $n, background => $red);

But I get the error:

Gtk-CRITICAL **: 09:54:09.834: gtk_list_store_get_value: assertion 'column < priv->n_columns' failed
GLib-GObject-CRITICAL **: g_value_transform: assertion 'G_IS_VALUE (src_value)' failed at /usr/share/perl5/Gtk3.pm line 568.
GLib-GObject-WARNING **: unable to set property 'background' of type 'gchararray' from value of type '(null)' at /usr/share/perl5/Gtk3.pm line 568.

When I create the color this way, I get the same error:

my $red2 = Gtk3::Gdk::RGBA::parse ('#FF0000');

Can anybody explain how to set the background color?

Complete example:

#! /usr/bin/perl
use strict;
use warnings;
use utf8;

use Gtk3 -init;
use Glib 'TRUE', 'FALSE';

my @COLUMNS = (
  'a',
  'b',
  'c');

my $red = Gtk3::Gdk::RGBA->new(1, 0, 0, 1);
my $red2 = Gtk3::Gdk::RGBA::parse ('#F00');

my $window = Gtk3::Window->new ('toplevel');

my $notebook = Gtk3::Notebook->new;
$window->add($notebook);

my $sw = Gtk3::ScrolledWindow->new (undef, undef);
$notebook->append_page ($sw, Gtk3::Label->new ("tab"));

my $model = Gtk3::ListStore->new (('Glib::String') x scalar @COLUMNS);

my $row = $model->append ();
$model->set ($row,
             0, "a",
             1, "b",
             2, "c");

my $treeview = Gtk3::TreeView->new($model);
$sw->add($treeview);

while (my ($n, $c) = each @COLUMNS) {
  my $renderer = Gtk3::CellRendererText->new;
  my $column = Gtk3::TreeViewColumn->new_with_attributes
      ($c, $renderer, text => $n, background => $red);
  $treeview->append_column($column);
}

$window->show_all;
Gtk3->main();

2 Answers 2

1

The problem is this line:

my $column = Gtk3::TreeViewColumn->new_with_attributes
  ($c, $renderer, text => $n, background => $red);

Here, the background attribute expects a Gtk3::ListStore index, but you are not giving an integer index, rather you are supplying the RGBA object $red.

Here is an example of how you can do it by adding a dummy column to the Gtk::ListStore containing the color as a RGB string:

use feature qw(say);
use strict;
use warnings;
use Gtk3 -init;
use Glib 'TRUE', 'FALSE';

my @COLUMNS = qw( a b c );
my $ncols = scalar @COLUMNS;

my $red = "#f00";
my $window = Gtk3::Window->new ('toplevel');
my $notebook = Gtk3::Notebook->new;
$window->add($notebook);
my $sw = Gtk3::ScrolledWindow->new (undef, undef);
$notebook->append_page ($sw, Gtk3::Label->new ("tab"));

my $model = Gtk3::ListStore->new ((('Glib::String') x $ncols), 'Glib::String');
my $row = $model->append ();
$model->set ($row, (map {($_, $COLUMNS[$_])} 0..$#COLUMNS), $ncols, $red);
my $treeview = Gtk3::TreeView->new($model);
$sw->add($treeview);
while (my ($n, $c) = each @COLUMNS) {
  my $renderer = Gtk3::CellRendererText->new;
  my $column = Gtk3::TreeViewColumn->new_with_attributes
      ($c, $renderer, text => $n, background => $ncols);
  $treeview->append_column($column);
}
$window->signal_connect( destroy  => sub { Gtk3->main_quit() } );
$window->show_all;
Gtk3->main();

See also Python 3 and Gtk+3 - issue with TreeView and alternate colors of rows

0

RGBA means 4 values, called RED, GREEN, BLUE, ALPHA (alias transparency).

If you just have RGB, add a the missing A your columns in range of 00..FF (min..max).

Now call your lines again and the correctly!

model (a, b, c, d)

my $red2 = Gtk3::Gdk::RGBA::parse ('#FF000000');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.