PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Re:Gtktreeview row edit (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Gtktreeview row edit
#908
simhen (User)
Fresh Boarder
Posts: 7
graphgraph
User Offline Click here to see the profile of this user
Gtktreeview row edit 3 Months ago Karma: 0  
Hi,

Can someone please help!!

I have a Gtktreeview and when I click on a row there are two buttons that appears above the tree. Edit and Delete.

When the user clicks on the edit button it gets the rowid from the selected row in the treeview. This all works fine the first time the user clicks on the row and then the button.

What happens when the second time and after is that the rowid gets appended. ie the $view->get_selection returns more than one selection.

How can I resolve this??

Any ideas?

Thanks

H
 
  The administrator has disabled public write access.
#911
kksou (Admin)
Admin
Posts: 443
graph
User Online Now Click here to see the profile of this user
Re:Gtktreeview row edit 3 Months ago Karma: 8  
Could you please provide a sample code so that I can run the program and see what's the problem?

Not too sure what's the problem just based on the description.

Regards,
/kksou
 
  The administrator has disabled public write access.
#932
simhen (User)
Fresh Boarder
Posts: 7
graphgraph
User Offline Click here to see the profile of this user
Re:Gtktreeview row edit 3 Months ago Karma: 0  
Hi,

Thanks for the reply.

It would be difficult to post the code as I use a couple of different classes.

Basically it works like this:

My whole GUI is dynamic in that it gets built from a database. The menus etc is loaded from what type of user logs on to the system. I have a toolbar that have a couple of menu groups. When the user clicks on the toolbar item it loads menu items into a new toolbar and also loads the data for that menu group into a view. Then I create an edit and delete button when the user clicks on a row in the view.

Here is some of the code that gets executed during the selection of the row.


This method loads the data depending on the currently selected menu group.
public function buildView($fields,$sql,$current) {
$db = DBManager::getInstance();
$main = Ceasar_MainWindow::getInstance();
$headingArray = array();
$resultArray = array();
$rowsArray= array();
$db->Connect();
$db->executeQuery($sql);
$groupList = $db->loadResult();
foreach ($groupList as $valArray) {
foreach ($valArray as $key => $value) {
$str .= $value.",";
}
$str = rtrim($str,",");
array_push($rowsArray,$str);
$str = "";
}
$main->loadWorkFrame($rowsArray,$fields,$current);
}

This method loads the data into the view.
It accepts the heading for the view, the data from the db and the current group.
public function loadWorkFrame($data,$headings,$current) {
$main = Ceasar_MainWindow::getInstance();
$workFrame = $main->workFrame;
if (is_object($this->scrolledWin)) {
$workFrame->remove($this->scrolledWin);
}
$main->scrolledWin = new GtkScrolledWindow();
$main->scrolledWin->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$teller=0;
$store = new GtkListStore();

for ($r=0; $r < count($data); $r++) {
$rows = explode(",",$data[$r]);
if ($r==0) { //Build the data types
$ListStoreTypes = array();
$ListStoreTypes = array_fill(0,count($rows),GObject::TYPE_STRING);
$store->set_column_types($ListStoreTypes);
}
$store->append($rows);
}

$this->treeview = new GtkTreeView();
$this->treeview->set_model($store);
$this->selection = $this->treeview->get_selection();
$this->selection->connect_simple('changed',array($this->functions,'loadSubmenu'),$current);

//Set the column headings
for ($i=0; $i < count($headings); $i++) {

$cell_renderer = new GtkCellRendererText();
$column = new GtkTreeViewColumn($headings[$i],$cell_renderer,'text',$i);
$column->set_resizable(true);
$column->set_sort_column_id($i);
if ($i == 0) {
$column->set_visible(false);
}
$this->treeview->insert_column($column,$i);

}

$this->scrolledWin->add($this->treeview);
$workFrame->add($this->scrolledWin);
$this->window->show_all();

$this->editButton->hide();
$this->delButton->hide();


}


This method loads the edit and delete buttons for the currently selected menu group.
public function loadSubmenu($current) {
$main = Ceasar_MainWindow::getInstance();
$view = $main->treeview;
if (!$main->editButton->is_visible) {
$main->editButton->show();
}
if (!$main->delButton->is_visible) {
$main->delButton->show();
}
$editFunction = "edit".get_class($current);
$delFunction = "del".get_class($current);
$main->editButton->connect_simple('clicked',array($current,$editFunction),$view);

}

This method gets the currently selected row in the view and needs to edit the selected row. ie a new popup window with the data displayed in the appropiate fields.

This is where the problem comes in because the rowid gets returned fine the first time the edit button is clicked but returns the current rowid and the previous selected rowids.
public function editMenuItem($view) {
$this->selection = $view->get_selection();
list($model, $iter) = $this->selection->get_selected();
$rowId = $model->get_value($iter, 0);
echo $rowId;
}

I hope that this makes the problem I am experiencing a bit clearer.

Thanks again for the help.

H
 
  The administrator has disabled public write access.
#934
kksou (Admin)
Admin
Posts: 443
graph
User Online Now Click here to see the profile of this user
Re:Gtktreeview row edit 3 Months ago Karma: 8  
Hi,

There's really no magic in debugging. You run the program, look at what's the error messages, set some breakpoints, look at the intermediate variable values, gather all the clues and figure out where the bug is.

Since you did not have any program for me to run, there's no way I can help you debug the program.

For your case, I think you first need to find out why are the signal handler "editMenuItem()" being called twice? (Note: can you try add a return true; at the end of the function and see if it works.)

It's really difficult to debug when your codes get that complicated.

What I usually did is to write a simple, yet standalone code, such as the one in

Sample Code 135: How to edit items in GtkTreeView - using popup dialog?

The standalone code will allow me to try to see if the widgets and the methods work properly first, before I start wrapping them up in classes and interface to mysql.

I know it's a bit tedious. But it works, and allows you to find the bug easily.

Regards,
/kksou
 
  The administrator has disabled public write access.
#945
simhen (User)
Fresh Boarder
Posts: 7
graphgraph
User Offline Click here to see the profile of this user
Re:Gtktreeview row edit 3 Months ago Karma: 0  
I have figured out that the treeview gets appended each time when I call the function

public function editMenuItem() {
$main = Ceasar_MainWindow::getInstance();
$view = $main->treeview;
echo $view."\n";
}

So in effect every single time I call the above function the treeview + 1 gets passed to this function. That is why there are so many selected rowids.

Do you know of any reason why this would happen?

H
 
  The administrator has disabled public write access.
#956
simhen (User)
Fresh Boarder
Posts: 7
graphgraph
User Offline Click here to see the profile of this user
Re:Gtktreeview row edit 3 Months ago Karma: 0  
I got it to work!!

The problem was that I created the buttons in the main class and then hid them.
Then whenever I needed to use the buttons - when the user selected a row - i showed the buttons and used the current selection to get the data from the treeview. First time was fine because there was only one view being used but after that there where actually one+n buttons being used.

So to solve it I just create the buttons when the user clicks on the row in the treeview. I then check to see if the buttons already exists and remove them from the button box if they exist. Then I create new buttons and pass the selected row to my function.

Thanks for the help kksou.
 
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved