 |
| View previous topic :: View next topic |
| Author |
Message |
SurfaceTension
Joined: 30 May 2006 Posts: 5
|
Posted: Tue May 30, 2006 4:55 pm Post subject: How do I handle a dynamic number of liststore columns? |
|
|
Hello all. I'm new here and hopefully I'll be able to be a regular contrubitor. I've written a couple tools for work using PHP-GTK2.
One that I'm working on now is a database interface. I want to display data pulled from any table in a database using a liststore/treeview. To use a liststore, it requires a static number of columns. BUT, each table in the database has a different # of columns so I don't know what the store will need until runtime.
How do I program it so that I can create a liststore with a dynamic number of columns that isn't defined until runtime? In the regular gtk+ reference, it appears that the method gtk_list_store_set_column_types() might accomplish this, but I can't find anything corresponding in the php-gtk2 documentation.
Am I missing something or is this something that cannot (yet?) be done with php-gtk2? |
|
| Back to top |
|
 |
squirrel
Joined: 02 May 2006 Posts: 10 Location: Switzerland
|
Posted: Tue May 30, 2006 5:19 pm Post subject: |
|
|
I encounterd the same problem and found the following solution as a workaround (it's really not nice, but working!!):
| Code: | //build up dynamic string with the arguments for the model
$list = 'Gtk::TYPE_STRING, Gtk::TYPE_LONG';
// create the model
$model = eval("return new GtkListStore($list);");
|
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Tue May 30, 2006 5:23 pm Post subject: |
|
|
set_column_types requires an array of the column types to use. It may only be used before adding anything to the liststore, best directly after instantiating it without any parameter.
An alternative I have been using a while now is subclassing GtkListStore, passing an array of types to the constructor and do the following in there:
| Code: | call_user_func_array(
array('parent', '__construct'),
$arTypes
); |
|
|
| Back to top |
|
 |
SurfaceTension
Joined: 30 May 2006 Posts: 5
|
Posted: Tue May 30, 2006 6:09 pm Post subject: |
|
|
I implemented Squirrel's solution and it works great. Thanks for the tips. Here's my proof of concept program:
| Code: | <?php
class test1 extends GtkWindow {
public function __construct() {
parent::__construct();
$this->connect_simple("destroy",array('gtk', 'main_quit'));
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_default_size(450,400);
$frame = new GtkFrame();
$frame->add($this->__create_prop_select());
$this->add($frame);
$this->show_all();
}
private function __create_prop_select() {
//for test 1:
// $aR1 = array("R1::C1","R1::C2");
// $aR2 = array("R2::C1","R2::C2");
//for test 2
$aR1 = array("R1::C1","R1::C2","R1::C3");
$aR2 = array("R2::C1","R2::C2","R2::C3");
$numcols = sizeof($aR1);
$list = "";
for ($i=0;$i<$numcols;$i++) {
$list .= "Gtk::TYPE_STRING";
if ($i < ($numcols - 1))
$list .= ", ";
}
print(">>" . $list . "<<\n");
$model = eval("return new GtkListStore($list);");
$model->append($aR1);
$model->append($aR2);
$this->treeview = new GtkTreeView($model);
$this->treeview->set_size_request(200,300);
for ($i=0;$i<$numcols;$i++) {
$col = new GtkTreeViewColumn('Column' . $i, new GtkCellRendererText(), 'text', $i);
$this->treeview->append_column($col);
}
return $this->treeview;
}
}
$t1 = new test1();
Gtk::main();
?> |
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Tue May 30, 2006 6:14 pm Post subject: |
|
|
| Downside is that "using eval() is bad(TM)", and set_column_types(array(Gtk::TYPE_STRING, GTK::TYPE_STRING)) would be cleaner. |
|
| Back to top |
|
 |
squirrel
Joined: 02 May 2006 Posts: 10 Location: Switzerland
|
Posted: Tue May 30, 2006 9:21 pm Post subject: |
|
|
Here is another example with 'set_column_types'. so forget about my example using 'eval()'!!
| Code: | <?php
// header and data
$arrColumns = array('col1', 'col2', 'col3');
$arrData = array(array('a1', 'a2', 'a3'), array('b1', 'b2', 'b3'));
// create model & treeview
$treeview = new GtkTreeView();
$arrCol = array();
$i = 0;
foreach ($arrColumns as $col) {
// enhance with different col-types!!
$arrCol[] = Gtk::TYPE_STRING;
$treeview->append_column(new GtkTreeViewColumn($col, new GtkCellRendererText(), 'text', $i));
$i++;
}
$model = new GtkListStore();
$model->set_column_types($arrCol);
$treeview->set_model($model);
// fill model
foreach ($arrData as $data) {
$model->append($data);
}
// build dialog
$window = new GtkWindow();
$window->add($treeview);
$window->show_all();
$window->connect_simple('destroy', array('Gtk', 'main_quit'));
gtk::main();
?> |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
 |